Skip to content

Commit 03816ec

Browse files
committed
Fix profiler service deprecation by using non-deprecated internal id
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 8bae088 commit 03816ec

3 files changed

Lines changed: 34 additions & 11 deletions

File tree

src/Codeception/Lib/Connector/Symfony.php

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

8888
private function getProfiler(): ?Profiler
8989
{
90-
if (!$this->container->has('profiler')) {
91-
return null;
92-
}
90+
// Prefer the non-deprecated internal id; accessing the public "profiler"
91+
// alias directly triggers a Symfony deprecation (framework-bundle 5.4+).
92+
foreach (['.container.private.profiler', 'profiler'] as $id) {
93+
if ($this->container->has($id)) {
94+
$profiler = $this->container->get($id);
9395

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

96-
return $profiler instanceof Profiler ? $profiler : null;
100+
return null;
97101
}
98102

99103
private function persistDoctrineConnections(): void

src/Codeception/Module/Symfony.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,19 @@ protected function getKernelClass(): string
357357
*/
358358
protected function getProfile(): ?Profile
359359
{
360-
/** @var Profiler|null $profiler */
361-
$profiler = $this->getService('profiler');
360+
$container = $this->_getContainer();
361+
362+
$profiler = null;
363+
// Prefer the non-deprecated internal id; accessing the public "profiler"
364+
// alias directly triggers a Symfony deprecation (framework-bundle 5.4+).
365+
foreach (['.container.private.profiler', 'profiler'] as $id) {
366+
if ($container->has($id)) {
367+
$profiler = $container->get($id);
368+
break;
369+
}
370+
}
362371

363-
if ($profiler === null) {
372+
if (!$profiler instanceof Profiler) {
364373
return null;
365374
}
366375

tests/Support/CodeceptTestCase.php

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

155155
$container = $this->_getContainer();
156-
if (!$container->has('profiler')) {
156+
157+
$profiler = null;
158+
// Prefer the non-deprecated internal id; accessing the public "profiler"
159+
// alias directly triggers a Symfony deprecation (framework-bundle 5.4+).
160+
foreach (['.container.private.profiler', 'profiler'] as $id) {
161+
if ($container->has($id)) {
162+
/** @var Profiler $profiler */
163+
$profiler = $container->get($id);
164+
break;
165+
}
166+
}
167+
168+
if (!$profiler instanceof Profiler) {
157169
return null;
158170
}
159171

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

164174
if ($profile instanceof Profile) {

0 commit comments

Comments
 (0)