8000 [DependencyInjection][HttpKernel] Introduce build parameters by HeahDude · Pull Request #47680 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection][HttpKernel] Introduce build parameters #47680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions UPGRADE-6.3.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
UPGRADE FROM 6.2 to 6.3
=======================

DependencyInjection
-------------------

* Deprecate `PhpDumper` options `inline_factories_parameter` and `inline_class_loader_parameter`, use `inline_factories` and `inline_class_loader` instead

HttpKernel
----------

* Deprecate parameters `container.dumper.inline_factories` and `container.dumper.inline_class_loader`, use `.container.dumper.inline_factories` and `.container.dumper.inline_class_loader` instead

SecurityBundle
--------------

Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
CHANGELOG
=========

6.3
---

* Add options `inline_factories` and `inline_class_loader` to `PhpDumper::dump()`
* Deprecate `PhpDumper` options `inline_factories_parameter` and `inline_class_loader_parameter`
* Add `RemoveBuildParametersPass`, which removes parameters starting with a dot during compilation

6.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@ public function __construct()
new DefinitionErrorExceptionPass(),
]];

$this->afterRemovingPasses = [[
new ResolveHotPathPass(),
new ResolveNoPreloadPass(),
new AliasDeprecatedPublicServicesPass(),
]];
$this->afterRemovingPasses = [
0 => [
new ResolveHotPathPass(),
new ResolveNoPreloadPass(),
new AliasDeprecatedPublicServicesPass(),
],
// Let build parameters be available as late as possible
-2048 => [new RemoveBuildParametersPass()],
];
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;

class RemoveBuildParametersPass implements CompilerPassInterface
{
/**
* @var array<string, mixed>
*/
private array $removedParameters = [];

public function process(ContainerBuilder $container)
{
$parameterBag = $container->getParameterBag();
$this->removedParameters = [];

foreach ($parameterBag->all() as $name => $value) {
if ('.' === ($name[0] ?? '')) {
$this->removedParameters[$name] = $value;

$parameterBag->remove($name);
$container->log($this, sprintf('Removing build parameter "%s".', $name));
}
}
}

/**
* @return array<string, mixed>
*/
public function getRemovedParameters(): array
{
return $this->removedParameters;
}
}
30 changes: 26 additions & 4 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ public function dump(array $options = []): string|array
'debug' => true,
'hot_path_tag' => 'container.hot_path',
'preload_tags' => ['container.preload', 'container.no_preload'],
'inline_factories_parameter' => 'container.dumper.inline_factories',
'inline_class_loader_parameter' => 'container.dumper.inline_class_loader',
'inline_factories_parameter' => 'container.dumper.inline_factories', // @deprecated since Symfony 6.3
'inline_class_loader_parameter' => 'container.dumper.inline_class_loader', // @deprecated since Symfony 6.3
'inline_factories' => null,
'inline_class_loader' => null,
'preload_classes' => [],
'service_locator_tag' => 'container.service_locator',
'build_time' => time(),
Expand All @@ -149,8 +151,28 @@ public function dump(array $options = []): string|array
$this->asFiles = $options['as_files'];
$this->hotPathTag = $options['hot_path_tag'];
$this->preloadTags = $options['preload_tags'];
$this->inlineFactories = $this->asFiles && $options['inline_factories_parameter'] && $this->container->hasParameter($options['inline_factories_parameter']) && $this->container->getParameter($options['inline_factories_parameter']);
$this->inlineRequires = $options['inline_class_loader_parameter'] && ($this->container->hasParameter($options['inline_class_loader_parameter']) ? $this->container->getParameter($options['inline_class_loader_parameter']) : $options['debug']);

$this->inlineFactories = false;
if (isset($options['inline_factories'])) {
$this->inlineFactories = $this->asFiles && $options['inline_factories'];
} elseif (!$options['inline_factories_parameter']) {
trigger_deprecation('symfony/dependency-injection', '6.3', 'Option "inline_factories_parameter" passed to "%s()" is deprecated, use option "inline_factories" instead.', __METHOD__);
} elseif ($this->container->hasParameter($options['inline_factories_parameter'])) {
trigger_deprecation('symfony/dependency-injection', '6.3', 'Option "inline_factories_parameter" passed to "%s()" is deprecated, use option "inline_factories" instead.', __METHOD__);
$this->inlineFactories = $this->asFiles && $this->container->getParameter($options['inline_factories_parameter']);
}

$this->inlineRequires = $options['debug'];
if (isset($options['inline_class_loader'])) {
$this->inlineRequires = $options['inline_class_loader'];
} elseif (!$options['inline_class_loader_parameter']) {
trigger_deprecation('symfony/dependency-injection', '6.3', 'Option "inline_class_loader_parameter" passed to "%s()" is deprecated, use option "inline_class_loader" instead.', __METHOD__);
$this->inlineRequires = false;
} elseif ($this->container->hasParameter($options['inline_class_loader_parameter'])) {
trigger_deprecation('symfony/dependency-injection', '6.3', 'Option "inline_class_loader_parameter" passed to "%s()" is deprecated, use option "inline_class_loader" instead.', __METHOD__);
$this->inlineRequires = $this->container->getParameter($options['inline_class_loader_parameter']);
}

