8000 feature #20634 [DI] Deprecate dumping an uncompiled container (ro0NL) · symfony/symfony@ed5b1d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit ed5b1d8

Browse files
committed
feature #20634 [DI] Deprecate dumping an uncompiled container (ro0NL)
This PR was squashed before being merged into the 3.3-dev branch (closes #20634). Discussion ---------- [DI] Deprecate dumping an uncompiled container | Q | A | ------------- | --- | Branch? | "master" | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #19673 (comment) | License | MIT | Doc PR | reference to the documentation PR, if any It makes the PHP dumper less complex. Compiled container goes in, compiled container goes out. Relates to #19673 Commits ------- da50fdb [DI] Deprecate dumping an uncompiled container
2 parents d221a4e + da50fdb commit ed5b1d8

File tree

7 files changed

+201
-18
lines changed

7 files changed

+201
-18
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class PhpDumper extends Dumper
7272
*/
7373
public function __construct(ContainerBuilder $container)
7474
{
75+
if (!$container->isFrozen()) {
76+
@trigger_error('Dumping an uncompiled ContainerBuilder is deprecated since version 3.3 and will not be supported anymore in 4.0. Compile the container beforehand.', E_USER_DEPRECATED);
77+
}
78+
7579
parent::__construct($container);
7680

7781
$this->inlinedDefinitions = new \SplObjectStorage();

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ public static function setUpBeforeClass()
3535

3636
public function testDump()
3737
{
38-
$dumper = new PhpDumper($container = new ContainerBuilder());
38+
$container = new ContainerBuilder();
39+
$container->compile();
40+
$dumper = new PhpDumper($container);
3941

4042
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services1.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
4143
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services1-1.php', $dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer', 'namespace' => 'Symfony\Component\DependencyInjection\Dump')), '->dump() takes a class and a base_class options');
4244

4345
$container = new ContainerBuilder();
46+
$container->compile();
4447
new PhpDumper($container);
4548
}
4649

@@ -97,7 +100,9 @@ public function testDumpRelativeDir()
97100
*/
98101
public function testExportParameters($parameters)
99102
{
100-
$dumper = new PhpDumper(new ContainerBuilder(new ParameterBag($parameters)));
103+
$container = new ContainerBuilder(new ParameterBag($parameters));
104+
$container->compile();
105+
$dumper = new PhpDumper($container);
101106
$dumper->dump();
102107
}
103108

@@ -114,25 +119,33 @@ public function provideInvalidParameters()
114119
public function testAddParameters()
115120
{
116121
$container = include self::$fixturesPath.'/containers/container8.php';
122+
$container->compile();
117123
$dumper = new PhpDumper($container);
118124
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
119125
}
120126

