Skip to content

Commit 4b3ca2c

Browse files
feature #31543 [DI] deprecate short callables in yaml (nicolas-grekas)
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 ------- f8a04fdda6 [DI] deprecate short callables in yaml
2 parents f8b84b0 + bee8c1c commit 4b3ca2c

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* deprecated support for short factories and short configurators in Yaml
8+
49
4.3.0
510
-----
611

Loader/YamlFileLoader.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -574,28 +574,29 @@ private function parseDefinition($id, $service, $file, array $defaults)
574574
/**
575575
* Parses a callable.
576576
*
577-
* @param string|array $callable A callable
578-
* @param string $parameter A parameter (e.g. 'factory' or 'configurator')
579-
* @param string $id A service identifier
580-
* @param string $file A parsed file
577+
* @param string|array $callable A callable reference
578+
* @param string $parameter The type of callable (e.g. 'factory' or 'configurator')
581579
*
582580
* @throws InvalidArgumentException When errors occur
583581
*
584582
* @return string|array|Reference A parsed callable
585583
*/
586-
private function parseCallable($callable, $parameter, $id, $file)
584+
private function parseCallable($callable, string $parameter, string $id, string $file)
587585
{
588586
if (\is_string($callable)) {
589587
if ('' !== $callable && '@' === $callable[0]) {
590588
if (false === strpos($callable, ':')) {
591589
return [$this->resolveServices($callable, $file), '__invoke'];
592590
}
593-
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)));
591+
592+
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));
594593
}
595594

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

598+
@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);
599+
599600
return [$this->resolveServices('@'.$parts[0], $file), $parts[1]];
600601
}
601602

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ public function testDeprecatedAliases()
191191
$this->assertSame($message, $container->getAlias('alias_for_foobar')->getDeprecationMessage('alias_for_foobar'));
192192
}
193193

194+
/**
195+
* @group legacy
196+
*/
194197
public function testLoadFactoryShortSyntax()
195198
{
196199
$container = new ContainerBuilder();
@@ -208,10 +211,13 @@ public function testFactorySyntaxError()
208211
$container = new ContainerBuilder();
209212
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
210213
$this->expectException(InvalidArgumentException::class);
211-
$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").');
214+
$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"');
212215
$loader->load('bad_factory_syntax.yml');
213216
}
214217

218+
/**
219+
* @group legacy
220+
*/
215221
public function testLoadConfiguratorShortSyntax()
216222
{
217223
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)