Skip to content

Commit 172e24c

Browse files
feature symfony#61860 [Config][DependencyInjection][Routing] Deprecate using $this or the internal scope of the loader from PHP config files (nicolas-grekas)
This PR was merged into the 7.4 branch. Discussion ---------- [Config][DependencyInjection][Routing] Deprecate using `$this` or the internal scope of the loader from PHP config files | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no | Deprecations? | yes | Issues | - | License | MIT The target of this change is being able to patch the PHP-DSL examples in the documentation without any downsides: ```diff -return static function (FrameworkConfig $framework) { +return function (FrameworkConfig $framework) { ``` Commits ------- 9afb4f4 [DependencyInjection][Config][Routing] Deprecate using `$this` or the internal scope of the loader from PHP config files
2 parents 145fa98 + 9afb4f4 commit 172e24c

File tree

18 files changed

+114
-13
lines changed

18 files changed

+114
-13
lines changed

UPGRADE-7.4.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Cache
1818

1919
* Bump ext-redis to 6.2 and ext-relay to 0.11 minimum
2020

21+
Config
22+
------
23+
24+
* Deprecate accessing the internal scope of the loader in PHP config files, use only its public API instead
25+
2126
Console
2227
-------
2328

@@ -29,6 +34,7 @@ DependencyInjection
2934
* Add argument `$target` to `ContainerBuilder::registerAliasForArgument()`
3035
* Add argument `$throwOnAbstract` to `ContainerBuilder::findTaggedResourceIds()`
3136
* Deprecate registering a service without a class when its id is a non-existing FQCN
37+
* Deprecate using `$this` or its internal scope from PHP config files; use the `$loader` variable instead
3238

3339
DoctrineBridge
3440
--------------
@@ -87,6 +93,7 @@ Routing
8793

8894
* Deprecate class aliases in the `Annotation` namespace, use attributes instead
8995
* Deprecate getters and setters in attribute classes in favor of public properties
96+
* Deprecate accessing the internal scope of the loader in PHP config files, use only its public API instead
9097

9198
Security
9299
--------

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/argon2i_hasher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$this->load('container1.php');
3+
$loader->load('container1.php');
44

55
$container->loadFromExtension('security', [
66
'password_hashers' => [

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/bcrypt_hasher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$this->load('container1.php');
3+
$loader->load('container1.php');
44

55
$container->loadFromExtension('security', [
66
'password_hashers' => [

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/merge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$this->load('merge_import.php');
3+
$loader->load('merge_import.php');
44

55
$container->loadFromExtension('security', [
66
'providers' => [

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/migrating_hasher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$this->load('container1.php');
3+
$loader->load('container1.php');
44

55
$container->loadFromExtension('security', [
66
'password_hashers' => [

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/sodium_hasher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$this->load('container1.php');
3+
$loader->load('container1.php');
44

55
$container->loadFromExtension('security', [
66
'password_hashers' => [

src/Symfony/Component/Config/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* Add support for `defaultNull()` on `ArrayNodeDefinition`
1010
* Add `ArrayNodeDefinition::acceptAndWrap()` to list alternative types that should be accepted and wrapped in an array
1111
* Add array-shapes to generated config builders
12+
* Deprecate accessing the internal scope of the loader in PHP config files, use only its public API instead
1213

1314
7.3
1415
---

src/Symfony/Component/Config/Definition/Loader/DefinitionFileLoader.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,19 @@ public function load(mixed $resource, ?string $type = null): mixed
4646
// the closure forbids access to the private scope in the included file
4747
$load = \Closure::bind(static function ($file) use ($loader) {
4848
return include $file;
49-
}, null, ProtectedDefinitionFileLoader::class);
49+
}, null, null);
5050

51-
$callback = $load($path);
51+
try {
52+
$callback = $load($path);
53+
} catch (\Error $e) {
54+
$load = \Closure::bind(static function ($file) use ($loader) {
55+
return include $file;
56+
}, null, ProtectedDefinitionFileLoader::class);
57+
58+
$callback = $load($path);
59+
60+
trigger_deprecation('symfony/config', '7.4', 'Accessing the internal scope of the loader in config files is deprecated, use only its public API instead in "%s" on line %d.', $e->getFile(), $e->getLine());
61+
}
5262

5363
if (\is_object($callback) && \is_callable($callback)) {
5464
$this->executeCallback($callback, new DefinitionConfigurator($this->treeBuilder, $this, $path, $resource), $path);

src/Symfony/Component/Config/Tests/Definition/Loader/DefinitionFileLoaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Config\Tests\Definition\Loader;
1313

14+
use PHPUnit\Framework\Attributes\Group;
15+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1416
use PHPUnit\Framework\TestCase;
1517
use Symfony\Component\Config\Definition\BaseNode;
1618
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -39,4 +41,15 @@ public function testLoad()
3941
$this->assertInstanceOf(BaseNode::class, $children['foo']);
4042
$this->assertSame('test.foo', $children['foo']->getPath(), '->load() loads a PHP file resource');
4143
}
44+
45+
#[IgnoreDeprecations]
46+
#[Group('legacy')]
47+
public function testTriggersDeprecationWhenAccessingLoaderInternalScope()
48+
{
49+
$loader = new DefinitionFileLoader(new TreeBuilder('test'), new FileLocator(__DIR__.'/../../Fixtures/Loader'));
50+
51+
$this->expectUserDeprecationMessageMatches('{^Since symfony/config 7.4: Accessing the internal scope of the loader in config files is deprecated, use only its public API instead in ".+" on line \d+\.$}');
52+
53+
$loader->load('legacy_internal_scope.php');
54+
}
4255
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// access the loader's internal scope to trigger deprecation
4+
$loader->resolver;

0 commit comments

Comments
 (0)