Skip to content

Commit

Permalink
feature #31543 [DI] deprecate short callables in yaml (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4-dev branch.

Discussion
----------

[DI] deprecate short callables in yaml

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

This deprecates defining factories as `foo:method` instead of `['@foo', 'method']` in yaml.

This is confusing syntax and misses the opportunity to raise an error when one does a typo and uses a single colon instead of two.

This basically reverts #19190

Commits
-------

f8a04fd [DI] deprecate short callables in yaml
  • Loading branch information
nicolas-grekas committed May 22, 2019
2 parents beb6036 + f8a04fd commit 5239873
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
21 changes: 21 additions & 0 deletions UPGRADE-4.4.md
@@ -0,0 +1,21 @@
UPGRADE FROM 4.3 to 4.4
=======================

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

* Deprecated support for short factories and short configurators in Yaml

Before:
```yaml
services:
my_service:
factory: factory_service:method
```

After:
```yaml
services:
my_service:
factory: ['@factory_service', method]
```
16 changes: 16 additions & 0 deletions UPGRADE-5.0.md
Expand Up @@ -69,6 +69,22 @@ DependencyInjection
env(NAME): '1.5'
```

* Removed support for short factories and short configurators in Yaml

Before:
```yaml
services:
my_service:
factory: factory_service:method
```

After:
```yaml
services:
my_service:
factory: ['@factory_service', method]
```

DoctrineBridge
--------------

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* deprecated support for short factories and short configurators in Yaml

4.3.0
-----

Expand Down
Expand Up @@ -574,28 +574,29 @@ private function parseDefinition($id, $service, $file, array $defaults)
/**
* Parses a callable.
*
* @param string|array $callable A callable
* @param string $parameter A parameter (e.g. 'factory' or 'configurator')
* @param string $id A service identifier
* @param string $file A parsed file
* @param string|array $callable A callable reference
* @param string $parameter The type of callable (e.g. 'factory' or 'configurator')
*
* @throws InvalidArgumentException When errors occur
*
* @return string|array|Reference A parsed callable
*/
private function parseCallable($callable, $parameter, $id, $file)
private function parseCallable($callable, string $parameter, string $id, string $file)
{
if (\is_string($callable)) {
if ('' !== $callable && '@' === $callable[0]) {
if (false === strpos($callable, ':')) {
return [$this->resolveServices($callable, $file), '__invoke'];
}
throw new InvalidArgumentException(sprintf('The value of the "%s" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $parameter, $id, $callable, substr($callable, 1)));

throw new InvalidArgumentException(sprintf('The value of the "%s" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s" in "%s").', $parameter, $id, $callable, substr($callable, 1), $file));
}

if (false !== strpos($callable, ':') && false === strpos($callable, '::')) {
$parts = explode(':', $callable);

@trigger_error(sprintf('Using short %s syntax for service "%s" is deprecated since Symfony 4.4, use "[\'@%s\', \'%s\']" instead.', $parameter, $id, ...$parts), E_USER_DEPRECATED);

return [$this->resolveServices('@'.$parts[0], $file), $parts[1]];
}

Expand Down
Expand Up @@ -191,6 +191,9 @@ public function testDeprecatedAliases()
$this->assertSame($message, $container->getAlias('alias_for_foobar')->getDeprecationMessage('alias_for_foobar'));
}

/**
* @group legacy
*/
public function testLoadFactoryShortSyntax()
{
$container = new ContainerBuilder();
Expand All @@ -208,10 +211,13 @@ public function testFactorySyntaxError()
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The value of the "factory" option for the "invalid_factory" service must be the id of the service without the "@" prefix (replace "@factory:method" with "factory:method").');
$this->expectExceptionMessage('The value of the "factory" option for the "invalid_factory" service must be the id of the service without the "@" prefix (replace "@factory:method" with "factory:method"');
$loader->load('bad_factory_syntax.yml');
}

/**
* @group legacy
*/
public function testLoadConfiguratorShortSyntax()
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit 5239873

Please sign in to comment.