Skip to content

Commit a798e89

Browse files
authored
Fix profiler service deprecation by using non-deprecated internal id (#237)
Accessing the "profiler" service directly from the container triggers a deprecation since symfony/framework-bundle 5.4: "Accessing the "profiler" service directly from the container is deprecated, use dependency injection instead." The public "profiler" id is a deprecated alias that calls trigger_deprecation() before delegating to the real, non-deprecated private definition ".container.private.profiler". Prefer the internal id and fall back to "profiler" for older/edge configs where the alias does not exist. This avoids the deprecation on every container path (raw or test container) across Symfony 5.4 to 8.0. Closes #212
1 parent 6cd2150 commit a798e89

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/Codeception/Lib/Connector/Symfony.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ private function resolveContainer(): ContainerInterface
8787

8888
private function getProfiler(): ?Profiler
8989
{
90-
if (!$this->container->has('profiler')) {
91-
return null;
92-
}
90+
foreach (['.container.private.profiler', 'profiler'] as $id) {
91+
if ($this->container->has($id)) {
92+
$profiler = $this->container->get($id);
9393

94-
$profiler = $this->container->get('profiler');
94+
return $profiler instanceof Profiler ? $profiler : null;
95+
}
96+
}
9597

96-
return $profiler instanceof Profiler ? $profiler : null;
98+
return null;
9799
}
98100

99101
private function persistDoctrineConnections(): void

src/Codeception/Module/Symfony.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,7 @@ protected function getKernelClass(): string
364364
*/
365365
protected function getProfile(): ?Profile
366366
{
367-
/** @var Profiler|null $profiler */
368-
$profiler = $this->getService('profiler');
367+
$profiler = $this->grabCachedService(Profiler::class, ['.container.private.profiler', 'profiler']);
369368

370369
if ($profiler === null) {
371370
return null;

tests/Support/CodeceptTestCase.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,20 @@ protected function getProfile(): ?Profile
153153
}
154154

155155
$container = $this->_getContainer();
156-
if (!$container->has('profiler')) {
156+
157+
$profiler = null;
158+
foreach (['.container.private.profiler', 'profiler'] as $id) {
159+
if ($container->has($id)) {
160+
/** @var Profiler $profiler */
161+
$profiler = $container->get($id);
162+
break;
163+
}
164+
}
165+
166+
if (!$profiler instanceof Profiler) {
157167
return null;
158168
}
159169

160-
/** @var Profiler $profiler */
161-
$profiler = $container->get('profiler');
162170
$profile = $profiler->collect($request, $response);
163171

164172
if ($profile instanceof Profile) {

0 commit comments

Comments
 (0)