Skip to content

Commit d3ee161

Browse files
HeahDudenicolas-grekas
authored andcommitted
-
1 parent c5b46b9 commit d3ee161

11 files changed

+54
-107
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ public function merge(self $container)
612612

613613
if ($parameterBag instanceof ParameterBag && $otherBag instanceof ParameterBag) {
614614
foreach ($otherBag->allDeprecated() as $name => $deprecated) {
615-
$parameterBag->deprecate($name, $deprecated['package'], $deprecated['version'], $deprecated['message']);
615+
$parameterBag->deprecate($name, ...$deprecated);
616616
}
617617
}
618618

@@ -696,11 +696,9 @@ public function prependExtensionConfig(string $name, array $config)
696696
/**
697697
* Deprecates a service container parameter.
698698
*
699-
* @param string|null $message Default to 'The parameter "%s" is deprecated.'
700-
*
701699
* @throws ParameterNotFoundException if the parameter is not defined
702700
*/
703-
public function deprecateParameter(string $name, string $package, string $version, string $message = null): void
701+
public function deprecateParameter(string $name, string $package, string $version, string $message = 'The parameter "%s" is deprecated.'): void
704702
{
705703
if (!$this->parameterBag instanceof ParameterBag) {
706704
throw new BadMethodCallException(sprintf('The parameter bag must be an instance of "%s" to call "%s".', ParameterBag::class, __METHOD__));

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,21 +1233,19 @@ private function startClass(string $class, string $baseClass, bool $hasProxyClas
12331233
*/
12341234
class $class extends $baseClass
12351235
{
1236+
private const DEPRECATED_PARAMETERS = [];
1237+
12361238
protected \$parameters = [];
12371239
protected readonly \WeakReference \$ref;
1238-
private \$deprecatedParameters = [];
12391240
12401241
public function __construct()
12411242
{
12421243
\$this->ref = \WeakReference::create(\$this);
12431244
12441245
EOF;
1245-
$code = str_replace(" private \$deprecatedParameters = [];\n", $this->addDeprecatedParameters(), $code);
1246+
$code = str_replace(" private const DEPRECATED_PARAMETERS = [];\n\n", $this->addDeprecatedParameters(), $code);
12461247
if ($this->asFiles) {
1247-
$code = str_replace('$parameters = []', "\$containerDir;\n protected \$parameters = [];\n private \$buildParameters", $code);
1248-
$code = str_replace('__construct()', '__construct(array $buildParameters = [], $containerDir = __DIR__)', $code);
1249-
$code .= " \$this->buildParameters = \$buildParameters;\n";
1250-
$code .= " \$this->containerDir = \$containerDir;\n";
1248+
$code = str_replace('__construct()', '__construct(private array $buildParameters = [], protected string $containerDir = __DIR__)', $code);
12511249

12521250
if (null !== $this->targetDirRegex) {
12531251
$code = str_replace('$parameters = []', "\$targetDir;\n protected \$parameters = []", $code);
@@ -1404,16 +1402,10 @@ private function addDeprecatedParameters(): string
14041402
$code = '';
14051403
ksort($deprecated);
14061404
foreach ($deprecated as $param => $deprecation) {
1407-
$deprecationArgs = '';
1408-
foreach ($deprecation as $k => $arg) {
1409-
$deprecationArgs .= " '$k' => '$arg',\n";
1410-
}
1411-
$code .= ' '.$this->doExport($param)." => [\n".$deprecationArgs." ],\n";
1405+
$code .= ' '.$this->doExport($param).' => ['.implode(', ', array_map($this->doExport(...), $deprecation))."],\n";
14121406
}
14131407

1414-
$code = "[\n{$code} ]";
1415-
1416-
return " private \$deprecatedParameters = {$code};\n";
1408+
return " private const DEPRECATED_PARAMETERS = [\n{$code} ];\n\n";
14171409
}
14181410

14191411
private function addMethodMap(): string
@@ -1577,15 +1569,17 @@ private function addDefaultParametersMethod(): string
15771569
15781570
public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
15791571
{
1572+
if (isset(self::DEPRECATED_PARAMETERS[$name])) {
1573+
trigger_deprecation(...self::DEPRECATED_PARAMETERS[$name]);
1574+
}
1575+
15801576
if (isset($this->buildParameters[$name])) {
1581-
__trigger_deprecation__
15821577
return $this->buildParameters[$name];
15831578
}
15841579
15851580
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
15861581
throw new ParameterNotFoundException($name);
15871582
}
1588-
__trigger_deprecation__
15891583
if (isset($this->loadedDynamicParameters[$name])) {
15901584
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
15911585
}
@@ -1617,7 +1611,7 @@ public function getParameterBag(): ParameterBagInterface
16171611
foreach ($this->buildParameters as $name => $value) {
16181612
$parameters[$name] = $value;
16191613
}
1620-
$this->parameterBag = new FrozenParameterBag($parameters__deprecated_parameters__);
1614+
$this->parameterBag = new FrozenParameterBag($parameters, self::DEPRECATED_PARAMETERS);
16211615
}
16221616
16231617
return $this->parameterBag;
@@ -1626,19 +1620,12 @@ public function getParameterBag(): ParameterBagInterface
16261620
EOF;
16271621

16281622
if (!$this->asFiles) {
1629-
$code = preg_replace('/^.*buildParameters.*\n(\s*__trigger_deprecation__\n)?.*\n.*\n\n?/m', '', $code);
1630-
}
1631-
if (($bag = $this->container->getParameterBag()) instanceof ParameterBag && $bag->allDeprecated()) {
1632-
$code = preg_replace('/( +)__trigger_deprecation__/m', <<<'EOF'
1633-
$1if (isset($this->deprecatedParameters[$name])) {
1634-
$1 $deprecated = $this->deprecatedParameters[$name];
1635-
$1 trigger_deprecation($deprecated['package'], $deprecated['version'], $deprecated['message'], $name);
1636-
$1}
1637-
EOF, $code);
1638-
$code = str_replace('__deprecated_parameters__', ', $this->deprecatedParameters', $code);
1639-
} else {
1640-
$code = preg_replace("/ +__trigger_deprecation__\n/m", '', $code);
1641-
$code = str_replace('__deprecated_parameters__', '', $code);
1623+
$code = preg_replace('/^.*buildParameters.*\n.*\n.*\n\n?/m', '', $code);
1624+
}
1625+
1626+
if (!($bag = $this->container->getParameterBag()) instanceof ParameterBag || !$bag->allDeprecated()) {
1627+
$code = preg_replace("/\n.*DEPRECATED_PARAMETERS.*\n.*\n.*\n/m", '', $code, 1);
1628+
$code = str_replace(', self::DEPRECATED_PARAMETERS', '', $code);
16421629
}
16431630

16441631
if ($dynamicPhp) {

src/Symfony/Component/DependencyInjection/ParameterBag/FrozenParameterBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function set(string $name, array|bool|string|int|float|\UnitEnum|null $va
4949
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
5050
}
5151

52-
public function deprecate(string $name, string $package, string $version, string $message = null)
52+
public function deprecate(string $name, string $package, string $version, string $message = 'The parameter "%s" is deprecated.')
5353
{
5454
throw new LogicException('Impossible to call deprecate() on a frozen ParameterBag.');
5555
}

src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ public function get(string $name): array|bool|string|int|float|\UnitEnum|null
8888
}
8989

9090
if (isset($this->deprecatedParameters[$name])) {
91-
$deprecated = $this->deprecatedParameters[$name];
92-
93-
trigger_deprecation($deprecated['package'], $deprecated['version'], $deprecated['message'], $name);
91+
trigger_deprecation(...$this->deprecatedParameters[$name]);
9492
}
9593

9694
return $this->parameters[$name];
@@ -110,21 +108,15 @@ public function set(string $name, array|bool|string|int|float|\UnitEnum|null $va
110108
/**
111109
* Deprecates a service container parameter.
112110
*
113-
* @param string|null $message Default to 'The parameter "%s" is deprecated.'
114-
*
115111
* @throws ParameterNotFoundException if the parameter is not defined
116112
*/
117-
public function deprecate(string $name, string $package, string $version, string $message = null)
113+
public function deprecate(string $name, string $package, string $version, string $message = 'The parameter "%s" is deprecated.')
118114
{
119115
if (!\array_key_exists($name, $this->parameters)) {
120116
throw new ParameterNotFoundException($name);
121117
}
122118

123-
$this->deprecatedParameters[$name] = [
124-
'package' => $package,
125-
'version' => $version,
126-
'message' => $message ?? 'The parameter "%s" is deprecated.',
127-
];
119+
$this->deprecatedParameters[$name] = [$package, $version, $message, $name];
128120
}
129121

130122
public function has(string $name): bool

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10_as_files.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,13 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
5959
*/
6060
class ProjectServiceContainer extends Container
6161
{
62-
protected $containerDir;
6362
protected $targetDir;
6463
protected $parameters = [];
65-
private $buildParameters;
6664
protected readonly \WeakReference $ref;
6765

68-
public function __construct(array $buildParameters = [], $containerDir = __DIR__)
66+
public function __construct(private array $buildParameters = [], protected string $containerDir = __DIR__)
6967
{
7068
$this->ref = \WeakReference::create($this);
71-
$this->buildParameters = $buildParameters;
72-
$this->containerDir = $containerDir;
7369
$this->targetDir = \dirname($containerDir);
7470
$this->services = $this->privates = [];
7571
$this->methodMap = [

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,17 +563,13 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
563563
*/
564564
class ProjectServiceContainer extends Container
565565
{
566-
protected $containerDir;
567566
protected $targetDir;
568567
protected $parameters = [];
569-
private $buildParameters;
570568
protected readonly \WeakReference $ref;
571569

572-
public function __construct(array $buildParameters = [], $containerDir = __DIR__)
570+
public function __construct(private array $buildParameters = [], protected string $containerDir = __DIR__)
573571
{
574572
$this->ref = \WeakReference::create($this);
575-
$this->buildParameters = $buildParameters;
576-
$this->containerDir = $containerDir;
577573
$this->targetDir = \dirname($containerDir);
578574
$this->parameters = $this->getDefaultParameters();
579575

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,13 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
3636
*/
3737
class ProjectServiceContainer extends Container
3838
{
39-
protected $containerDir;
4039
protected $targetDir;
4140
protected $parameters = [];
42-
private $buildParameters;
4341
protected readonly \WeakReference $ref;
4442

45-
public function __construct(array $buildParameters = [], $containerDir = __DIR__)
43+
public function __construct(private array $buildParameters = [], protected string $containerDir = __DIR__)
4644
{
4745
$this->ref = \WeakReference::create($this);
48-
$this->buildParameters = $buildParameters;
49-
$this->containerDir = $containerDir;
5046
$this->targetDir = \dirname($containerDir);
5147
$this->parameters = $this->getDefaultParameters();
5248

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_lazy_inlined_factories.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,13 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
3131
*/
3232
class ProjectServiceContainer extends Container
3333
{
34-
protected $containerDir;
3534
protected $targetDir;
3635
protected $parameters = [];
37-
private $buildParameters;
3836
protected readonly \WeakReference $ref;
3937

40-
public function __construct(array $buildParameters = [], $containerDir = __DIR__)
38+
public function __construct(private array $buildParameters = [], protected string $containerDir = __DIR__)
4139
{
4240
$this->ref = \WeakReference::create($this);
43-
$this->buildParameters = $buildParameters;
44-
$this->containerDir = $containerDir;
4541
$this->targetDir = \dirname($containerDir);
4642
$this->parameters = $this->getDefaultParameters();
4743

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_deprecated_parameters.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414
*/
1515
class ProjectServiceContainer extends Container
1616
{
17-
protected $parameters = [];
18-
private $deprecatedParameters = [
19-
'foo_class' => [
20-
'package' => 'symfony/test',
21-
'version' => '6.3',
22-
'message' => 'The parameter "%s" is deprecated.',
23-
],
17+
private const DEPRECATED_PARAMETERS = [
18+
'foo_class' => ['symfony/test', '6.3', 'The parameter "%s" is deprecated.', 'foo_class'],
2419
];
2520

21+
protected $parameters = [];
22+
protected readonly \WeakReference $ref;
23+
2624
public function __construct()
2725
{
26+
$this->ref = \WeakReference::create($this);
2827
$this->parameters = $this->getDefaultParameters();
2928

3029
$this->services = $this->privates = [];
@@ -50,20 +49,20 @@ public function isCompiled(): bool
5049
*
5150
* @return \FooClass\Foo
5251
*/
53-
protected function getFooService()
52+
protected static function getFooService($container)
5453
{
55-
return $this->services['foo'] = new \FooClass\Foo();
54+
return $container->services['foo'] = new \FooClass\Foo();
5655
}
5756

5857
public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
5958
{
59+
if (isset(self::DEPRECATED_PARAMETERS[$name])) {
60+
trigger_deprecation(...self::DEPRECATED_PARAMETERS[$name]);
61+
}
62+
6063
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
6164
throw new ParameterNotFoundException($name);
6265
}
63-
if (isset($this->deprecatedParameters[$name])) {
64-
$deprecated = $this->deprecatedParameters[$name];
65-
trigger_deprecation($deprecated['package'], $deprecated['version'], $deprecated['message'], $name);
66-
}
6766
if (isset($this->loadedDynamicParameters[$name])) {
6867
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
6968
}
@@ -88,7 +87,7 @@ public function getParameterBag(): ParameterBagInterface
8887
foreach ($this->loadedDynamicParameters as $name => $loaded) {
8988
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
9089
}
91-
$this->parameterBag = new FrozenParameterBag($parameters, $this->deprecatedParameters);
90+
$this->parameterBag = new FrozenParameterBag($parameters, self::DEPRECATED_PARAMETERS);
9291
}
9392

9493
return $this->parameterBag;

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_deprecated_parameters_as_files.txt

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class getFooService extends ProjectServiceContainer
2525

2626
[Container%s/ProjectServiceContainer.php] => <?php
2727

28-
namespace ContainerHT0kpGM;
28+
namespace Container%s;
2929

3030
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
3131
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -41,22 +41,17 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
4141
*/
4242
class ProjectServiceContainer extends Container
4343
{
44-
protected $containerDir;
44+
private const DEPRECATED_PARAMETERS = [
45+
'foo_class' => ['symfony/test', '6.3', 'The parameter "%s" is deprecated.', 'foo_class'],
46+
];
47+
4548
protected $targetDir;
4649
protected $parameters = [];
47-
private $buildParameters;
48-
private $deprecatedParameters = [
49-
'foo_class' => [
50-
'package' => 'symfony/test',
51-
'version' => '6.3',
52-
'message' => 'The parameter "%s" is deprecated.',
53-
],
54-
];
50+
protected readonly \WeakReference $ref;
5551

56-
public function __construct(array $buildParameters = [], $containerDir = __DIR__)
52+
public function __construct(private array $buildParameters = [], protected string $containerDir = __DIR__)
5753
{
58-
$this->buildParameters = $buildParameters;
59-
$this->containerDir = $containerDir;
54+
$this->ref = \WeakReference::create($this);
6055
$this->targetDir = \dirname($containerDir);
6156
$this->parameters = $this->getDefaultParameters();
6257

@@ -97,21 +92,17 @@ class ProjectServiceContainer extends Container
9792

9893
public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
9994
{
95+
if (isset(self::DEPRECATED_PARAMETERS[$name])) {
96+
trigger_deprecation(...self::DEPRECATED_PARAMETERS[$name]);
97+
}
98+
10099
if (isset($this->buildParameters[$name])) {
101-
if (isset($this->deprecatedParameters[$name])) {
102-
$deprecated = $this->deprecatedParameters[$name];
103-
trigger_deprecation($deprecated['package'], $deprecated['version'], $deprecated['message'], $name);
104-
}
105100
return $this->buildParameters[$name];
106101
}
107102

108103
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
109104
throw new ParameterNotFoundException($name);
110105
}
111-
if (isset($this->deprecatedParameters[$name])) {
112-
$deprecated = $this->deprecatedParameters[$name];
113-
trigger_deprecation($deprecated['package'], $deprecated['version'], $deprecated['message'], $name);
114-
}
115106
if (isset($this->loadedDynamicParameters[$name])) {
116107
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
117108
}
@@ -143,7 +134,7 @@ class ProjectServiceContainer extends Container
143134
foreach ($this->buildParameters as $name => $value) {
144135
$parameters[$name] = $value;
145136
}
146-
$this->parameterBag = new FrozenParameterBag($parameters, $this->deprecatedParameters);
137+
$this->parameterBag = new FrozenParameterBag($parameters, self::DEPRECATED_PARAMETERS);
147138
}
148139

149140
return $this->parameterBag;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy