diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 5595e14b36329..f5111cd1096f9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -106,6 +106,7 @@ use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator; +use Symfony\Component\HttpKernel\Profiler\ProfilerStateChecker; use Symfony\Component\JsonStreamer\Attribute\JsonStreamable; use Symfony\Component\JsonStreamer\JsonStreamWriter; use Symfony\Component\JsonStreamer\StreamReaderInterface; @@ -963,6 +964,11 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $ $loader->load('collectors.php'); $loader->load('cache_debug.php'); + if (!class_exists(ProfilerStateChecker::class)) { + $container->removeDefinition('profiler.state_checker'); + $container->removeDefinition('profiler.is_disabled_state_checker'); + } + if ($this->isInitializedConfigEnabled('form')) { $loader->load('form_debug.php'); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.php index 5c426653daeca..842f5b35b412a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.php @@ -25,6 +25,7 @@ service('debug.stopwatch'), service('logger')->nullOnInvalid(), service('.virtual_request_stack')->nullOnInvalid(), + service('profiler.is_disabled_state_checker')->nullOnInvalid(), ]) ->tag('monolog.logger', ['channel' => 'event']) ->tag('kernel.reset', ['method' => 'reset']) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/profiling.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/profiling.php index 4ae34649b4aaf..68fb295bb8768 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/profiling.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/profiling.php @@ -16,6 +16,7 @@ use Symfony\Component\HttpKernel\EventListener\ProfilerListener; use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage; use Symfony\Component\HttpKernel\Profiler\Profiler; +use Symfony\Component\HttpKernel\Profiler\ProfilerStateChecker; return static function (ContainerConfigurator $container) { $container->services() @@ -56,5 +57,15 @@ ->set('.virtual_request_stack', VirtualRequestStack::class) ->args([service('request_stack')]) ->public() + + ->set('profiler.state_checker', ProfilerStateChecker::class) + ->args([ + service_locator(['profiler' => service('profiler')->ignoreOnUninitialized()]), + param('kernel.runtime_mode.web'), + ]) + + ->set('profiler.is_disabled_state_checker', 'Closure') + ->factory(['Closure', 'fromCallable']) + ->args([[service('profiler.state_checker'), 'isProfilerDisabled']]) ; }; diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator_debug.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator_debug.php index e9fe441140742..b195aea2b57b0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator_debug.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator_debug.php @@ -20,6 +20,7 @@ ->decorate('validator', null, 255) ->args([ service('debug.validator.inner'), + service('profiler.is_disabled_state_checker')->nullOnInvalid(), ]) ->tag('kernel.reset', [ 'method' => 'reset', diff --git a/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php b/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php index 43628e4cedbf0..3e1bf2bf7a9a9 100644 --- a/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php @@ -34,6 +34,7 @@ class TraceableAdapter implements AdapterInterface, CacheInterface, NamespacedPo public function __construct( protected AdapterInterface $pool, + protected readonly ?\Closure $disabled = null, ) { } @@ -45,6 +46,9 @@ public function get(string $key, callable $callback, ?float $beta = null, ?array if (!$this->pool instanceof CacheInterface) { throw new BadMethodCallException(\sprintf('Cannot call "%s::get()": this class doesn\'t implement "%s".', get_debug_type($this->pool), CacheInterface::class)); } + if ($this->disabled?->__invoke()) { + return $this->pool->get($key, $callback, $beta, $metadata); + } $isHit = true; $callback = function (CacheItem $item, bool &$save) use ($callback, &$isHit) { @@ -71,6 +75,9 @@ public function get(string $key, callable $callback, ?float $beta = null, ?array public function getItem(mixed $key): CacheItem { + if ($this->disabled?->__invoke()) { + return $this->pool->getItem($key); + } $event = $this->start(__FUNCTION__); try { $item = $this->pool->getItem($key); @@ -88,6 +95,9 @@ public function getItem(mixed $key): CacheItem public function hasItem(mixed $key): bool { + if ($this->disabled?->__invoke()) { + return $this->pool->hasItem($key); + } $event = $this->start(__FUNCTION__); try { return $event->result[$key] = $this->pool->hasItem($key); @@ -98,6 +108,9 @@ public function hasItem(mixed $key): bool public function deleteItem(mixed $key): bool { + if ($this->disabled?->__invoke()) { + return $this->pool->deleteItem($key); + } $event = $this->start(__FUNCTION__); try { return $event->result[$key] = $this->pool->deleteItem($key); @@ -108,6 +121,9 @@ public function deleteItem(mixed $key): bool public function save(CacheItemInterface $item): bool { + if ($this->disabled?->__invoke()) { + return $this->pool->save($item); + } $event = $this->start(__FUNCTION__); try { return $event->result[$item->getKey()] = $this->pool->save($item); @@ -118,6 +134,9 @@ public function save(CacheItemInterface $item): bool public function saveDeferred(CacheItemInterface $item): bool { + if ($this->disabled?->__invoke()) { + return $this->pool->saveDeferred($item); + } $event = $this->start(__FUNCTION__); try { return $event->result[$item->getKey()] = $this->pool->saveDeferred($item); @@ -128,6 +147,9 @@ public function saveDeferred(CacheItemInterface $item): bool public function getItems(array $keys = []): iterable { + if ($this->disabled?->__invoke()) { + return $this->pool->getItems($keys); + } $event = $this->start(__FUNCTION__); try { $result = $this->pool->getItems($keys); @@ -151,6 +173,9 @@ public function getItems(array $keys = []): iterable public function clear(string $prefix = ''): bool { + if ($this->disabled?->__invoke()) { + return $this->pool->clear($prefix); + } $event = $this->start(__FUNCTION__); try { if ($this->pool instanceof AdapterInterface) { @@ -165,6 +190,9 @@ public function clear(string $prefix = ''): bool public function deleteItems(array $keys): bool { + if ($this->disabled?->__invoke()) { + return $this->pool->deleteItems($keys); + } $event = $this->start(__FUNCTION__); $event->result['keys'] = $keys; try { @@ -176,6 +204,9 @@ public function deleteItems(array $keys): bool public function commit(): bool { + if ($this->disabled?->__invoke()) { + return $this->pool->commit(); + } $event = $this->start(__FUNCTION__); try { return $event->result = $this->pool->commit(); @@ -189,6 +220,9 @@ public function prune(): bool if (!$this->pool instanceof PruneableInterface) { return false; } + if ($this->disabled?->__invoke()) { + return $this->pool->prune(); + } $event = $this->start(__FUNCTION__); try { return $event->result = $this->pool->prune(); @@ -208,6 +242,9 @@ public function reset(): void public function delete(string $key): bool { + if ($this->disabled?->__invoke()) { + return $this->pool->deleteItem($key); + } $event = $this->start(__FUNCTION__); try { return $event->result[$key] = $this->pool->deleteItem($key); diff --git a/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php index c85d199e49cb6..bde27c68a740f 100644 --- a/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php @@ -18,13 +18,16 @@ */ class TraceableTagAwareAdapter extends TraceableAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface { - public function __construct(TagAwareAdapterInterface $pool) + public function __construct(TagAwareAdapterInterface $pool, ?\Closure $disabled = null) { - parent::__construct($pool); + parent::__construct($pool, $disabled); } public function invalidateTags(array $tags): bool { + if ($this->disabled?->__invoke()) { + return $this->pool->invalidateTags($tags); + } $event = $this->start(__FUNCTION__); try { return $event->result = $this->pool->invalidateTags($tags); diff --git a/src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php b/src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php index ed957406dafbe..0b8d6aed569dc 100644 --- a/src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php +++ b/src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php @@ -52,7 +52,7 @@ private function addToCollector(string $id, string $name, ContainerBuilder $cont if (!$definition->isPublic() || !$definition->isPrivate()) { $recorder->setPublic($definition->isPublic()); } - $recorder->setArguments([new Reference($innerId = $id.'.recorder_inner')]); + $recorder->setArguments([new Reference($innerId = $id.'.recorder_inner'), new Reference('profiler.is_disabled_state_checker', ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)]); foreach ($definition->getMethodCalls() as [$method, $args]) { if ('setCallbackWrapper' !== $method || !$args[0] instanceof Definition || !($args[0]->getArguments()[2] ?? null) instanceof Definition) { diff --git a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php index 8330ce15e47e9..cd71745ac8935 100644 --- a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php @@ -43,6 +43,7 @@ public function __construct( protected Stopwatch $stopwatch, protected ?LoggerInterface $logger = null, private ?RequestStack $requestStack = null, + protected readonly ?\Closure $disabled = null, ) { } @@ -103,6 +104,9 @@ public function hasListeners(?string $eventName = null): bool public function dispatch(object $event, ?string $eventName = null): object { + if ($this->disabled?->__invoke()) { + return $this->dispatcher->dispatch($event, $eventName); + } $eventName ??= $event::class; $this->callStack ??= new \SplObjectStorage(); diff --git a/src/Symfony/Component/HttpClient/DependencyInjection/HttpClientPass.php b/src/Symfony/Component/HttpClient/DependencyInjection/HttpClientPass.php index 214a655bc6992..2888d2e5c15b2 100644 --- a/src/Symfony/Component/HttpClient/DependencyInjection/HttpClientPass.php +++ b/src/Symfony/Component/HttpClient/DependencyInjection/HttpClientPass.php @@ -27,7 +27,7 @@ public function process(ContainerBuilder $container): void foreach ($container->findTaggedServiceIds('http_client.client') as $id => $tags) { $container->register('.debug.'.$id, TraceableHttpClient::class) - ->setArguments([new Reference('.debug.'.$id.'.inner'), new Reference('debug.stopwatch', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]) + ->setArguments([new Reference('.debug.'.$id.'.inner'), new Reference('debug.stopwatch', ContainerInterface::IGNORE_ON_INVALID_REFERENCE), new Reference('profiler.is_disabled_state_checker', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]) ->addTag('kernel.reset', ['method' => 'reset']) ->setDecoratedService($id, null, 5); $container->getDefinition('data_collector.http_client') diff --git a/src/Symfony/Component/HttpClient/Response/TraceableResponse.php b/src/Symfony/Component/HttpClient/Response/TraceableResponse.php index c8a796d6e94a0..f7d402eb9c6ee 100644 --- a/src/Symfony/Component/HttpClient/Response/TraceableResponse.php +++ b/src/Symfony/Component/HttpClient/Response/TraceableResponse.php @@ -34,7 +34,7 @@ class TraceableResponse implements ResponseInterface, StreamableInterface public function __construct( private HttpClientInterface $client, private ResponseInterface $response, - private mixed &$content, + private mixed &$content = false, private ?StopwatchEvent $event = null, ) { } diff --git a/src/Symfony/Component/HttpClient/TraceableHttpClient.php b/src/Symfony/Component/HttpClient/TraceableHttpClient.php index 83342db58f470..0d6cc51bcd534 100644 --- a/src/Symfony/Component/HttpClient/TraceableHttpClient.php +++ b/src/Symfony/Component/HttpClient/TraceableHttpClient.php @@ -31,12 +31,17 @@ final class TraceableHttpClient implements HttpClientInterface, ResetInterface, public function __construct( private HttpClientInterface $client, private ?Stopwatch $stopwatch = null, + private ?\Closure $disabled = null, ) { $this->tracedRequests = new \ArrayObject(); } public function request(string $method, string $url, array $options = []): ResponseInterface { + if ($this->disabled?->__invoke()) { + return new TraceableResponse($this->client, $this->client->request($method, $url, $options)); + } + $content = null; $traceInfo = []; $this->tracedRequests[] = [ diff --git a/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php b/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php index beca6bfb149a1..915862eddb8cb 100644 --- a/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php +++ b/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php @@ -25,6 +25,9 @@ class TraceableEventDispatcher extends BaseTraceableEventDispatcher { protected function beforeDispatch(string $eventName, object $event): void { + if ($this->disabled?->__invoke()) { + return; + } switch ($eventName) { case KernelEvents::REQUEST: $event->getRequest()->attributes->set('_stopwatch_token', bin2hex(random_bytes(3))); @@ -57,6 +60,9 @@ protected function beforeDispatch(string $eventName, object $event): void protected function afterDispatch(string $eventName, object $event): void { + if ($this->disabled?->__invoke()) { + return; + } switch ($eventName) { case KernelEvents::CONTROLLER_ARGUMENTS: $this->stopwatch->start('controller', 'section'); diff --git a/src/Symfony/Component/HttpKernel/Profiler/ProfilerStateChecker.php b/src/Symfony/Component/HttpKernel/Profiler/ProfilerStateChecker.php new file mode 100644 index 0000000000000..56cb4e3cc597f --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Profiler/ProfilerStateChecker.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Profiler; + +use Psr\Container\ContainerInterface; + +class ProfilerStateChecker +{ + public function __construct( + private ContainerInterface $container, + private bool $defaultEnabled, + ) { + } + + public function isProfilerEnabled(): bool + { + return $this->container->get('profiler')?->isEnabled() ?? $this->defaultEnabled; + } + + public function isProfilerDisabled(): bool + { + return !$this->isProfilerEnabled(); + } +} diff --git a/src/Symfony/Component/HttpKernel/composer.json b/src/Symfony/Component/HttpKernel/composer.json index e9cb077587abb..bb9f4ba6175de 100644 --- a/src/Symfony/Component/HttpKernel/composer.json +++ b/src/Symfony/Component/HttpKernel/composer.json @@ -19,7 +19,7 @@ "php": ">=8.2", "symfony/deprecation-contracts": "^2.5|^3", "symfony/error-handler": "^6.4|^7.0", - "symfony/event-dispatcher": "^6.4|^7.0", + "symfony/event-dispatcher": "^7.3", "symfony/http-foundation": "^7.3", "symfony/polyfill-ctype": "^1.8", "psr/log": "^1|^2|^3" diff --git a/src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php b/src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php index ff81188b87857..41985459c63f1 100644 --- a/src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php +++ b/src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php @@ -337,7 +337,7 @@ private function registerBusToCollector(ContainerBuilder $container, string $bus { $container->setDefinition( $tracedBusId = 'debug.traced.'.$busId, - (new Definition(TraceableMessageBus::class, [new Reference($tracedBusId.'.inner')]))->setDecoratedService($busId) + (new Definition(TraceableMessageBus::class, [new Reference($tracedBusId.'.inner'), new Reference('profiler.is_disabled_state_checker', ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)]))->setDecoratedService($busId) ); $container->getDefinition('data_collector.messenger')->addMethodCall('registerBus', [$busId, new Reference($tracedBusId)]); diff --git a/src/Symfony/Component/Messenger/TraceableMessageBus.php b/src/Symfony/Component/Messenger/TraceableMessageBus.php index 7f0ac09219f18..b5fb6eea3782a 100644 --- a/src/Symfony/Component/Messenger/TraceableMessageBus.php +++ b/src/Symfony/Component/Messenger/TraceableMessageBus.php @@ -20,11 +20,16 @@ class TraceableMessageBus implements MessageBusInterface public function __construct( private MessageBusInterface $decoratedBus, + protected readonly ?\Closure $disabled = null, ) { } public function dispatch(object $message, array $stamps = []): Envelope { + if ($this->disabled?->__invoke()) { + return $this->decoratedBus->dispatch($message, $stamps); + } + $envelope = Envelope::wrap($message, $stamps); $context = [ 'stamps' => array_merge([], ...array_values($envelope->all())), diff --git a/src/Symfony/Component/Validator/Validator/TraceableValidator.php b/src/Symfony/Component/Validator/Validator/TraceableValidator.php index 5442c53da5a56..6f9ab5bbc4303 100644 --- a/src/Symfony/Component/Validator/Validator/TraceableValidator.php +++ b/src/Symfony/Component/Validator/Validator/TraceableValidator.php @@ -29,6 +29,7 @@ class TraceableValidator implements ValidatorInterface, ResetInterface public function __construct( private ValidatorInterface $validator, + protected readonly ?\Closure $disabled = null, ) { } @@ -56,6 +57,10 @@ public function validate(mixed $value, Constraint|array|null $constraints = null { $violations = $this->validator->validate($value, $constraints, $groups); + if ($this->disabled?->__invoke()) { + return $violations; + } + $trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 7); $file = $trace[0]['file']; diff --git a/src/Symfony/Component/Workflow/Debug/TraceableWorkflow.php b/src/Symfony/Component/Workflow/Debug/TraceableWorkflow.php index 6d0afd80cf620..c783e63541dd5 100644 --- a/src/Symfony/Component/Workflow/Debug/TraceableWorkflow.php +++ b/src/Symfony/Component/Workflow/Debug/TraceableWorkflow.php @@ -30,6 +30,7 @@ class TraceableWorkflow implements WorkflowInterface public function __construct( private readonly WorkflowInterface $workflow, private readonly Stopwatch $stopwatch, + protected readonly ?\Closure $disabled = null, ) { } @@ -90,6 +91,9 @@ public function getCalls(): array private function callInner(string $method, array $args): mixed { + if ($this->disabled?->__invoke()) { + return $this->workflow->{$method}(...$args); + } $sMethod = $this->workflow::class.'::'.$method; $this->stopwatch->start($sMethod, 'workflow'); diff --git a/src/Symfony/Component/Workflow/DependencyInjection/WorkflowDebugPass.php b/src/Symfony/Component/Workflow/DependencyInjection/WorkflowDebugPass.php index 634605dffa5ee..042aaba8162a8 100644 --- a/src/Symfony/Component/Workflow/DependencyInjection/WorkflowDebugPass.php +++ b/src/Symfony/Component/Workflow/DependencyInjection/WorkflowDebugPass.php @@ -31,6 +31,7 @@ public function process(ContainerBuilder $container): void ->setArguments([ new Reference("debug.{$id}.inner"), new Reference('debug.stopwatch'), + new Reference('profiler.is_disabled_state_checker', ContainerBuilder::IGNORE_ON_INVALID_REFERENCE), ]); } }