Skip to content

Commit 7667365

Browse files
committed
[DependencyInjection] Introduce build parameters
1 parent 7b212a1 commit 7667365

29 files changed

+282
-1
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Allow #[When] to be extended
1212
* Change the signature of `ContainerAwareInterface::setContainer()` to `setContainer(?ContainerInterface)`
1313
* Deprecate calling `ContainerAwareTrait::setContainer()` without arguments
14+
* Parameters named with a starting dot are accessible during build-time only
1415

1516
6.1
1617
---

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,20 @@ public function getRemovedIds(): array
303303
return [];
304304
}
305305

306+
/**
307+
* Gets parameters that existed at compile time.
308+
*/
309+
public function getRemovedParameters(): array
310+
{
311+
return array_fill_keys(
312+
array_filter(
313+
array_keys($this->parameterBag->all()),
314+
fn ($key) => str_starts_with($key, '.')
315+
),
316+
true
317+
);
318+
}
319+
306320
/**
307321
* Camelizes a string.
308322
*/

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,15 @@ class %s extends {$options['class']}
261261
}
262262
$files['removed-ids.php'] = $c."];\n";
263263
}
264+
$params = $this->container->getRemovedParameters();
265+
if ($params = array_keys($params)) {
266+
sort($params);
267+
$c = "<?php\n\nreturn [\n";
268+
foreach ($params as $param) {
269+
$c .= ' '.$this->doExport($param)." => true,\n";
270+
}
271+
$files['removed-parameters.php'] = $c."];\n";
272+
}
264273

