handle disaled csrf protection in the Twig FormExtension

This commit is contained in:
lsmith77 2012-01-23 18:05:48 +01:00
parent 2a998e01b9
commit 3a1699a420
3 changed files with 7 additions and 3 deletions

View File

@ -33,7 +33,7 @@ class FormExtension extends \Twig_Extension
protected $varStack;
protected $template;
public function __construct(CsrfProviderInterface $csrfProvider, array $resources = array())
public function __construct(CsrfProviderInterface $csrfProvider = null, array $resources = array())
{
$this->csrfProvider = $csrfProvider;
$this->themes = new \SplObjectStorage();
@ -298,6 +298,10 @@ class FormExtension extends \Twig_Extension
*/
public function getCsrfToken($intention)
{
if (!$this->csrfProvider instanceof CsrfProviderInterface) {
throw new \BadMethodCallException('CSRF token can only be generated if the "form.csrf_provider" service is available');
}
return $this->csrfProvider->generateCsrfToken($intention);
}

View File

@ -202,7 +202,7 @@ class FormHelper extends Helper
*/
public function csrfToken($intention)
{
if (! $this->csrfProvider instanceof CsrfProviderInterface) {
if (!$this->csrfProvider instanceof CsrfProviderInterface) {
throw new \BadMethodCallException('CSRF token can only be generated if the "form.csrf_provider" service is available');
}

View File

@ -75,7 +75,7 @@
<service id="twig.extension.form" class="%twig.extension.form.class%" public="false">
<tag name="twig.extension" />
<argument type="service" id="form.csrf_provider" />
<argument type="service" id="form.csrf_provider" on-invalid="null" />
<argument>%twig.form.resources%</argument>
</service>