Skip to content

Commit

Permalink
feature #21484 [DI] Deprecate underscore-services in YamlFileLoader (…
Browse files Browse the repository at this point in the history
…nicolas-grekas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Deprecate underscore-services in YamlFileLoader

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | no
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

As discussed when introducing `_defaults`

Commits
-------

7781082 [DI] Deprecate underscore-services in YamlFileLoader
  • Loading branch information
fabpot committed Feb 16, 2017
2 parents a70ee67 + 7781082 commit 7c9a5c1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Expand Up @@ -210,7 +210,7 @@ private function parseDefaults(array &$content, $file)
throw new InvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file));
}
if (isset($defaults['alias']) || isset($defaults['class']) || isset($defaults['factory'])) {
@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);
// @deprecated code path, to be removed in 4.0

return array();
}
Expand Down Expand Up @@ -282,6 +282,9 @@ private function isUsingShortSyntax(array $service)
*/
private function parseDefinition($id, $service, $file, array $defaults)
{
if (preg_match('/^_[a-zA-Z0-9_]*$/', $id)) {
@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);
}
if (is_string($service) && 0 === strpos($service, '@')) {
$public = isset($defaults['public']) ? $defaults['public'] : true;
$this->container->setAlias($id, new Alias(substr($service, 1), $public));
Expand Down
@@ -0,0 +1,3 @@
services:
_foo:
class: Foo
Expand Up @@ -488,4 +488,15 @@ public function testInvalidTagsWithDefaults()
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services31_invalid_tags.yml');
}

/**
* @group legacy
* @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.
*/
public function testUnderscoreServiceId()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services_underscore.yml');
}
}

0 comments on commit 7c9a5c1

Please sign in to comment.