vendor/nelmio/security-bundle/Twig/NelmioCSPTwigExtension.php line 34

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Nelmio SecurityBundle.
  4.  *
  5.  * (c) Nelmio <hello@nelm.io>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Nelmio\SecurityBundle\Twig;
  11. use Nelmio\SecurityBundle\ContentSecurityPolicy\ShaComputer;
  12. use Nelmio\SecurityBundle\EventListener\ContentSecurityPolicyListener;
  13. use Nelmio\SecurityBundle\Twig\TokenParser\CSPScriptParser;
  14. use Nelmio\SecurityBundle\Twig\TokenParser\CSPStyleParser;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFunction;
  17. class NelmioCSPTwigExtension extends AbstractExtension
  18. {
  19.     private $listener;
  20.     private $shaComputer;
  21.     public function __construct(ContentSecurityPolicyListener $listenerShaComputer $shaComputer)
  22.     {
  23.         $this->listener $listener;
  24.         $this->shaComputer $shaComputer;
  25.     }
  26.     public function getTokenParsers()
  27.     {
  28.         return array(new CSPScriptParser($this->shaComputer), new CSPStyleParser($this->shaComputer));
  29.     }
  30.     public function getListener()
  31.     {
  32.         return $this->listener;
  33.     }
  34.     public function getName()
  35.     {
  36.         return 'Nelmio\\SecurityBundle\\Twig\\NelmioCSPTwigExtension';
  37.     }
  38.     public function getFunctions()
  39.     {
  40.         return array(
  41.             new TwigFunction('csp_nonce', array($this'getCSPNonce')),
  42.         );
  43.     }
  44.     public function getCSPNonce($usage null)
  45.     {
  46.         if (null === $nonce $this->listener->getNonce($usage)) {
  47.             throw new \RuntimeException('You must enable nonce to use this feature');
  48.         }
  49.         return $nonce;
  50.     }
  51. }