annotate vendor/symfony/dependency-injection/Compiler/CheckDefinitionValidityPass.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
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@0 15 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * This pass validates each definition individually only taking the information
Chris@0 19 * into account which is contained in the definition itself.
Chris@0 20 *
Chris@0 21 * Later passes can rely on the following, and specifically do not need to
Chris@0 22 * perform these checks themselves:
Chris@0 23 *
Chris@0 24 * - non synthetic, non abstract services always have a class set
Chris@0 25 * - synthetic services are always public
Chris@0 26 *
Chris@0 27 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
Chris@0 28 */
Chris@0 29 class CheckDefinitionValidityPass implements CompilerPassInterface
Chris@0 30 {
Chris@0 31 /**
Chris@0 32 * Processes the ContainerBuilder to validate the Definition.
Chris@0 33 *
Chris@0 34 * @param ContainerBuilder $container
Chris@0 35 *
Chris@0 36 * @throws RuntimeException When the Definition is invalid
Chris@0 37 */
Chris@0 38 public function process(ContainerBuilder $container)
Chris@0 39 {
Chris@0 40 foreach ($container->getDefinitions() as $id => $definition) {
Chris@0 41 // synthetic service is public
Chris@0 42 if ($definition->isSynthetic() && !$definition->isPublic()) {
Chris@0 43 throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
Chris@0 44 }
Chris@0 45
Chris@0 46 // non-synthetic, non-abstract service has class
Chris@0 47 if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
Chris@0 48 if ($definition->getFactory()) {
Chris@0 49 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 50 }
Chris@0 51
Chris@0 52 throw new RuntimeException(sprintf(
Chris@0 53 'The definition for "%s" has no class. If you intend to inject '
Chris@0 54 .'this service dynamically at runtime, please mark it as synthetic=true. '
Chris@0 55 .'If this is an abstract definition solely used by child definitions, '
Chris@0 56 .'please add abstract=true, otherwise specify a class to get rid of this error.',
Chris@0 57 $id
Chris@0 58 ));
Chris@0 59 }
Chris@0 60
Chris@0 61 // tag attribute values must be scalars
Chris@0 62 foreach ($definition->getTags() as $name => $tags) {
Chris@0 63 foreach ($tags as $attributes) {
Chris@0 64 foreach ($attributes as $attribute => $value) {
Chris@0 65 if (!is_scalar($value) && null !== $value) {
Chris@0 66 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 67 }
Chris@0 68 }
Chris@0 69 }
Chris@0 70 }
Chris@0 71 }
Chris@0 72 }
Chris@0 73 }