8000 [AssetMapper] Disable profiler when the "dev server" respond by smnandre · Pull Request #52100 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Dismiss alert

[AssetMapper] Disable profiler when the "dev server" respond #52100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -137,14 +142,26 @@ 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
{
return [
// priority higher than RouterListener
KernelEvents::REQUEST => [['onKernelRequest', 35]],
// Highest priority possible to bypass all other listeners
KernelEvents::RESPONSE => [['onKernelResponse', 2048]],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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\Component\AssetMapper\Tests\ImportMap;

use PHPUnit\Framework\TestCase;
Expand Down
0