Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DependencyInjection\Compiler; Chris@0: Chris@0: use Symfony\Component\DependencyInjection\Definition; Chris@0: use Symfony\Component\DependencyInjection\Reference; Chris@0: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: use Symfony\Component\DependencyInjection\Exception\RuntimeException; Chris@0: Chris@0: /** Chris@0: * Checks the validity of references. Chris@0: * Chris@0: * The following checks are performed by this pass: Chris@0: * - target definitions are not abstract Chris@0: * Chris@0: * @author Johannes M. Schmitt Chris@0: */ Chris@0: class CheckReferenceValidityPass implements CompilerPassInterface Chris@0: { Chris@0: private $container; Chris@0: private $currentId; Chris@0: Chris@0: /** Chris@0: * Processes the ContainerBuilder to validate References. Chris@0: * Chris@0: * @param ContainerBuilder $container Chris@0: */ Chris@0: public function process(ContainerBuilder $container) Chris@0: { Chris@0: $this->container = $container; Chris@0: Chris@0: foreach ($container->getDefinitions() as $id => $definition) { Chris@0: if ($definition->isSynthetic() || $definition->isAbstract()) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $this->currentId = $id; Chris@0: Chris@0: $this->validateReferences($definition->getArguments()); Chris@0: $this->validateReferences($definition->getMethodCalls()); Chris@0: $this->validateReferences($definition->getProperties()); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validates an array of References. Chris@0: * Chris@0: * @param array $arguments An array of Reference objects Chris@0: * Chris@0: * @throws RuntimeException when there is a reference to an abstract definition. Chris@0: */ Chris@0: private function validateReferences(array $arguments) Chris@0: { Chris@0: foreach ($arguments as $argument) { Chris@0: if (is_array($argument)) { Chris@0: $this->validateReferences($argument); Chris@0: } elseif ($argument instanceof Reference) { Chris@0: $targetDefinition = $this->getDefinition((string) $argument); Chris@0: Chris@0: if (null !== $targetDefinition && $targetDefinition->isAbstract()) { Chris@0: throw new RuntimeException(sprintf( Chris@0: 'The definition "%s" has a reference to an abstract definition "%s". ' Chris@0: .'Abstract definitions cannot be the target of references.', Chris@0: $this->currentId, Chris@0: $argument Chris@0: )); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the Definition given an id. Chris@0: * Chris@0: * @param string $id Definition identifier Chris@0: * Chris@0: * @return Definition Chris@0: */ Chris@0: private function getDefinition($id) Chris@0: { Chris@0: if (!$this->container->hasDefinition($id)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: return $this->container->getDefinition($id); Chris@0: } Chris@0: }