10000 [DI] Deprecate underscore-services in YamlFileLoader · symfony/symfony@7781082 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7781082

Browse files
[DI] Deprecate underscore-services in YamlFileLoader
1 parent 55a34b7 commit 7781082

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private function parseDefaults(array &$content, $file)
182182
throw new InvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file));
183183
}
184184
if (isset($defaults['alias']) || isset($defaults['class']) || isset($defaults['factory'])) {
185-
@trigger_error('Giving a service the "_defaults" name is deprecated since Symfony 3.3 and will be forbidden in 4.0. Rename your service.', E_USER_DEPRECATED);
185+
// @deprecated code path, to be removed in 4.0
186186

187187
return array();
188188
}
@@ -239,6 +239,9 @@ private function parseDefaults(array &$content, $file)
239239
*/
240240
private function parseDefinition($id, $service, $file, array $defaults)
241241
{
242+
if (preg_match('/^_[a-zA-Z0-9_]*$/', $id)) {
243+
@trigger_error(sprintf('Service names that start with an underscore are deprecated since Symfony 3.3 and will be reserved in 4.0. Rename the "%s" service or define it in XML instead.', $id), E_USER_DEPRECATED);
244+
}
242245
if (is_string($service) && 0 === strpos($service, '@')) {
243246
$public = isset($defaults['public']) ? $defaults['public'] : true;
244247
$this->container->setAlias($id, new Alias(substr($service, 1), $public));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
_foo:
3+
class: Foo

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,15 @@ public function testInvalidTagsWithDefaults()
436436
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
437437
$loader->load('services31_invalid_tags.yml');
438438
}
439+
440+
/**
441+
* @group legacy
442+
* @expectedDeprecation Service names that start with an underscore are deprecated since Symfony 3.3 and will be reserved in 4.0. Rename the "_foo" service or define it in XML instead.
443+
*/
444+
public function testUnderscoreServiceId()
445+
{
446+
$container = new ContainerBuilder();
447+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
448+
$loader->load('services_underscore.yml');
449+
}
439450
}

0 commit comments

Comments
 (0)
0