You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #20113 Use the method map as authoritative list of factories for dumped containers (stof)
This PR was merged into the 3.2-dev branch.
Discussion
----------
Use the method map as authoritative list of factories for dumped containers
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | yes
| New feature? | no
| BC breaks? | only for weird broken cases
| Deprecations? | yes (but only for people doing weird things)
| Tests pass? | yes
| Fixed tickets | #11761, #19690
| License | MIT
| Doc PR | n/a
The initial implementation of the method factory discovery was based on a naming convention for factory methods. However, this naming convention allowed to generate the same name for multiple ids. In the meantime, a method map was introduced to solve this issue (and others).
When accessing a service with a different id than the official one (thanks to ambiguities), this breaks the sharing of the service, as it creates a new instance each time and replaces the existing shared instance. This was also inconsistent between a dumped container (affected by this) and a non-dumped container (reporting
8000
a service not found error for the other id).
The method map is now the authoritative way to discover available service factories. When the dumped container was generated with a method map (which is the case when using the dumper shipped in the component), the logic based on defined methods is not executed anymore. This forbids using another id than the real one to access the service (preventing to trigger the broken behavior). So this breaks BC for people being lucky (i.e. they were using the broken id only once and *before* any usage of the official id) and fixes a WTF bug for all others.
When using a dumper which does not fill the method map, the old logic is still applied, but deprecation warnings are triggered on access to dumped services. Currently, this will trigger a deprecation warning for each new service instantiation. I have not found an easy way to trigger it only once (except adding a private property to remember we already triggered it, but is it worth it ?), but only people writing a project container by hand or writing their own dumper would ever see such deprecation anyway (as the core dumper generates the method map).
Additionally, this makes ``getServiceIds`` faster by avoiding doing a regex match for each method in the class.
Commits
-------
03b9108 Use the method map as authoritative list of factories for dumped containers
Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Container.php
+34-10Lines changed: 34 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -206,15 +206,29 @@ public function has($id)
206
206
) {
207
207
returntrue;
208
208
}
209
+
210
+
if (isset($this->privates[$id])) {
211
+
@trigger_error(sprintf('Checking for the existence of the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
212
+
}
213
+
214
+
if (isset($this->methodMap[$id])) {
215
+
returntrue;
216
+
}
217
+
209
218
if (--$i && $id !== $lcId = strtolower($id)) {
210
219
$id = $lcId;
211
-
} else {
212
-
if (isset($this->privates[$id])) {
213
-
@trigger_error(sprintf('Checking for the existence of the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
214
-
}
220
+
continue;
221
+
}
222
+
223
+
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
224
+
// and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
@trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
281
+
// and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
282
+
@trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
266
283
// $method is set to the right value, proceed
267
284
} else {
268
285
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
@@ -341,14 +358,21 @@ public function reset()
341
358
publicfunctiongetServiceIds()
342
359
{
343
360
$ids = array();
344
-
foreach (get_class_methods($this) as$method) {
345
-
if (preg_match('/^get(.+)Service$/', $method, $match)) {
346
-
$ids[] = self::underscore($match[1]);
361
+
362
+
if (!$this->methodMap && !$thisinstanceof ContainerBuilder && __CLASS__ !== static::class) {
363
+
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
364
+
// and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
365
+
@trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
366
+
367
+
foreach (get_class_methods($this) as$method) {
368
+
if (preg_match('/^get(.+)Service$/', $method, $match)) {
Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
+148-3Lines changed: 148 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -126,7 +126,25 @@ public function testGetServiceIds()
126
126
127
127
$sc = newProjectServiceContainer();
128
128
$sc->set('foo', $obj = new \stdClass());
129
-
$this->assertEquals(array('internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods, followed by service ids defined by set()');
129
+
$this->assertEquals(array('service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()');
130
+
}
131
+
132
+
/**
133
+
* @group legacy
134
+
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
135
+
*/
136
+
publicfunctiontestGetLegacyServiceIds()
137
+
{
138
+
$sc = newLegacyProjectServiceContainer();
139
+
$sc->set('foo', $obj = new \stdClass());
140
+
141
+
$deprecations = array(
142
+
'Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.',
143
+
);
144
+
145
+
ErrorAssert::assertDeprecationsAreTriggered($deprecations, function () use ($sc) {
146
+
$this->assertEquals(array('internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods, followed by service ids defined by set()');
147
+
});
130
148
}
131
149
132
150
publicfunctiontestSet()
@@ -160,7 +178,6 @@ public function testGet()
160
178
$this->assertEquals($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
161
179
$this->assertEquals($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
162
180
$this->assertEquals($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
163
-
$this->assertEquals($sc->__foo_baz, $sc->get('foo\\baz'), '->get() returns the service if a get*Method() is defined');
164
181
165
182
$sc->set('bar', $bar = new \stdClass());
166
183
$this->assertEquals($bar, $sc->get('bar'), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()');
@@ -174,6 +191,43 @@ public function testGet()
174
191
$this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service is empty');
175
192
}
176
193
194
+
/**
195
+
* @group legacy
196
+
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
197
+
*/
198
+
publicfunctiontestLegacyGet()
199
+
{
200
+
$sc = newLegacyProjectServiceContainer();
201
+
$sc->set('foo', $foo = new \stdClass());
202
+
203
+
$deprecations = array(
204
+
'Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.',
205
+
'Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.',
206
+
'Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.',
207
+
'Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.',
208
+
);
209
+
210
+
ErrorAssert::assertDeprecationsAreTriggered($deprecations, function () use ($sc, $foo) {
211
+
$this->assertEquals($foo, $sc->get('foo'), '->get() returns the service for the given id');
212
+
$this->assertEquals($foo, $sc->get('Foo'), '->get() returns the service for the given id, and converts id to lowercase');
213
+
$this->assertEquals($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
214
+
$this->assertEquals($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
215
+
$this->assertEquals($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
216
+
$this->assertEquals($sc->__foo_baz, $sc->get('foo\\baz'), '->get() returns the service if a get*Method() is defined');
217
+
218
+
$sc->set('bar', $bar = new \stdClass());
219
+
$this->assertEquals($bar, $sc->get('bar'), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()');
220
+
221
+
try {
222
+
$sc->get('');
223
+
$this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
224
+
} catch (\Exception$e) {
225
+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e, '->get() throws a ServiceNotFoundException exception if the service is empty');
226
+
}
227
+
$this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service is empty');
$this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
231
285
$this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
232
286
$this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
233
-
$this->assertTrue($sc->has('foo\\baz'), '->has() returns true if a get*Method() is defined');
287
+
}
288
+
289
+
/**
290
+
* @group legacy
291
+
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
292
+
*/
293
+
publicfunctiontestLegacyHas()
294
+
{
295
+
$sc = newLegacyProjectServiceContainer();
296
+
$sc->set('foo', new \stdClass());
297
+
298
+
$deprecations = array(
299
+
'Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.',
300
+
'Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.',
301
+
'Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.',
302
+
'Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.',
303
+
);
304
+
305
+
ErrorAssert::assertDeprecationsAreTriggered($deprecations, function () use ($sc) {
306
+
$this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
307
+
$this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
308
+
$this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
309
+
$this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
310
+
$this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
311
+
$this->assertTrue($sc->has('foo\\baz'), '->has() returns true if a get*Method() is defined');
312
+
});
234
313
}
235
314
236
315
publicfunctiontestInitialized()
@@ -385,6 +464,72 @@ public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
0 commit comments