This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Compiler/CheckReferenceValidityPass.php

100 lines
3.0 KiB
PHP
Raw Normal View History

2011-01-26 23:14:31 +00:00
<?php
2011-05-31 09:57:06 +01:00
/*
2012-03-31 22:00:32 +01:00
* This file is part of the Symfony package.
2011-05-31 09:57:06 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
*
2012-03-31 22:00:32 +01:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2011-05-31 09:57:06 +01:00
*/
2011-01-26 23:14:31 +00:00
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
2011-01-26 23:14:31 +00:00
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2011-01-26 23:14:31 +00:00
/**
2014-12-21 17:00:50 +00:00
* Checks the validity of references.
2011-01-26 23:14:31 +00:00
*
* The following checks are performed by this pass:
* - target definitions are not abstract
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class CheckReferenceValidityPass implements CompilerPassInterface
{
private $container;
private $currentId;
2011-01-26 23:14:31 +00:00
2011-02-13 18:06:41 +00:00
/**
* Processes the ContainerBuilder to validate References.
*
* @param ContainerBuilder $container
2011-02-13 18:06:41 +00:00
*/
2011-01-26 23:14:31 +00:00
public function process(ContainerBuilder $container)
{
$this->container = $container;
foreach ($container->getDefinitions() as $id => $definition) {
if ($definition->isSynthetic() || $definition->isAbstract()) {
continue;
}
$this->currentId = $id;
$this->validateReferences($definition->getArguments());
$this->validateReferences($definition->getMethodCalls());
$this->validateReferences($definition->getProperties());
2011-01-26 23:14:31 +00:00
}
}
2011-02-13 18:06:41 +00:00
/**
* Validates an array of References.
*
* @param array $arguments An array of Reference objects
2011-12-13 07:50:54 +00:00
*
* @throws RuntimeException when there is a reference to an abstract definition.
2011-02-13 18:06:41 +00:00
*/
private function validateReferences(array $arguments)
2011-01-26 23:14:31 +00:00
{
foreach ($arguments as $argument) {
if (is_array($argument)) {
$this->validateReferences($argument);
} elseif ($argument instanceof ArgumentInterface) {
$this->validateReferences($argument->getValues());
2011-01-26 23:14:31 +00:00
} elseif ($argument instanceof Reference) {
$targetDefinition = $this->getDefinition((string) $argument);
if (null !== $targetDefinition && $targetDefinition->isAbstract()) {
throw new RuntimeException(sprintf(
2011-01-26 23:14:31 +00:00
'The definition "%s" has a reference to an abstract definition "%s". '
.'Abstract definitions cannot be the target of references.',
$this->currentId,
$argument
));
}
}
}
}
2011-02-13 18:06:41 +00:00
/**
* Returns the Definition given an id.
*
* @param string $id Definition identifier
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return Definition
*/
private function getDefinition($id)
2011-01-26 23:14:31 +00:00
{
if (!$this->container->hasDefinition($id)) {
2014-04-16 08:15:58 +01:00
return;
2011-01-26 23:14:31 +00:00
}
return $this->container->getDefinition($id);
}
}