Skip to content

Commit

Permalink
[FrameworkBundle][TwigBridge] Fix BC break from strong dependency on …
Browse files Browse the repository at this point in the history
…CSRF token storage
  • Loading branch information
tgalopin committed May 31, 2018
1 parent 8bbd738 commit 68994a6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
16 changes: 2 additions & 14 deletions src/Symfony/Bridge/Twig/Extension/CsrfExtension.php
Expand Up @@ -11,34 +11,22 @@

namespace Symfony\Bridge\Twig\Extension;

use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
* @author Titouan Galopin <galopintitouan@gmail.com>
*/
class CsrfExtension extends AbstractExtension
{
private $csrfTokenManager;

public function __construct(CsrfTokenManagerInterface $csrfTokenManager)
{
$this->csrfTokenManager = $csrfTokenManager;
}

/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return array(
new TwigFunction('csrf_token', array($this, 'getCsrfToken')),
new TwigFunction('csrf_token', array(CsrfRuntime::class, 'getCsrfToken')),
);
}

public function getCsrfToken(string $tokenId): string
{
return $this->csrfTokenManager->getToken($tokenId)->getValue();
}
}
33 changes: 33 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/CsrfRuntime.php
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Extension;

use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;

/**
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
* @author Titouan Galopin <galopintitouan@gmail.com>
*/
class CsrfRuntime
{
private $csrfTokenManager;

public function __construct(CsrfTokenManagerInterface $csrfTokenManager)
{
$this->csrfTokenManager = $csrfTokenManager;
}

public function getCsrfToken(string $tokenId): string
{
return $this->csrfTokenManager->getToken($tokenId)->getValue();
}
}
Expand Up @@ -22,9 +22,13 @@
</service>
<service id="Symfony\Component\Security\Csrf\CsrfTokenManagerInterface" alias="security.csrf.token_manager" />

<service id="twig.runtime.security_csrf" class="Symfony\Bridge\Twig\Extension\CsrfRuntime">
<tag name="twig.runtime" />
<argument type="service" id="security.csrf.token_manager" />
</service>

<service id="twig.extension.security_csrf" class="Symfony\Bridge\Twig\Extension\CsrfExtension">
<tag name="twig.extension" />
<argument type="service" id="security.csrf.token_manager" />
</service>
</services>
</container>

0 comments on commit 68994a6

Please sign in to comment.