From e1174ac9dd0f71a59763a3780acb36d4323618c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Tue, 17 Oct 2023 11:29:36 +0200 Subject: [PATCH] [AssetMapper] Disable profiler when the "dev server" respond --- .../Resources/config/asset_mapper.php | 4 ++-- .../AssetMapperDevServerSubscriber.php | 17 +++++++++++++++++ ...tMapperDevServerSubscriberFunctionalTest.php | 2 ++ .../Tests/ImportMap/JavaScriptImportTest.php | 9 +++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php index 296358cfcf72c..01d43d2d6413d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php @@ -38,7 +38,6 @@ use Symfony\Component\AssetMapper\ImportMap\Resolver\JsDelivrEsmResolver; use Symfony\Component\AssetMapper\MapperAwareAssetPackage; use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolver; -use Symfony\Component\HttpKernel\Event\RequestEvent; return static function (ContainerConfigurator $container) { $container->services() @@ -92,8 +91,9 @@ abstract_arg('asset public prefix'), abstract_arg('extensions map'), service('cache.asset_mapper'), + service('profiler')->nullOnInvalid(), ]) - ->tag('kernel.event_subscriber', ['event' => RequestEvent::class]) + ->tag('kernel.event_subscriber') ->set('asset_mapper.command.compile', AssetMapperCompileCommand::class) ->args([ diff --git a/src/Symfony/Component/AssetMapper/AssetMapperDevServerSubscriber.php b/src/Symfony/Component/AssetMapper/AssetMapperDevServerSubscriber.php index 72e88b19b9408..174d45c17caf0 100644 --- a/src/Symfony/Component/AssetMapper/AssetMapperDevServerSubscriber.php +++ b/src/Symfony/Component/AssetMapper/AssetMapperDevServerSubscriber.php @@ -15,8 +15,10 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\RequestEvent; +use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\HttpKernel\Profiler\Profiler; /** * Functions like a controller that returns assets from the asset mapper. @@ -104,6 +106,7 @@ public function __construct( string $publicPrefix = '/assets/', array $extensionsMap = [], private readonly ?CacheItemPoolInterface $cacheMapCache = null, + private readonly ?Profiler $profiler = null, ) { $this->publicPrefix = rtrim($publicPrefix, '/').'/'; $this->extensionsMap = array_merge(self::EXTENSIONS_MAP, $extensionsMap); @@ -126,6 +129,8 @@ public function onKernelRequest(RequestEvent $event): void throw new NotFoundHttpException(sprintf('Asset with public path "%s" not found.', $pathInfo)); } + $this->profiler?->disable(); + $mediaType = $this->getMediaType($asset->publicPath); $response = (new Response( $asset->content, @@ -137,7 +142,17 @@ public function onKernelRequest(RequestEvent $event): void ->setEtag($asset->digest) ; + $response->headers->set('X-Assets-Dev', true); + $event->setResponse($response); + $event->stopPropagation(); + } + + public function onKernelResponse(ResponseEvent $event): void + { + if ($event->getResponse()->headers->get('X-Assets-Dev')) { + $event->stopPropagation(); + } } public static function getSubscribedEvents(): array @@ -145,6 +160,8 @@ public static function getSubscribedEvents(): array return [ // priority higher than RouterListener KernelEvents::REQUEST => [['onKernelRequest', 35]], + // Highest priority possible to bypass all other listeners + KernelEvents::RESPONSE => [['onKernelResponse', 2048]], ]; } diff --git a/src/Symfony/Component/AssetMapper/Tests/AssetMapperDevServerSubscriberFunctionalTest.php b/src/Symfony/Component/AssetMapper/Tests/AssetMapperDevServerSubscriberFunctionalTest.php index e8d52c3248b38..b4183b063c720 100644 --- a/src/Symfony/Component/AssetMapper/Tests/AssetMapperDevServerSubscriberFunctionalTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/AssetMapperDevServerSubscriberFunctionalTest.php @@ -30,6 +30,7 @@ public function testGettingAssetWorks() EOF, $response->getContent()); $this->assertSame('"b3445cb7a86a0795a7af7f2004498aef"', $response->headers->get('ETag')); $this->assertSame('immutable, max-age=604800, public', $response->headers->get('Cache-Control')); + $this->assertTrue($response->headers->has('X-Assets-Dev')); } public function test404OnUnknownAsset() @@ -39,6 +40,7 @@ public function test404OnUnknownAsset() $client->request('GET', '/assets/unknown.css'); $response = $client->getResponse(); $this->assertSame(404, $response->getStatusCode()); + $this->assertFalse($response->headers->has('X-Assets-Dev')); } public function test404OnInvalidDigest() diff --git a/src/Symfony/Component/AssetMapper/Tests/ImportMap/JavaScriptImportTest.php b/src/Symfony/Component/AssetMapper/Tests/ImportMap/JavaScriptImportTest.php index 0703ec598bfb1..899ff1bf86a2a 100644 --- a/src/Symfony/Component/AssetMapper/Tests/ImportMap/JavaScriptImportTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/ImportMap/JavaScriptImportTest.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\AssetMapper\Tests\ImportMap; use PHPUnit\Framework\TestCase;