8000 [DI] deprecate short callables in yaml by nicolas-grekas · Pull Request #31543 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] deprecate short callables in yaml #31543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions UPGRADE-4.4.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* deprecated support for short factories and short configurators in Yaml

4.3.0
-----

Expand Down
Original file line number Diff line number Diff line change
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $parameter is something that the user types ... then we should replace short %s syntax by short "%s" syntax to avoid potential confusions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can only be factory of configurator, so no issue here.


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

Expand Down
Original file line number Diff line number Diff line change
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