Issue #3293926 by kingdutch, geekygnr, godotislate, m4olivei, longwave: Error decorating non-existent service when inner service's module not installed

(cherry picked from commit c0053e636e)
merge-requests/9998/head
Lee Rowlands 2024-10-24 12:11:28 +10:00
parent 659b8eaef4
commit a64662a3ce
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
2 changed files with 56 additions and 2 deletions

View File

@ -390,10 +390,32 @@ class YamlFileLoader
$definition->addTag($name, $tag);
}
if (isset($service['decorates'])) {
if (null !== $decorates = $service['decorates'] ?? null) {
if ('' !== $decorates && '@' === $decorates[0]) {
throw new InvalidArgumentException(\sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($decorates, 1)));
}
$decorationOnInvalid = \array_key_exists('decoration_on_invalid', $service) ? $service['decoration_on_invalid'] : 'exception';
if ('exception' === $decorationOnInvalid) {
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
}
elseif ('ignore' === $decorationOnInvalid) {
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
}
elseif (null === $decorationOnInvalid) {
$invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
}
elseif ('null' === $decorationOnInvalid) {
throw new InvalidArgumentException(\sprintf('Invalid value "%s" for attribute "decoration_on_invalid" on service "%s". Did you mean null (without quotes) in "%s"?', $decorationOnInvalid, $id, $file));
}
else {
throw new InvalidArgumentException(\sprintf('Invalid value "%s" for attribute "decoration_on_invalid" on service "%s". Did you mean "exception", "ignore" or null in "%s"?', $decorationOnInvalid, $id, $file));
}
$renameId = $service['decoration_inner_name'] ?? null;
$priority = $service['decoration_priority'] ?? 0;
$definition->setDecoratedService($service['decorates'], $renameId, $priority);
$definition->setDecoratedService($decorates, $renameId, $priority, $invalidBehavior);
}
if (isset($service['autowire'])) {

View File

@ -200,6 +200,38 @@ YAML,
YAML,
'The service file "vfs://drupal/modules/example/example.yml" is not valid: it contains invalid root key(s) "do not". Services have to be added under "services" and Parameters under "parameters".',
],
'decorates must be without @' => [<<<YAML
services:
example_service_1:
class: \Drupal\Core\ExampleClass
example_decoration:
class: \Drupal\Core\ExampleClass
decorates: "@example_service_1"
YAML,
'The value of the "decorates" option for the "example_decoration" service must be the id of the service without the "@" prefix (replace "@example_service_1" with "example_service_1").',
],
'decorates_on_invalid may not be "null" with quotes' => [<<<YAML
services:
example_service_1:
class: \Drupal\Core\ExampleClass
example_decoration:
class: \Drupal\Core\ExampleClass
decorates: example_service_1
decoration_on_invalid: "null"
YAML,
'Invalid value "null" for attribute "decoration_on_invalid" on service "example_decoration". Did you mean null (without quotes) in "vfs://drupal/modules/example/example.yml"?',
],
'decoration_on_invalid must be valid' => [<<<YAML
services:
example_service_1:
class: \Drupal\Core\ExampleClass
example_decoration:
class: \Drupal\Core\ExampleClass
decorates: example_service_1
decoration_on_invalid: foo
YAML,
'Invalid value "foo" for attribute "decoration_on_invalid" on service "example_decoration". Did you mean "exception", "ignore" or null in "vfs://drupal/modules/example/example.yml"?',
],
];
}