Skip to content

Commit 3a5a93e

Browse files
Don't enable tracing unless the profiler is enabled
1 parent 904df78 commit 3a5a93e

File tree

16 files changed

+105
-5
lines changed

16 files changed

+105
-5
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+
false,
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+
false,
2324
])
2425
->tag('kernel.reset', [
2526
'method' => 'reset',

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

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

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

@@ -45,6 +46,9 @@ public function get(string $key, callable $callback, ?float $beta = null, ?array
4546
if (!$this->pool instanceof CacheInterface) {
4647
throw new BadMethodCallException(\sprintf('Cannot call "%s::get()": this class doesn\'t implement "%s".', get_debug_type($this->pool), CacheInterface::class));
4748
}
49+
if (!$this->enabled) {
50+
return $this->pool->get($key, $callback, $beta, $metadata);
51+
}
4852

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

7276
public function getItem(mixed $key): CacheItem
7377
{
78+
if (!$this->enabled) {
79+
return $this->pool->getItem($key);
80+
}
7481
$event = $this->start(__FUNCTION__);
7582
try {
7683
$item = $this->pool->getItem($key);
@@ -88,6 +95,9 @@ public function getItem(mixed $key): CacheItem
8895

8996
public function hasItem(mixed $key): bool
9097
{
98+
if (!$this->enabled) {
99+
return $this->pool->hasItem($key);
100+
}
91101
$event = $this->start(__FUNCTION__);
92102
try {
93103
return $event->result[$key] = $this->pool->hasItem($key);
@@ -98,6 +108,9 @@ public function hasItem(mixed $key): bool
98108

99109
public function deleteItem(mixed $key): bool
100110
{
111+
if (!$this->enabled) {
112+
return $this->pool->deleteItem($key);
113+
}
101114
$event = $this->start(__FUNCTION__);
102115
try {
103116
return $event->result[$key] = $this->pool->deleteItem($key);
@@ -108,6 +121,9 @@ public function deleteItem(mixed $key): bool
108121

109122
public function save(CacheItemInterface $item): bool
110123
{
124+
if (!$this->enabled) {
125+
return $this->pool->save($item);
126+
}
111127
$event = $this->start(__FUNCTION__);
112128
try {
113129
return $event->result[$item->getKey()] = $this->pool->save($item);
@@ -118,6 +134,9 @@ public function save(CacheItemInterface $item): bool
118134

119135
public function saveDeferred(CacheItemInterface $item): bool
120136
{
137+
if (!$this->enabled) {
138+
return $this->pool->saveDeferred($item);
139+
}
121140
$event = $this->start(__FUNCTION__);
122141
try {
123142
return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
@@ -128,6 +147,9 @@ public function saveDeferred(CacheItemInterface $item): bool
128147

129148
public function getItems(array $keys = []): iterable
130149
{
150+
if (!$this->enabled) {
151+
return $this->pool->getItems($keys);
152+
}
131153
$event = $this->start(__FUNCTION__);
132154
try {
133155
$result = $this->pool->getItems($keys);
@@ -151,6 +173,9 @@ public function getItems(array $keys = []): iterable
151173

152174
public function clear(string $prefix = ''): bool
153175
{
176+
if (!$this->enabled) {
177+
return $this->pool->clear($prefix);
178+
}
154179
$event = $this->start(__FUNCTION__);
155180
try {
156181
if ($this->pool instanceof AdapterInterface) {
@@ -165,6 +190,9 @@ public function clear(string $prefix = ''): bool
165190

166191
public function deleteItems(array $keys): bool
167192
{
193+
if (!$this->enabled) {
194+
return $this->pool->deleteItems($keys);
195+
}
168196
$event = $this->start(__FUNCTION__);
169197
$event->result['keys'] = $keys;
170198
try {
@@ -176,6 +204,9 @@ public function deleteItems(array $keys): bool
176204

177205
public function commit(): bool
178206
{
207+
if (!$this->enabled) {
208+
return $this->pool->commit();
209+
}
179210
$event = $this->start(__FUNCTION__);
180211
try {
181212
return $event->result = $this->pool->commit();
@@ -189,6 +220,9 @@ public function prune(): bool
189220
if (!$this->pool instanceof PruneableInterface) {
190221
return false;
191222
}
223+
if (!$this->enabled) {
224+
return $this->pool->prune();
225+
}
192226
$event = $this->start(__FUNCTION__);
193227
try {
194228
return $event->result = $this->pool->prune();
@@ -208,6 +242,9 @@ public function reset(): void
208242

209243
public function delete(string $key): bool
210244
{
245+
if (!$this->enabled) {
246+
return $this->pool->deleteItem($key);
247+
}
211248
$event = $this->start(__FUNCTION__);
212249
try {
213250
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'), false]);
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function __construct(
4343
protected Stopwatch $stopwatch,
4444
protected ?LoggerInterface $logger = null,
4545
private ?RequestStack $requestStack = null,
46+
public bool $enabled = true,
4647
) {
4748
}
4849

@@ -103,6 +104,9 @@ public function hasListeners(?string $eventName = null): bool
103104

104105
public function dispatch(object $event, ?string $eventName = null): object
105106
{
107+
if (!$this->enabled) {
108+
return $this->dispatcher->dispatch($event, $eventName);
109+
}
106110
$eventName ??= $event::class;
107111

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

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace Symfony\Component\HttpKernel\DataCollector;
1313

14-
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
1514
use Symfony\Component\HttpFoundation\Request;
1615
use Symfony\Component\HttpFoundation\RequestStack;
1716
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
1818
use Symfony\Component\VarDumper\Cloner\Data;
1919
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2020
use Symfony\Contracts\Service\ResetInterface;
@@ -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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
namespace Symfony\Component\HttpKernel\Debug;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher as BaseTraceableEventDispatcher;
16+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17+
use Symfony\Component\HttpFoundation\RequestStack;
1518
use Symfony\Component\HttpKernel\KernelEvents;
19+
use Symfony\Component\Stopwatch\Stopwatch;
1620

1721
/**
1822
* Collects some data about event listeners.
@@ -23,8 +27,21 @@
2327
*/
2428
class TraceableEventDispatcher extends BaseTraceableEventDispatcher
2529
{
30+
public function __construct(
31+
EventDispatcherInterface $dispatcher,
32+
Stopwatch $stopwatch,
33+
?LoggerInterface $logger = null,
34+
?RequestStack $requestStack = null,
35+
public bool $enabled = true,
36+
) {
37+
parent::__construct($dispatcher, $stopwatch, $logger, $requestStack, $enabled);
38+
}
39+
2640
protected function beforeDispatch(string $eventName, object $event): void
2741
{
42+
if (!$this->enabled) {
43+
return;
44+
}
2845
switch ($eventName) {
2946
case KernelEvents::REQUEST:
3047
$event->getRequest()->attributes->set('_stopwatch_token', bin2hex(random_bytes(3)));
@@ -57,6 +74,9 @@ protected function beforeDispatch(string $eventName, object $event): void
5774

5875
protected function afterDispatch(string $eventName, object $event): void
5976
{
77+
if (!$this->enabled) {
78+
return;
79+
}
6080
switch ($eventName) {
6181
case KernelEvents::CONTROLLER_ARGUMENTS:
6282
$this->stopwatch->start('controller', 'section');

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'), false]))->setDecoratedService($busId)
341341
);
342342

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

src/Symfony/Component/Messenger/TraceableMessageBus.php

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

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

2627
public function dispatch(object $message, array $stamps = []): Envelope
2728
{
29+
if (!$this->enabled) {
30+
return $this->decoratedBus->dispatch($message, $stamps);
31+
}
2832
$envelope = Envelope::wrap($message, $stamps);
2933
$context = [
3034
'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

src/Symfony/Component/Validator/Validator/TraceableValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class TraceableValidator implements ValidatorInterface, ResetInterface
2929

3030
public function __construct(
3131
private ValidatorInterface $validator,
32+
public bool $enabled = true,
3233
) {
3334
}
3435

@@ -56,6 +57,10 @@ public function validate(mixed $value, Constraint|array|null $constraints = null
5657
{
5758
$violations = $this->validator->validate($value, $constraints, $groups);
5859

60+
if (!$this->enabled) {
61+
return $violations;
62+
}
63+
5964
$trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 7);
6065

6166
$file = $trace[0]['file'];

src/Symfony/Component/Workflow/DataCollector/WorkflowDataCollector.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public function lateCollect(): void
6565

6666
public function getName(): string
6767
{
68+
// Don't loop in the constructor to defer initializing workflows post-instantiation
69+
foreach ($this->workflows ?? [] as $workflow) {
70+
if ($workflow instanceof TraceableWorkflow) {
71+
$workflow->enabled = true;
72+
} elseif (!$workflow instanceof WorkflowInterface) {
73+
throw new \InvalidArgumentException(sprintf('Each workflow must implement "%s".', WorkflowInterface::class));
74+
}
75+
}
76+
6877
return 'workflow';
6978
}
7079

src/Symfony/Component/Workflow/Debug/TraceableWorkflow.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TraceableWorkflow implements WorkflowInterface
3030
public function __construct(
3131
private readonly WorkflowInterface $workflow,
3232
private readonly Stopwatch $stopwatch,
33+
public bool $enabled = true,
3334
) {
3435
}
3536

@@ -90,6 +91,9 @@ public function getCalls(): array
9091

9192
private function callInner(string $method, array $args): mixed
9293
{
94+
if (!$this->enabled) {
95+
return $this->workflow->{$method}(...$args);
96+
}
9397
$sMethod = $this->workflow::class.'::'.$method;
9498
$this->stopwatch->start($sMethod, 'workflow');
9599

0 commit comments

Comments
 (0)