Skip to content

Commit 0e8ae8f

Browse files
bug #62646 [DependencyInjection] Throw when using $this or its internal scope from PHP config files (nicolas-grekas)
This PR was merged into the 7.4 branch. Discussion ---------- [DependencyInjection] Throw when using `$this` or its internal scope from PHP config files | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | #62547 | License | MIT The BC layer brought by #61860 doesn't work: as described in #62547, the first call to load() has side effects which can break when the second and fallback call is made. I don't see any other way. Well, one would be to operate on a clone, and if that works, rerun with the real container builder. But the overhead is going to be high for everybody. I'm therefor proposing to change this as a BC BREAK. While less than ideal, this happens at compile time, and the fix is trivial and explained in the error message. Commits ------- 7ffcd64 [DependencyInjection] Throw when using `$this` or its internal scope from PHP config files; use the `$loader` variable instead
2 parents e55bef0 + 7ffcd64 commit 0e8ae8f

File tree

4 files changed

+5
-13
lines changed

4 files changed

+5
-13
lines changed

UPGRADE-7.4.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ Console
3333
DependencyInjection
3434
-------------------
3535

36+
* [BC BREAK] Throw when using `$this` or its internal scope from PHP config files; use the `$loader` variable instead
3637
* Add argument `$target` to `ContainerBuilder::registerAliasForArgument()`
3738
* Add argument `$throwOnAbstract` to `ContainerBuilder::findTaggedResourceIds()`
3839
* Deprecate registering a service without a class when its id is a non-existing FQCN
39-
* Deprecate using `$this` or its internal scope from PHP config files; use the `$loader` variable instead
4040
* Deprecate XML configuration format, use YAML or PHP instead
4141
* Deprecate `ExtensionInterface::getXsdValidationBasePath()` and `getNamespace()`;
4242
bundles that need to support older versions of Symfony can keep the methods

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.4
55
---
66

7+
* [BC BREAK] Throw when using `$this` or its internal scope from PHP config files; use the `$loader` variable instead
78
* Allow adding resource tags using any config format
89
* Allow `#[AsAlias]` to be extended
910
* Parse attributes found on abstract classes for resource definitions
@@ -12,7 +13,6 @@ CHANGELOG
1213
* Allow multiple `#[AsDecorator]` attributes
1314
* Handle declaring services using PHP arrays that follow the same shape as corresponding yaml files
1415
* Add `AppReference` to help writing PHP configs using yaml-like array-shapes
15-
* Deprecate using `$this` or its internal scope from PHP config files; use the `$loader` variable instead
1616
* Deprecate XML configuration format, use YAML or PHP instead
1717
* Deprecate `ExtensionInterface::getXsdValidationBasePath()` and `getNamespace()`
1818
* Deprecate the fluent PHP format for semantic configuration, use `$container->extension()` or return an array instead

src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class_alias(AppReference::class, App::class);
9898
$result = null;
9999
}
100100

101-
trigger_deprecation('symfony/dependency-injection', '7.4', 'Using `$this` or its internal scope in config files is deprecated, use the `$loader` variable instead in "%s" on line %d.', $e->getFile(), $e->getLine());
101+
throw new LogicException(\sprintf('Using `$this` or its internal scope in config files is not supported anymore, use the `$loader` variable instead in "%s" on line %d.', $e->getFile(), $e->getLine()), $e->getCode(), $e);
102102
}
103103

104104
if (\is_object($result) && \is_callable($result)) {
@@ -301,10 +301,3 @@ private function configBuilder(string $namespace): ConfigBuilderInterface
301301
return $loader();
302302
}
303303
}
304-
305-
/**
306-
* @internal
307-
*/
308-
final class ProtectedPhpFileLoader extends PhpFileLoader
309-
{
310-
}

src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,13 @@ public function testNamedClosure()
331331
$this->assertStringEqualsFile(\dirname(__DIR__).'/Fixtures/php/named_closure_compiled.php', $dumper->dump());
332332
}
333333

334-
#[IgnoreDeprecations]
335-
#[Group('legacy')]
336334
public function testTriggersDeprecationWhenAccessingLoaderInternalScope()
337335
{
338336
$fixtures = realpath(__DIR__.'/../Fixtures');
339337
$loader = new PhpFileLoader(new ContainerBuilder(), new FileLocator($fixtures.'/config'));
340338

341-
$this->expectUserDeprecationMessageMatches('{^Since symfony/dependency-injection 7.4: Using \`\$this\` or its internal scope in config files is deprecated, use the \`\$loader\` variable instead in ".+" on line \d+\.$}');
339+
$this->expectException(LogicException::class);
340+
$this->expectExceptionMessageMatches('/^Using \`\$this\` or its internal scope in config files is not supported anymore, use the \`\$loader\` variable instead in ".+" on line \d+\.$/');
342341

343342
$loader->load('legacy_internal_scope.php');
344343
}

0 commit comments

Comments
 (0)