265274
if (!$this->inlineFactories) {
266275
foreach ($this->generateServiceFiles($services) as $file => [$c, $preload]) {
@@ -1275,6 +1284,7 @@ public function isCompiled(): bool
12751284
12761285
EOF;
12771286
$code .= $this->addRemovedIds();
1287+
$code .= $this->addRemovedParameters();
12781288

12791289
if ($this->asFiles && !$this->inlineFactories) {
12801290
$code .= <<<'EOF'
@@ -1372,6 +1382,35 @@ public function getRemovedIds(): array
13721382
return {$code};
13731383
}
13741384
1385+
EOF;
1386+
}
1387+
1388+
private function addRemovedParameters(): string
1389+
{
1390+
$params = $this->container->getRemovedParameters();
1391+
if (!$params) {
1392+
return '';
1393+
}
1394+
if ($this->asFiles) {
1395+
$code = "require \$this->containerDir.\\DIRECTORY_SEPARATOR.'removed-parameters.php'";
1396+
} else {
1397+
$code = '';
1398+
$params = array_keys($params);
1399+
sort($params);
1400+
foreach ($params as $param) {
1401+
$code .= ' '.$this->doExport($param)." => true,\n";
1402+
}
1403+
1404+
$code = "[\n{$code} ]";
1405+
}
1406+
1407+
return <<<EOF
1408+
1409+
public function getRemovedParameters(): array
1410+
{
1411+
return {$code};
1412+
}
1413+
13751414
EOF;
13761415
}
13771416

@@ -1517,6 +1556,10 @@ private function addDefaultParametersMethod(): string
15171556
$dynamicPhp = [];
15181557

15191558
foreach ($this->container->getParameterBag()->all() as $key => $value) {
1559+
if (str_starts_with($key, '.')) {
1560+
// do not dump build parameters
1561+
continue;
1562+
}
15201563
if ($key !== $resolvedKey = $this->container->resolveEnvPlaceholders($key)) {
15211564
throw new InvalidArgumentException(sprintf('Parameter name cannot use env parameters: "%s".', $resolvedKey));
15221565
}
@@ -1541,6 +1584,9 @@ public function getParameter(string $name): array|bool|string|int|float|\UnitEnu
15411584
}
15421585
15431586
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
1587+
if ('.' === $name[0] && isset($this->getRemovedParameters()[$name])) {
1588+
throw new \LogicException(sprintf('The parameter "%s" is only accessible at build-time and has been removed.', $name));
1589+
}
15441590
throw new ParameterNotFoundException($name);
15451591
}
15461592
if (isset($this->loadedDynamicParameters[$name])) {
@@ -1930,7 +1976,7 @@ private function dumpParameter(string $name): string
19301976
$value = $this->container->getParameter($name);
19311977
$dumpedValue = $this->dumpValue($value, false);
19321978

1933-
if (!$value || !\is_array($value)) {
1979+
if (!$value || !\is_array($value) || '.' === $name[0]) {
19341980
return $dumpedValue;
19351981
}
19361982

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ private function convertParameters(array $parameters, string $type, \DOMElement
265265
{
266266
$withKeys = !array_is_list($parameters);
267267
foreach ($parameters as $key => $value) {
268+
if (str_starts_with($key, '.')) {
269+
// do not dump build parameters
270+
continue;
271+
}
272+
268273
$element = $this->document->createElement($type);
269274
if ($withKeys) {
270275
$element->setAttribute($keyAttribute, $key);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ private function prepareParameters(array $parameters, bool $escape = true): arra
335335
{
336336
$filtered = [];
337337
foreach ($parameters as $key => $value) {
338+
if (str_starts_with($key, '.')) {
339+
// do not dump build parameters
340+
continue;
341+
}
342+
338343
if (\is_array($value)) {
339344
$value = $this->prepareParameters($value, $escape);
340345
} elseif ($value instanceof Reference || \is_string($value) && str_starts_with($value, '@')) {

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,15 @@ public function testEnvInId()
462462
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_env_in_id.php', $dumper->dump());
463463
}
464464

465+
public function testBuildParameters()
466+
{
467+
$container = include self::$fixturesPath.'/containers/container_with_build_parameters.php';
468+
$container->compile();
469+
$dumper = new PhpDumper($container);
470+
471+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_with_build_parameters.php', $dumper->dump());
472+
}
473+
465474
public function testEnvParameter()
466475
{
467476
$rand = mt_rand();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerBuilder;
4+
use Symfony\Component\DependencyInjection\Definition;
5+
6+
require_once __DIR__.'/../includes/classes.php';
7+
8+
$container = new ContainerBuilder();
9+
10+
$container->setParameter('foo', '%.build_bar%');
11+
$container->setParameter('bar', '%.build_baz%');
12+
$container->setParameter('.build_bar', 'bar');
13+
$container->setParameter('.build_baz', ['baz']);
14+
15+
$container->register('bar', 'BarClass')
16+
->setProperty('foo', '%.build_baz%')
17+
->setPublic(true)
18+
;
19+
20+
return $container;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ protected function getTestService()
5151
public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
5252
{
5353
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
54+
if ('.' === $name[0] && isset($this->getRemovedParameters()[$name])) {
55+
throw new \LogicException(sprintf('The parameter "%s" is only accessible at build-time and has been removed.', $name));
56+
}
5457
throw new ParameterNotFoundException($name);
5558
}
5659
if (isset($this->loadedDynamicParameters[$name])) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ protected function getTestService()
5151
public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
5252
{
5353
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
54+
if ('.' === $name[0] && isset($this->getRemovedParameters()[$name])) {
55+
throw new \LogicException(sprintf('The parameter "%s" is only accessible at build-time and has been removed.', $name));
56+
}
5457
throw new ParameterNotFoundException($name);
5558
}
5659
if (isset($this->loadedDynamicParameters[$name])) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ protected function getServiceWithMethodCallAndFactoryService()
6666
public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
6767
{
6868
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
69+
if ('.' === $name[0] && isset($this->getRemovedParameters()[$name])) {
70+
throw new \LogicException(sprintf('The parameter "%s" is only accessible at build-time and has been removed.', $name));
71+
}
6972
throw new ParameterNotFoundException($name);
7073
}
7174
if (isset($this->loadedDynamicParameters[$name])) {

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