src/Framework/Security/ContentSecurityPolicy/EventSubscriber/ScriptNonceSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Framework\Security\ContentSecurityPolicy\EventSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\WebpackEncoreBundle\Event\RenderAssetTagEvent;
  6. class ScriptNonceSubscriber implements EventSubscriberInterface
  7. {
  8.     public function __construct(protected readonly ContentSecurityPolicyListener $listener)
  9.     {
  10.     }
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             RenderAssetTagEvent::class => 'addNonceToTags',
  15.         ];
  16.     }
  17.     public function addNonceToTags(RenderAssetTagEvent $event): void
  18.     {
  19.         $event->setAttribute('nonce'$this->listener->getNonce());
  20.     }
  21. }