Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Enhance DX by throwing instead of triggering a deprecation notice #22185

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
Mar 28, 2017
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
6 changes: 6 additions & 0 deletions 6 UPGRADE-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ Debug
DependencyInjection
-------------------

* [BC BREAK] `_defaults` and `_instanceof` are now reserved service names in Yaml configurations. Please rename any services with that names.

* [BC BREAK] non-numeric keys in methods and constructors arguments have never been supported and are now forbidden. Please remove them if you happen to have one.

* Service names that start with an underscore are deprecated in Yaml files and will be reserved in 4.0. Please rename any services with such names.

* Autowiring-types have been deprecated, use aliases instead.

Before:
Expand Down
6 changes: 6 additions & 0 deletions 6 UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ Debug
DependencyInjection
-------------------

* `_defaults` and `_instanceof` are now reserved service names in Yaml configurations. Please rename any services with that names.

* Non-numeric keys in methods and constructors arguments have never been supported and are now forbidden. Please remove them if you happen to have one.

* Service names that start with an underscore are now reserved in Yaml files. Please rename any services with such names.

* Autowiring-types have been removed, use aliases instead.

Before:
Expand Down
7 changes: 1 addition & 6 deletions 7 src/Symfony/Component/DependencyInjection/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ class Alias
*/
public function __construct($id, $public = true)
{
if (!is_string($id)) {
$type = is_object($id) ? get_class($id) : gettype($id);
$id = (string) $id;
@trigger_error(sprintf('Non-string identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for Alias to "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED);
}
$this->id = $id;
$this->id = (string) $id;
$this->public = $public;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ protected function processValue($value, $isRoot = false)
$resolvedArguments = array();

foreach ($arguments as $key => $argument) {
if (is_int($key) || '' === $key || '$' !== $key[0]) {
if (!is_int($key)) {
@trigger_error(sprintf('Using key "%s" for defining arguments of method "%s" for service "%s" is deprecated since Symfony 3.3 and will throw an exception in 4.0. Use no keys or $named arguments instead.', $key, $method, $this->currentId), E_USER_DEPRECATED);
}
if (is_int($key)) {
$resolvedArguments[] = $argument;
continue;
}
if ('' === $key || '$' !== $key[0]) {
throw new InvalidArgumentException(sprintf('Invalid key "%s" found in arguments of method "%s" for service "%s": only integer or $named arguments are allowed.', $key, $method, $this->currentId));
Copy link
Contributor

@danrot danrot May 30, 2017

Choose a reason for hiding this comment

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

Looks like I am running into this now: What do you understand under a named arg? What exact configuration is forbidden by this change?

Is it possible that this disallows something like this:

<argument key="string" type="collection">
    <argument key="other_string">value</argument>
</argument>

Copy link
Member

Choose a reason for hiding this comment

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

the issue is key=string on your top-level argument. Adding a key on an argument only makes sense inside a collection-argument (as it is then the key inside the array) or using a supported way of defining named arguments.

Previously, defining keys for top-level arguments was not detected early, and would be causing weird bugs for some features

}

$parameters = null !== $parameters ? $parameters : $this->getParameters($class, $method);

Expand Down
2 changes: 0 additions & 2 deletions 2 src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,7 @@ protected function getEnv($name)
public function normalizeId($id)
{
if (!is_string($id)) {
$type = is_object($id) ? get_class($id) : gettype($id);
$id = (string) $id;
@trigger_error(sprintf('Non-string service identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for service "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED);
}
if (isset($this->normalizedIds[$normalizedId = strtolower($id)])) {
$normalizedId = $this->normalizedIds[$normalizedId];
Expand Down
33 changes: 13 additions & 20 deletions 33 src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,16 @@ private function parseDefinitions(array $content, $file)
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
}

if ($this->isUnderscoredParamValid($content, '_instanceof', $file)) {
if (isset($content['services']['_instanceof'])) {
$instanceof = $content['services']['_instanceof'];
unset($content['services']['_instanceof']);

if (!is_array($instanceof)) {
throw new InvalidArgumentException(sprintf('Service "_instanceof" key must be an array, "%s" given in "%s".', gettype($instanceof), $file));
}
$this->instanceof = array();
$this->isLoadingInstanceof = true;
foreach ($content['services']['_instanceof'] as $id => $service) {
foreach ($instanceof as $id => $service) {
if (!$service || !is_array($service)) {
throw new InvalidArgumentException(sprintf('Type definition "%s" must be a non-empty array within "_instanceof" in %s. Check your YAML syntax.', $id, $file));
}
Expand All @@ -217,7 +223,6 @@ private function parseDefinitions(array $content, $file)
}
$this->parseDefinition($id, $service, $file, array());
}
unset($content['services']['_instanceof']);
}

$this->isLoadingInstanceof = false;
Expand All @@ -237,13 +242,16 @@ private function parseDefinitions(array $content, $file)
*/
private function parseDefaults(array &$content, $file)
{
if (!$this->isUnderscoredParamValid($content, '_defaults', $file)) {
if (!isset($content['services']['_defaults'])) {
return array();
}

$defaults = $content['services']['_defaults'];
unset($content['services']['_defaults']);

if (!is_array($defaults)) {
throw new InvalidArgumentException(sprintf('Service "_defaults" key must be an array, "%s" given in "%s".', gettype($defaults), $file));
}

foreach ($defaults as $key => $default) {
if (!isset(self::$defaultsKeywords[$key])) {
throw new InvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', self::$defaultsKeywords)));
Expand Down Expand Up @@ -281,21 +289,6 @@ private function parseDefaults(array &$content, $file)
return $defaults;
}

private function isUnderscoredParamValid($content, $name, $file)
{
if (!isset($content['services'][$name])) {
return false;
}

if (!is_array($underscoreParam = $content['services'][$name])) {
throw new InvalidArgumentException(sprintf('Service "%s" key must be an array, "%s" given in "%s".', $name, gettype($underscoreParam), $file));
}

// @deprecated condition, to be removed in 4.0

return !isset($underscoreParam['alias']) && !isset($underscoreParam['class']) && !isset($underscoreParam['factory']);
}

/**
* @param array $service
*
Expand Down
7 changes: 1 addition & 6 deletions 7 src/Symfony/Component/DependencyInjection/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ class Reference
*/
public function __construct($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
{
if (!is_string($id)) {
$type = is_object($id) ? get_class($id) : gettype($id);
$id = (string) $id;
@trigger_error(sprintf('Non-string identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for Reference to "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED);
}
$this->id = $id;
$this->id = (string) $id;
$this->invalidBehavior = $invalidBehavior;
}

Expand Down
12 changes: 0 additions & 12 deletions 12 src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,6 @@ public function testGetInsensitivity()
$this->assertSame($foo, $sc->get('Foo'), '->get() returns the service for the given id, and converts id to lowercase');
}

/**
* @group legacy
* @expectedDeprecation Non-string service identifiers are deprecated since Symfony 3.3 and won't be supported in 4.0 for service "foo" ("Symfony\Component\DependencyInjection\Alias" given.) Cast it to string beforehand.
* @expectedDeprecation Service identifiers will be made case sensitive in Symfony 4.0. Using "Foo" instead of "foo" is deprecated since version 3.3.
*/
public function testNonStringNormalizeId()
{
$sc = new ProjectServiceContainer();
$this->assertSame('foo', $sc->normalizeId(new Alias('foo')));
$this->assertSame('foo', $sc->normalizeId('Foo'));
}

/**
* @group legacy
* @expectedDeprecation Service identifiers will be made case sensitive in Symfony 4.0. Using "foo" instead of "Foo" is deprecated since version 3.3.
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.