Skip to content

Commit a9e77cb

Browse files
Don't enable tracing unless the profiler is instantiated
1 parent 904df78 commit a9e77cb

File tree

22 files changed

+154
-7
lines changed

22 files changed

+154
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
service('debug.stopwatch'),
2626
service('logger')->nullOnInvalid(),
2727
service('.virtual_request_stack')->nullOnInvalid(),
28+
'%kernel.runtime_mode.web%',
2829
])
2930
->tag('monolog.logger', ['channel' => 'event'])
3031
->tag('kernel.reset', ['method' => 'reset'])

src/Symfony/Bundle/FrameworkBundle/Resources/config/validator_debug.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
->decorate('validator', null, 255)
2121
->args([
2222
service('debug.validator.inner'),
23+
'%kernel.runtime_mode.web%',
2324
])
2425
->tag('kernel.reset', [
2526
'method' => 'reset',

src/Symfony/Component/Cache/Adapter/TraceableAdapter.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,19 @@ class TraceableAdapter implements AdapterInterface, CacheInterface, NamespacedPo
3434

3535
public function __construct(
3636
protected AdapterInterface $pool,
37+
private bool $enabled = true,
3738
) {
3839
}
3940

41+
final public function enabled(?bool $enabled = null): bool
42+
{
43+
if (null === $enabled) {
44+
return $this->enabled;
45+
}
46+
47+
return $this->enabled = $enabled;
48+
}
49+
4050
/**
4151
* @throws BadMethodCallException When the item pool is not a CacheInterface
4252
*/
@@ -45,6 +55,9 @@ public function get(string $key, callable $callback, ?float $beta = null, ?array
4555
if (!$this->pool instanceof CacheInterface) {
4656
throw new BadMethodCallException(\sprintf('Cannot call "%s::get()": this class doesn\'t implement "%s".', get_debug_type($this->pool), CacheInterface::class));
4757
}
58+
if (!$this->enabled()) {
59+
return $this->pool->get($key, $callback, $beta, $metadata);
60+
}
4861

4962
$isHit = true;
5063
$callback = function (CacheItem $item, bool &$save) use ($callback, &$isHit) {
@@ -71,6 +84,9 @@ public function get(string $key, callable $callback, ?float $beta = null, ?array
7184

7285
public function getItem(mixed $key): CacheItem
7386
{
87+
if (!$this->enabled()) {
88+
return $this->pool->getItem($key);
89+
}
7490
$event = $this->start(__FUNCTION__);
7591
try {
7692
$item = $this->pool->getItem($key);
@@ -88,6 +104,9 @@ public function getItem(mixed $key): CacheItem
88104

89105
public function hasItem(mixed $key): bool
90106
{
107+
if (!$this->enabled()) {
108+
return $this->pool->hasItem($key);
109+
}
91110
$event = $this->start(__FUNCTION__);
92111
try {
93112
return $event->result[$key] = $this->pool->hasItem($key);
@@ -98,6 +117,9 @@ public function hasItem(mixed $key): bool
98117

99118
public function deleteItem(mixed $key): bool
100119
{
120+
if (!$this->enabled()) {
121+
return $this->pool->deleteItem($key);
122+
}
101123
$event = $this->start(__FUNCTION__);
102124
try {
103125
return $event->result[$key] = $this->pool->deleteItem($key);
@@ -108,6 +130,9 @@ public function deleteItem(mixed $key): bool
108130

109131
public function save(CacheItemInterface $item): bool
110132
{
133+
if (!$this->enabled()) {
134+
return $this->pool->save($item);
135+
}
111136
$event = $this->start(__FUNCTION__);
112137
try {
113138
return $event->result[$item->getKey()] = $this->pool->save($item);
@@ -118,6 +143,9 @@ public function save(CacheItemInterface $item): bool
118143

119144
public function saveDeferred(CacheItemInterface $item): bool
120145
{
146+
if (!$this->enabled()) {
147+
return $this->pool->saveDeferred($item);
148+
}
121149
$event = $this->start(__FUNCTION__);
122150
try {
123151
return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
@@ -128,6 +156,9 @@ public function saveDeferred(CacheItemInterface $item): bool
128156

129157
public function getItems(array $keys = []): iterable
130158
{
159+
if (!$this->enabled()) {
160+
return $this->pool->getItems($keys);
161+
}
131162
$event = $this->start(__FUNCTION__);
132163
try {
133164
$result = $this->pool->getItems($keys);
@@ -151,6 +182,9 @@ public function getItems(array $keys = []): iterable
151182

152183
public function clear(string $prefix = ''): bool
153184
{
185+
if (!$this->enabled()) {
186+
return $this->pool->clear($prefix);
187+
}
154188
$event = $this->start(__FUNCTION__);
155189
try {
156190
if ($this->pool instanceof AdapterInterface) {
@@ -165,6 +199,9 @@ public function clear(string $prefix = ''): bool
165199

166200
public function deleteItems(array $keys): bool
167201
{
202+
if (!$this->enabled()) {
203+
return $this->pool->deleteItems($keys);
204+
}
168205
$event = $this->start(__FUNCTION__);
169206
$event->result['keys'] = $keys;
170207
try {
@@ -176,6 +213,9 @@ public function deleteItems(array $keys): bool
176213

177214
public function commit(): bool
178215
{
216+
if (!$this->enabled()) {
217+
return $this->pool->commit();
218+
}
179219
$event = $this->start(__FUNCTION__);
180220
try {
181221
return $event->result = $this->pool->commit();
@@ -189,6 +229,9 @@ public function prune(): bool
189229
if (!$this->pool instanceof PruneableInterface) {
190230
return false;
191231
}
232+
if (!$this->enabled()) {
233+
return $this->pool->prune();
234+
}
192235
$event = $this->start(__FUNCTION__);
193236
try {
194237
return $event->result = $this->pool->prune();
@@ -208,6 +251,9 @@ public function reset(): void
208251

209252
public function delete(string $key): bool
210253
{
254+
if (!$this->enabled()) {
255+
return $this->pool->deleteItem($key);
256+
}
211257
$event = $this->start(__FUNCTION__);
212258
try {
213259
return $event->result[$key] = $this->pool->deleteItem($key);

src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
*/
1919
class TraceableTagAwareAdapter extends TraceableAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface
2020
{
21-
public function __construct(TagAwareAdapterInterface $pool)
21+
public function __construct(TagAwareAdapterInterface $pool, bool $enabled = true)
2222
{
23-
parent::__construct($pool);
23+
parent::__construct($pool, $enabled);
2424
}
2525

2626
public function invalidateTags(array $tags): bool
2727
{
28+
if (!$this->enabled()) {
29+
return $this->pool->invalidateTags($tags);
30+
}
2831
$event = $this->start(__FUNCTION__);
2932
try {
3033
return $event->result = $this->pool->invalidateTags($tags);

src/Symfony/Component/Cache/DataCollector/CacheDataCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CacheDataCollector extends DataCollector implements LateDataCollectorInter
3434
public function addInstance(string $name, TraceableAdapter $instance): void
3535
{
3636
$this->instances[$name] = $instance;
37+
$instance->enabled(true);
3738
}
3839

3940
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void

src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private function addToCollector(string $id, string $name, ContainerBuilder $cont
5252
if (!$definition->isPublic() || !$definition->isPrivate()) {
5353
$recorder->setPublic($definition->isPublic());
5454
}
55-
$recorder->setArguments([new Reference($innerId = $id.'.recorder_inner')]);
55+
$recorder->setArguments([new Reference($innerId = $id.'.recorder_inner'), '%kernel.runtime_mode.web%']);
5656

5757
foreach ($definition->getMethodCalls() as [$method, $args]) {
5858
if ('setCallbackWrapper' !== $method || !$args[0] instanceof Definition || !($args[0]->getArguments()[2] ?? null) instanceof Definition) {

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,19 @@ public function __construct(
4343
protected Stopwatch $stopwatch,
4444
protected ?LoggerInterface $logger = null,
4545
private ?RequestStack $requestStack = null,
46+
private bool $enabled = true,
4647
) {
4748
}
4849

50+
final public function enabled(?bool $enabled = null): bool
51+
{
52+
if (null === $enabled) {
53+
return $this->enabled;
54+
}
55+
56+
return $this->enabled = $enabled;
57+
}
58+
4959
public function addListener(string $eventName, callable|array $listener, int $priority = 0): void
5060
{
5161
$this->dispatcher->addListener($eventName, $listener, $priority);
@@ -103,6 +113,9 @@ public function hasListeners(?string $eventName = null): bool
103113

104114
public function dispatch(object $event, ?string $eventName = null): object
105115
{
116+
if (!$this->enabled()) {
117+
return $this->dispatcher->dispatch($event, $eventName);
118+
}
106119
$eventName ??= $event::class;
107120

108121
$this->callStack ??= new \SplObjectStorage();

src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ final class HttpClientDataCollector extends DataCollector implements LateDataCol
3636
public function registerClient(string $name, TraceableHttpClient $client): void
3737
{
3838
$this->clients[$name] = $client;
39+
$client->enabled(true);
3940
}
4041

4142
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void

src/Symfony/Component/HttpClient/DependencyInjection/HttpClientPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function process(ContainerBuilder $container): void
2727

2828
foreach ($container->findTaggedServiceIds('http_client.client') as $id => $tags) {
2929
$container->register('.debug.'.$id, TraceableHttpClient::class)
30-
->setArguments([new Reference('.debug.'.$id.'.inner'), new Reference('debug.stopwatch', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)])
30+
->setArguments([new Reference('.debug.'.$id.'.inner'), new Reference('debug.stopwatch', ContainerInterface::IGNORE_ON_INVALID_REFERENCE), '%kernel.runtime_mode.web%'])
3131
->addTag('kernel.reset', ['method' => 'reset'])
3232
->setDecoratedService($id, null, 5);
3333
$container->getDefinition('data_collector.http_client')

src/Symfony/Component/HttpClient/Response/TraceableResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TraceableResponse implements ResponseInterface, StreamableInterface
3434
public function __construct(
3535
private HttpClientInterface $client,
3636
private ResponseInterface $response,
37-
private mixed &$content,
37+
private mixed &$content = false,
3838
private ?StopwatchEvent $event = null,
3939
) {
4040
}

src/Symfony/Component/HttpClient/TraceableHttpClient.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,26 @@ final class TraceableHttpClient implements HttpClientInterface, ResetInterface,
3131
public function __construct(
3232
private HttpClientInterface $client,
3333
private ?Stopwatch $stopwatch = null,
34+
private bool $enabled = true,
3435
) {
3536
$this->tracedRequests = new \ArrayObject();
3637
}
3738

39+
public function enabled(?bool $enabled = null): bool
40+
{
41+
if (null === $enabled) {
42+
return $this->enabled;
43+
}
44+
45+
return $this->enabled = $enabled;
46+
}
47+
3848
public function request(string $method, string $url, array $options = []): ResponseInterface
3949
{
50+
if (!$this->enabled()) {
51+
return new TraceableResponse($this->client, $this->client->request($method, $url, $options));
52+
}
53+
4054
$content = null;
4155
$traceInfo = [];
4256
$this->tracedRequests[] = [

src/Symfony/Component/HttpKernel/DataCollector/EventDataCollector.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ public function getOrphanedEvents(?string $dispatcher = null): array|Data
135135

136136
public function getName(): string
137137
{
138+
// Don't loop in the constructor to defer initializing dispatchers post-instantiation
139+
foreach ($this->dispatchers ?? [] as $dispatcher) {
140+
if ($dispatcher instanceof TraceableEventDispatcher) {
141+
$dispatcher->enabled(true);
142+
} elseif (!$dispatcher instanceof EventDispatcherInterface) {
143+
throw new \InvalidArgumentException(sprintf('Each dispatcher must implement "%s".', EventDispatcherInterface::class));
144+
}
145+
}
146+
138147
return 'events';
139148
}
140149
}

src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class TraceableEventDispatcher extends BaseTraceableEventDispatcher
2525
{
2626
protected function beforeDispatch(string $eventName, object $event): void
2727
{
28+
if (!$this->enabled()) {
29+
return;
30+
}
2831
switch ($eventName) {
2932
case KernelEvents::REQUEST:
3033
$event->getRequest()->attributes->set('_stopwatch_token', bin2hex(random_bytes(3)));
@@ -57,6 +60,9 @@ protected function beforeDispatch(string $eventName, object $event): void
5760

5861
protected function afterDispatch(string $eventName, object $event): void
5962
{
63+
if (!$this->enabled()) {
64+
return;
65+
}
6066
switch ($eventName) {
6167
case KernelEvents::CONTROLLER_ARGUMENTS:
6268
$this->stopwatch->start('controller', 'section');

src/Symfony/Component/HttpKernel/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=8.2",
2020
"symfony/deprecation-contracts": "^2.5|^3",
2121
"symfony/error-handler": "^6.4|^7.0",
22-
"symfony/event-dispatcher": "^6.4|^7.0",
22+
"symfony/event-dispatcher": "^7.3",
2323
"symfony/http-foundation": "^7.3",
2424
"symfony/polyfill-ctype": "^1.8",
2525
"psr/log": "^1|^2|^3"

src/Symfony/Component/Messenger/DataCollector/MessengerDataCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class MessengerDataCollector extends DataCollector implements LateDataCollectorI
3030
public function registerBus(string $name, TraceableMessageBus $bus): void
3131
{
3232
$this->traceableBuses[$name] = $bus;
33+
$bus->enabled(true);
3334
}
3435

3536
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void

src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ private function registerBusToCollector(ContainerBuilder $container, string $bus
337337
{
338338
$container->setDefinition(
339339
$tracedBusId = 'debug.traced.'.$busId,
340-
(new Definition(TraceableMessageBus::class, [new Reference($tracedBusId.'.inner')]))->setDecoratedService($busId)
340+
(new Definition(TraceableMessageBus::class, [new Reference($tracedBusId.'.inner'), '%kernel.runtime_mode.web%']))->setDecoratedService($busId)
341341
);
342342

343343
$container->getDefinition('data_collector.messenger')->addMethodCall('registerBus', [$busId, new Reference($tracedBusId)]);

src/Symfony/Component/Messenger/TraceableMessageBus.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,24 @@ class TraceableMessageBus implements MessageBusInterface
2020

2121
public function __construct(
2222
private MessageBusInterface $decoratedBus,
23+
private bool $enabled = true,
2324
) {
2425
}
2526

27+
final public function enabled(?bool $enabled = null): bool
28+
{
29+
if (null === $enabled) {
30+
return $this->enabled;
31+
}
32+
33+
return $this->enabled = $enabled;
34+
}
35+
2636
public function dispatch(object $message, array $stamps = []): Envelope
2737
{
38+
if (!$this->enabled()) {
39+
return $this->decoratedBus->dispatch($message, $stamps);
40+
}
2841
$envelope = Envelope::wrap($message, $stamps);
2942
$context = [
3043
'stamps' => array_merge([], ...array_values($envelope->all())),

src/Symfony/Component/Validator/DataCollector/ValidatorDataCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct(
3333
private TraceableValidator $validator,
3434
) {
3535
$this->reset();
36+
$validator->enabled(true);
3637
}
3738

3839
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void

0 commit comments

Comments
 (0)