121-
public function testAddService()
127+
/**
128+
* @group legacy
129+
* @expectedDeprecation Dumping an uncompiled ContainerBuilder is deprecated since version 3.3 and will not be supported anymore in 4.0. Compile the container beforehand.
130+
*/
131+
public function testAddServiceWithoutCompilation()
122132
{
123-
// without compilation
124133
$container = include self::$fixturesPath.'/containers/container9.php';
125134
$dumper = new PhpDumper($container);
126135
$this->assertEquals(str_replace('%path%', str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
136+
}
127137

128-
// with compilation
138+
public function testAddService()
139+
{
129140
$container = include self::$fixturesPath.'/containers/container9.php';
130141
$container->compile();
131142
$dumper = new PhpDumper($container);
132143
$this->assertEquals(str_replace('%path%', str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9_compiled.php')), $dumper->dump(), '->dump() dumps services');
133144

134-
$dumper = new PhpDumper($container = new ContainerBuilder());
145+
$container = new ContainerBuilder();
135146
$container->register('foo', 'FooClass')->addArgument(new \stdClass());
147+
$container->compile();
148+
$dumper = new PhpDumper($container);
136149
try {
137150
$dumper->dump();
138151
$this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
@@ -145,6 +158,7 @@ public function testAddService()
145158
public function testServicesWithAnonymousFactories()
146159
{
147160
$container = include self::$fixturesPath.'/containers/container19.php';
161+
$container->compile();
148162
$dumper = new PhpDumper($container);
149163

150164
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories');
@@ -156,6 +170,7 @@ public function testAddServiceIdWithUnsupportedCharacters()
156170
$container = new ContainerBuilder();
157171
$container->register('bar$', 'FooClass');
158172
$container->register('bar$!', 'FooClass');
173+
$container->compile();
159174
$dumper = new PhpDumper($container);
160175
eval('?>'.$dumper->dump(array('class' => $class)));
161176

@@ -169,6 +184,7 @@ public function testConflictingServiceIds()
169184
$container = new ContainerBuilder();
170185
$container->register('foo_bar', 'FooClass');
171186
$container->register('foobar', 'FooClass');
187+
$container->compile();
172188
$dumper = new PhpDumper($container);
173189
eval('?>'.$dumper->dump(array('class' => $class)));
174190

@@ -182,6 +198,7 @@ public function testConflictingMethodsWithParent()
182198
$container = new ContainerBuilder();
183199
$container->register('bar', 'FooClass');
184200
$container->register('foo_bar', 'FooClass');
201+
$container->compile();
185202
$dumper = new PhpDumper($container);
186203
eval('?>'.$dumper->dump(array(
187204
'class' => $class,
@@ -203,6 +220,7 @@ public function testInvalidFactories($factory)
203220
$def = new Definition('stdClass');
204221
$def->setFactory($factory);
205222
$container->setDefinition('bar', $def);
223+
$container->compile();
206224
$dumper = new PhpDumper($container);
207225
$dumper->dump();
208226
}
@@ -285,6 +303,7 @@ public function testCircularReference()
285303
public function testDumpAutowireData()
286304
{
287305
$container = include self::$fixturesPath.'/containers/container24.php';
306+
$container->compile();
288307
$dumper = new PhpDumper($container);
289308

290309
$this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services24.php'), $dumper->dump());

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
77
use Symfony\Component\DependencyInjection\Exception\LogicException;
88
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9-
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
9+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
1010

1111
/**
1212
* Container.
@@ -24,6 +24,24 @@ class Container extends AbstractContainer
2424
*/
2525
public function __construct()
2626
{
27-
parent::__construct();
27+
$this->services = array();
28+
29+
$this->aliases = array();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function compile()
36+
{
37+
throw new LogicException('You cannot compile a dumped frozen container.');
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function isFrozen()
44+
{
45+
return true;
2846
}
2947
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
66
use Symfony\Component\DependencyInjection\Exception\LogicException;
77
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
8-
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
8+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
99

1010
/**
1111
* ProjectServiceContainer.
@@ -23,6 +23,24 @@ class ProjectServiceContainer extends Container
2323
*/
2424
public function __construct()
2525
{
26-
parent::__construct();
26+
$this->services = array();
27+
28+
$this->aliases = array();
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function compile()
35+
{
36+
throw new LogicException('You cannot compile a dumped frozen container.');
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function isFrozen()
43+
{
44+
return true;
2745
}
2846
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
66
use Symfony\Component\DependencyInjection\Exception\LogicException;
77
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
8-
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
8+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
99

1010
/**
1111
* ProjectServiceContainer.
@@ -23,11 +23,29 @@ class ProjectServiceContainer extends Container
2323
*/
2424
public function __construct()
2525
{
26-
parent::__construct();
26+
$this->services = array();
2727
$this->methodMap = array(
2828
'service_from_anonymous_factory' => 'getServiceFromAnonymousFactoryService',
2929
'service_with_method_call_and_factory' => 'getServiceWithMethodCallAndFactoryService',
3030
);
31+
32+
$this->aliases = array();
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function compile()
39+
{
40+
throw new LogicException('You cannot compile a dumped frozen container.');
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function isFrozen()
47+
{
48+
return true;
3149
}
3250

3351
/**

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
66
use Symfony\Component\DependencyInjection\Exception\LogicException;
77
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
8-
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
8+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
99

1010
/**
1111
* ProjectServiceContainer.
@@ -23,10 +23,28 @@ class ProjectServiceContainer extends Container
2323
*/
2424
public function __construct()
2525
{
26-
parent::__construct();
26+
$this->services = array();
2727
$this->methodMap = array(
2828
'foo' => 'getFooService',
2929
);
30+
31+
$this->aliases = array();
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function compile()
38+
{
39+
throw new LogicException('You cannot compile a dumped frozen container.');
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function isFrozen()
46+
{
47+
return true;
3048
}
3149

3250
/**

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

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
66
use Symfony\Component\DependencyInjection\Exception\LogicException;
77
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
8-
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
8+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
99

1010
/**
1111
* ProjectServiceContainer.
@@ -23,7 +23,95 @@ class ProjectServiceContainer extends Container
2323
*/
2424
public function __construct()
2525
{
26-
parent::__construct(new ParameterBag($this->getDefaultParameters()));
26+
$this->parameters = $this->getDefaultParameters();
27+
28+
$this->services = array();
29+
30+
$this->aliases = array();
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function compile()
37+
{
38+
throw new LogicException('You cannot compile a dumped frozen container.');
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function isFrozen()
45+
{
46+
return true;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function getParameter($name)
53+
{
54+
$name = strtolower($name);
55+
56+
if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters) || isset($this->loadedDynamicParameters[$name]))) {
57+
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
58+
}
59+
if (isset($this->loadedDynamicParameters[$name])) {
60+
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
61+
}
62+
63+
return $this->parameters[$name];
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function hasParameter($name)
70+
{
71+
$name = strtolower($name);
72+
73+
return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters) || isset($this->loadedDynamicParameters[$name]);
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function setParameter($name, $value)
80+
{
81+
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
82+
}
83+
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
public function getParameterBag()
88+
{
89+
if (null === $this->parameterBag) {
90+
$parameters = $this->parameters;
91+
foreach ($this->loadedDynamicParameters as $name => $loaded) {
92+
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
93+
}
94+
$this->parameterBag = new FrozenParameterBag($parameters);
95+
}
96+
97+
return $this->parameterBag;
98+
}
99+
100+
private $loadedDynamicParameters = array();
101+
private $dynamicParameters = array();
102+
103+
/**
104+
* Computes a dynamic parameter.
105+
*
106+
* @param string The name of the dynamic parameter to load
107+
*
108+
* @return mixed The value of the dynamic parameter
109+
*
110+
* @throws InvalidArgumentException When the dynamic parameter does not exist
111+
*/
112+
private function getDynamicParameter($name)
113+
{
114+
throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
27115
}
28116

29117
/**
@@ -34,9 +122,9 @@ public function __construct()
34122
protected function getDefaultParameters()
35123
{
36124
return array(
37-
'foo' => '%baz%',
125+
'foo' => 'bar',
38126
'baz' => 'bar',
39-
'bar' => 'foo is %%foo bar',
127+
'bar' => 'foo is %foo bar',
40128
'escape' => '@escapeme',
41129
'values' => array(
42130
0 => true,

0 commit comments

Comments
 (0)
0