annotate vendor/symfony/dependency-injection/Compiler/CheckDefinitionValidityPass.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
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@17 51 if (0 === strpos($id, '\\') && 1 < substr_count($id, '\\')) {
Chris@17 52 throw new RuntimeException(sprintf(
Chris@17 53 'The definition for "%s" has no class attribute, and appears to reference a class or interface. '
Chris@17 54 .'Please specify the class attribute explicitly or remove the leading backslash by renaming '
Chris@17 55 .'the service to "%s" to get rid of this error.',
Chris@17 56 $id, substr($id, 1)
Chris@17 57 ));
Chris@17 58 }
Chris@17 59
Chris@14 60 throw new RuntimeException(sprintf(
Chris@14 61 'The definition for "%s" has no class attribute, and appears to reference a '
Chris@14 62 .'class or interface in the global namespace. Leaving out the "class" attribute '
Chris@14 63 .'is only allowed for namespaced classes. Please specify the class attribute '
Chris@14 64 .'explicitly to get rid of this error.',
Chris@14 65 $id
Chris@14 66 ));
Chris@14 67 }
Chris@0 68
Chris@17 69 throw new RuntimeException(sprintf('The definition for "%s" has no class. If you intend to inject this service dynamically at runtime, please mark it as synthetic=true. If this is an abstract definition solely used by child definitions, please add abstract=true, otherwise specify a class to get rid of this error.', $id));
Chris@0 70 }
Chris@0 71
Chris@0 72 // tag attribute values must be scalars
Chris@0 73 foreach ($definition->getTags() as $name => $tags) {
Chris@0 74 foreach ($tags as $attributes) {
Chris@0 75 foreach ($attributes as $attribute => $value) {
Chris@0 76 if (!is_scalar($value) && null !== $value) {
Chris@0 77 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 78 }
Chris@0 79 }
Chris@0 80 }
Chris@0 81 }
Chris@14 82
Chris@14 83 if ($definition->isPublic() && !$definition->isPrivate()) {
Chris@14 84 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
Chris@14 85 if (null !== $usedEnvs) {
Chris@17 86 throw new EnvParameterException([$resolvedId], null, 'A service name ("%s") cannot contain dynamic values.');
Chris@14 87 }
Chris@14 88 }
Chris@14 89 }
Chris@14 90
Chris@14 91 foreach ($container->getAliases() as $id => $alias) {
Chris@14 92 if ($alias->isPublic() && !$alias->isPrivate()) {
Chris@14 93 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
Chris@14 94 if (null !== $usedEnvs) {
Chris@17 95 throw new EnvParameterException([$resolvedId], null, 'An alias name ("%s") cannot contain dynamic values.');
Chris@14 96 }
Chris@14 97 }
Chris@0 98 }
Chris@0 99 }
Chris@0 100 }