annotate vendor/symfony/dependency-injection/Compiler/CheckReferenceValidityPass.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\Definition;
Chris@0 15 use Symfony\Component\DependencyInjection\Reference;
Chris@0 16 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@0 17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Checks the validity of references.
Chris@0 21 *
Chris@0 22 * The following checks are performed by this pass:
Chris@0 23 * - target definitions are not abstract
Chris@0 24 *
Chris@0 25 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
Chris@0 26 */
Chris@0 27 class CheckReferenceValidityPass implements CompilerPassInterface
Chris@0 28 {
Chris@0 29 private $container;
Chris@0 30 private $currentId;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Processes the ContainerBuilder to validate References.
Chris@0 34 *
Chris@0 35 * @param ContainerBuilder $container
Chris@0 36 */
Chris@0 37 public function process(ContainerBuilder $container)
Chris@0 38 {
Chris@0 39 $this->container = $container;
Chris@0 40
Chris@0 41 foreach ($container->getDefinitions() as $id => $definition) {
Chris@0 42 if ($definition->isSynthetic() || $definition->isAbstract()) {
Chris@0 43 continue;
Chris@0 44 }
Chris@0 45
Chris@0 46 $this->currentId = $id;
Chris@0 47
Chris@0 48 $this->validateReferences($definition->getArguments());
Chris@0 49 $this->validateReferences($definition->getMethodCalls());
Chris@0 50 $this->validateReferences($definition->getProperties());
Chris@0 51 }
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Validates an array of References.
Chris@0 56 *
Chris@0 57 * @param array $arguments An array of Reference objects
Chris@0 58 *
Chris@0 59 * @throws RuntimeException when there is a reference to an abstract definition.
Chris@0 60 */
Chris@0 61 private function validateReferences(array $arguments)
Chris@0 62 {
Chris@0 63 foreach ($arguments as $argument) {
Chris@0 64 if (is_array($argument)) {
Chris@0 65 $this->validateReferences($argument);
Chris@0 66 } elseif ($argument instanceof Reference) {
Chris@0 67 $targetDefinition = $this->getDefinition((string) $argument);
Chris@0 68
Chris@0 69 if (null !== $targetDefinition && $targetDefinition->isAbstract()) {
Chris@0 70 throw new RuntimeException(sprintf(
Chris@0 71 'The definition "%s" has a reference to an abstract definition "%s". '
Chris@0 72 .'Abstract definitions cannot be the target of references.',
Chris@0 73 $this->currentId,
Chris@0 74 $argument
Chris@0 75 ));
Chris@0 76 }
Chris@0 77 }
Chris@0 78 }
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * Returns the Definition given an id.
Chris@0 83 *
Chris@0 84 * @param string $id Definition identifier
Chris@0 85 *
Chris@0 86 * @return Definition
Chris@0 87 */
Chris@0 88 private function getDefinition($id)
Chris@0 89 {
Chris@0 90 if (!$this->container->hasDefinition($id)) {
Chris@0 91 return;
Chris@0 92 }
Chris@0 93
Chris@0 94 return $this->container->getDefinition($id);
Chris@0 95 }
Chris@0 96 }