$this->serviceLocatorTag = $options['service_locator_tag'];
$this->class = $options['class'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function updateRepr()
$this->message = sprintf('The service "%s" has a dependency on a non-existent parameter "%s".', $this->sourceId, $this->key);
} elseif (null !== $this->sourceKey) {
$this->message = sprintf('The parameter "%s" has a dependency on a non-existent parameter "%s".', $this->sourceKey, $this->key);
} elseif ('.' === ($this->key[0] ?? '')) {
$this->message = sprintf('Parameter "%s" not found. It was probably deleted during the compilation of the container.', $this->key);
} else {
$this->message = sprintf('You have requested a non-existent parameter "%s".', $this->key);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Compiler\RemoveBuildParametersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class RemoveBuildParametersPassTest extends TestCase
{
public function testBuildParametersShouldBeRemoved()
{
$builder = new ContainerBuilder();
$builder->setParameter('foo', 'Foo');
$builder->setParameter('.bar', 'Bar');

$pass = new RemoveBuildParametersPass();
$pass->process($builder);

$this->assertSame('Foo', $builder->getParameter('foo'), '"foo" parameter must be defined.');
$this->assertFalse($builder->hasParameter('.bar'), '".bar" parameter must be removed.');
$this->assertSame(['.bar' => 'Bar'], $pass->getRemovedParameters(), '".baz" parameter must be returned with its value.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function testDumpAsFiles()
->addError('No-no-no-no');
$container->compile();
$dumper = new PhpDumper($container);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'inline_factories_parameter' => false, 'inline_class_loader_parameter' => false]), true);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'inline_factories' => false, 'inline_class_loader' => false]), true);
if ('\\' === \DIRECTORY_SEPARATOR) {
$dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump);
}
Expand All @@ -241,7 +241,7 @@ public function testDumpAsFilesWithTypedReference()
->setPublic(true);
$container->compile();
$dumper = new PhpDumper($container);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'inline_factories_parameter' => false, 'inline_class_loader_parameter' => false]), true);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'inline_factories' => false, 'inline_class_loader' => false]), true);
if ('\\' === \DIRECTORY_SEPARATOR) {
$dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump);
}
Expand All @@ -252,8 +252,6 @@ public function testDumpAsFilesWithTypedReference()
public function testDumpAsFilesWithFactoriesInlined()
{
$container = include self::$fixturesPath.'/containers/container9.php';
$container->setParameter('container.dumper.inline_factories', true);
$container->setParameter('container.dumper.inline_class_loader', true);

$container->getDefinition('bar')->addTag('hot');
$container->register('non_shared_foo', \Bar\FooClass::class)
Expand All @@ -268,7 +266,7 @@ public function testDumpAsFilesWithFactoriesInlined()
$container->compile();

$dumper = new PhpDumper($container);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'build_time' => 1563381341]), true);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'build_time' => 1563381341, 'inline_factories' => true, 'inline_class_loader' => true]), true);

if ('\\' === \DIRECTORY_SEPARATOR) {
$dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump);
Expand All @@ -279,8 +277,6 @@ public function testDumpAsFilesWithFactoriesInlined()
public function testDumpAsFilesWithLazyFactoriesInlined()
{
$container = new ContainerBuilder();
$container->setParameter('container.dumper.inline_factories', true);
$container->setParameter('container.dumper.inline_class_loader', true);
$container->setParameter('lazy_foo_class', \Bar\FooClass::class);

$container->register('lazy_foo', '%lazy_foo_class%')
Expand All @@ -292,7 +288,7 @@ public function testDumpAsFilesWithLazyFactoriesInlined()
$container->compile();

$dumper = new PhpDumper($container);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'build_time' => 1563381341]), true);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'build_time' => 1563381341, 'inline_factories' => true, 'inline_class_loader' => true]), true);

if ('\\' === \DIRECTORY_SEPARATOR) {
$dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump);
Expand All @@ -310,7 +306,7 @@ public function testNonSharedLazyDumpAsFiles()
->setLazy(true);
$container->compile();
$dumper = new PhpDumper($container);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'inline_factories_parameter' => false, 'inline_class_loader_parameter' => false]), true);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'inline_factories' => false, 'inline_class_loader' => false]), true);

if ('\\' === \DIRECTORY_SEPARATOR) {
$dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump);
Expand Down Expand Up @@ -473,7 +469,7 @@ public function testEnvParameter()
$container->compile();
$dumper = new PhpDumper($container);

