Skip to content

Commit 4d9dc04

Browse files
committed
bug symfony#59515 [FrameworkBundle] Fix wiring ConsoleProfilerListener (nicolas-grekas)
This PR was merged into the 6.4 branch. Discussion ---------- [FrameworkBundle] Fix wiring ConsoleProfilerListener | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT At the moment, when cache:clear is called, the profiler is instantiated, with all its data collectors. When one of them needs an env var to be constructed, 💥 But this should not happen in the first place: command profiling is an opt-in feature. Nothing related should be instantiated unless asked to do so. The issue happens because service registration doesn't take care of not using the profiler unless really needed. This fixes it by: 1. not using the profiler to "disable" it 2. injecting a lazy profiler service so that it's instantiated only when actually required Commits ------- 45763bd [FrameworkBundle] Fix wiring ConsoleProfilerListener
2 parents a9be4d1 + 45763bd commit 4d9dc04

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Symfony/Bundle/FrameworkBundle/EventListener/ConsoleProfilerListener.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ final class ConsoleProfilerListener implements EventSubscriberInterface
3838
/** @var \SplObjectStorage<Request, ?Request> */
3939
private \SplObjectStorage $parents;
4040

41+
private bool $disabled = false;
42+
4143
public function __construct(
4244
private readonly Profiler $profiler,
4345
private readonly RequestStack $requestStack,
@@ -66,7 +68,7 @@ public function initialize(ConsoleCommandEvent $event): void
6668

6769
$input = $event->getInput();
6870
if (!$input->hasOption('profile') || !$input->getOption('profile')) {
69-
$this->profiler->disable();
71+
$this->disabled = true;
7072

7173
return;
7274
}
@@ -92,7 +94,12 @@ public function catch(ConsoleErrorEvent $event): void
9294

9395
public function profile(ConsoleTerminateEvent $event): void
9496
{
95-
if (!$this->cliMode || !$this->profiler->isEnabled()) {
97+
$error = $this->error;
98+
$this->error = null;
99+
100+
if (!$this->cliMode || $this->disabled) {
101+
$this->disabled = false;
102+
96103
return;
97104
}
98105

@@ -114,8 +121,7 @@ public function profile(ConsoleTerminateEvent $event): void
114121
$request->command->exitCode = $event->getExitCode();
115122
$request->command->interruptedBySignal = $event->getInterruptingSignal();
116123

117-
$profile = $this->profiler->collect($request, $request->getResponse(), $this->error);
118-
$this->error = null;
124+
$profile = $this->profiler->collect($request, $request->getResponse(), $error);
119125
$this->profiles[$request] = $profile;
120126

121127
if ($this->parents[$request] = $this->requestStack->getParentRequest()) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@
4040

4141
->set('console_profiler_listener', ConsoleProfilerListener::class)
4242
->args([
43-
service('profiler'),
43+
service('.lazy_profiler'),
4444
service('.virtual_request_stack'),
4545
service('debug.stopwatch'),
4646
param('kernel.runtime_mode.cli'),
4747
service('router')->nullOnInvalid(),
4848
])
4949
->tag('kernel.event_subscriber')
5050

51+
->set('.lazy_profiler', Profiler::class)
52+
->factory('current')
53+
->args([[service('profiler')]])
54+
->lazy()
55+
5156
->set('.virtual_request_stack', VirtualRequestStack::class)
5257
->args([service('request_stack')])
5358
->public()

0 commit comments

Comments
 (0)