annotate vendor/symfony/dependency-injection/Compiler/CheckDefinitionValidityPass.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\DependencyInjection\Compiler;
Chris@0 13
Chris@0 14 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@14 15 use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
Chris@0 16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * This pass validates each definition individually only taking the information
Chris@0 20 * into account which is contained in the definition itself.
Chris@0 21 *
Chris@0 22 * Later passes can rely on the following, and specifically do not need to
Chris@0 23 * perform these checks themselves:
Chris@0 24 *
Chris@0 25 * - non synthetic, non abstract services always have a class set
Chris@0 26 * - synthetic services are always public
Chris@0 27 *
Chris@0 28 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
Chris@0 29 */
Chris@0 30 class CheckDefinitionValidityPass implements CompilerPassInterface
Chris@0 31 {
Chris@0 32 /**
Chris@0 33 * Processes the ContainerBuilder to validate the Definition.
Chris@0 34 *
Chris@0 35 * @throws RuntimeException When the Definition is invalid
Chris@0 36 */
Chris@0 37 public function process(ContainerBuilder $container)
Chris@0 38 {
Chris@0 39 foreach ($container->getDefinitions() as $id => $definition) {
Chris@0 40 // synthetic service is public
Chris@14 41 if ($definition->isSynthetic() && !($definition->isPublic() || $definition->isPrivate())) {
Chris@0 42 throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
Chris@0 43 }
Chris@0 44
Chris@0 45 // non-synthetic, non-abstract service has class
Chris@0 46 if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
Chris@0 47 if ($definition->getFactory()) {
Chris@0 48 throw new RuntimeException(sprintf('Please add the class to service "%s" even if it is constructed by a factory since we might need to add method calls based on compile-time checks.', $id));
Chris@0 49 }
Chris@14 50 if (class_exists($id) || interface_exists($id, false)) {
Chris@14 51 throw new RuntimeException(sprintf(
Chris@14 52 'The definition for "%s" has no class attribute, and appears to reference a '
Chris@14 53 .'class or interface in the global namespace. Leaving out the "class" attribute '
Chris@14 54 .'is only allowed for namespaced classes. Please specify the class attribute '
Chris@14 55 .'explicitly to get rid of this error.',
Chris@14 56 $id
Chris@14 57 ));
Chris@14 58 }
Chris@0 59
Chris@0 60 throw new RuntimeException(sprintf(
Chris@0 61 'The definition for "%s" has no class. If you intend to inject '
Chris@0 62 .'this service dynamically at runtime, please mark it as synthetic=true. '
Chris@0 63 .'If this is an abstract definition solely used by child definitions, '
Chris@0 64 .'please add abstract=true, otherwise specify a class to get rid of this error.',
Chris@0 65 $id
Chris@0 66 ));
Chris@0 67 }
Chris@0 68
Chris@0 69 // tag attribute values must be scalars
Chris@0 70 foreach ($definition->getTags() as $name => $tags) {
Chris@0 71 foreach ($tags as $attributes) {
Chris@0 72 foreach ($attributes as $attribute => $value) {
Chris@0 73 if (!is_scalar($value) && null !== $value) {
Chris@0 74 throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
Chris@0 75 }
Chris@0 76 }
Chris@0 77 }
Chris@0 78 }
Chris@14 79
Chris@14 80 if ($definition->isPublic() && !$definition->isPrivate()) {
Chris@14 81 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
Chris@14 82 if (null !== $usedEnvs) {
Chris@14 83 throw new EnvParameterException(array($resolvedId), null, 'A service name ("%s") cannot contain dynamic values.');
Chris@14 84 }
Chris@14 85 }
Chris@14 86 }
Chris@14 87
Chris@14 88 foreach ($container->getAliases() as $id => $alias) {
Chris@14 89 if ($alias->isPublic() && !$alias->isPrivate()) {
Chris@14 90 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
Chris@14 91 if (null !== $usedEnvs) {
Chris@14 92 throw new EnvParameterException(array($resolvedId), null, 'An alias name ("%s") cannot contain dynamic values.');
Chris@14 93 }
Chris@14 94 }
Chris@0 95 }
Chris@0 96 }
Chris@0 97 }