$this->assertStringEqualsFile(self::$fixturesPath.'/php/services26.php', $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_EnvParameters', 'file' => self::$fixturesPath.'/php/services26.php', 'inline_factories_parameter' => false, 'inline_class_loader_parameter' => false]));
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services26.php', $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_EnvParameters', 'file' => self::$fixturesPath.'/php/services26.php', 'inline_factories' => false, 'inline_class_loader' => false]));

require self::$fixturesPath.'/php/services26.php';
$container = new \Symfony_DI_PhpDumper_Test_EnvParameters();
Expand Down Expand Up @@ -994,7 +990,7 @@ public function testArrayParameters()

$dumper = new PhpDumper($container);

$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_array_params.php', str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dumper->dump(['file' => self::$fixturesPath.'/php/services_array_params.php', 'inline_factories_parameter' => false, 'inline_class_loader_parameter' => false])));
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_array_params.php', str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dumper->dump(['file' => self::$fixturesPath.'/php/services_array_params.php', 'inline_factories' => false, 'inline_class_loader' => false])));
}

public function testExpressionReferencingPrivateService()
Expand Down Expand Up @@ -1162,11 +1158,10 @@ public function testInlineSelfRef()
public function testHotPathOptimizations()
{
$container = include self::$fixturesPath.'/containers/container_inline_requires.php';
$container->setParameter('inline_requires', true);
$container->compile();
$dumper = new PhpDumper($container);

$dump = $dumper->dump(['hot_path_tag' => 'container.hot_path', 'inline_class_loader_parameter' => 'inline_requires', 'file' => self::$fixturesPath.'/php/services_inline_requires.php']);
$dump = $dumper->dump(['hot_path_tag' => 'container.hot_path', 'inline_class_loader' => true, 'file' => self::$fixturesPath.'/php/services_inline_requires.php']);
if ('\\' === \DIRECTORY_SEPARATOR) {
$dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump);
}
Expand Down Expand Up @@ -1199,13 +1194,12 @@ public function testDumpHandlesObjectClassNames()
new Reference('foo'),
]))->setPublic(true);

$container->setParameter('inline_requires', true);
$container->compile();

$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump([
'class' => 'Symfony_DI_PhpDumper_Test_Object_Class_Name',
'inline_class_loader_parameter' => 'inline_requires',
'inline_class_loader' => true,
]));

$container = new \Symfony_DI_PhpDumper_Test_Object_Class_Name();
Expand Down Expand Up @@ -1281,7 +1275,6 @@ public function testUninitializedSyntheticReference()
$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump([
'class' => 'Symfony_DI_PhpDumper_Test_UninitializedSyntheticReference',
'inline_class_loader_parameter' => 'inline_requires',
]));

$container = new \Symfony_DI_PhpDumper_Test_UninitializedSyntheticReference();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,6 @@ class ProjectServiceContainer extends Container
'baz_class' => 'BazClass',
'foo_class' => 'Bar\\FooClass',
'foo' => 'bar',
'container.dumper.inline_factories' => true,
'container.dumper.inline_class_loader' => true,
];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ class ProjectServiceContainer extends Container
protected function getDefaultParameters(): array
{
return [
'container.dumper.inline_factories' => true,
'container.dumper.inline_class_loader' => true,
'lazy_foo_class' => 'Bar\\FooClass',
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class ProjectServiceContainer extends Container

public function __construct()
{
$this->parameters = $this->getDefaultParameters();

$this->services = $this->privates = [];
$this->methodMap = [
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\ParentNotExists' => 'getParentNotExistsService',
Expand Down Expand Up @@ -86,54 +84,4 @@ protected function getC2Service()

return $this->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\includes\\HotPath\\C2'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C2(new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3());
}

public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
{
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
throw new ParameterNotFoundException($name);
}
if (isset($this->loadedDynamicParameters[$name])) {
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
}

return $this->parameters[$name];
}

public function hasParameter(string $name): bool
{
return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters);
}

public function setParameter(string $name, $value): void
{
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
}

public function getParameterBag(): ParameterBagInterface
{
if (null === $this->parameterBag) {
$parameters = $this->parameters;
foreach ($this->loadedDynamicParameters as $name => $loaded) {
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
}
$this->parameterBag = new FrozenParameterBag($parameters);
}

return $this->parameterBag;
}

private $loadedDynamicParameters = [];
private $dynamicParameters = [];

private function getDynamicParameter(string $name)
{
throw new ParameterNotFoundException($name);
}

protected function getDefaultParameters(): array
{
return [
'inline_requires' => true,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function provideGetThrowParameterNotFoundExceptionData()
['', 'You have requested a non-existent parameter "".'],

['fiz.bar.boo', 'You have requested a non-existent parameter "fiz.bar.boo". You cannot access nested array items, do you want to inject "fiz" instead?'],
['.foo', 'Parameter ".foo" not found. It was probably deleted during the compilation of the container. Did you mean this: "foo"?'],
];
}

Expand Down
Loading
0