2011-04-14 23:11:50 +01: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-04-14 23:11:50 +01:00
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
|
|
|
|
2011-04-15 07:47:28 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Definition;
|
2011-05-17 15:26:08 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
2011-04-14 23:11:50 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that all references are pointing to a valid service.
|
|
|
|
*
|
|
|
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
|
|
|
*/
|
|
|
|
class CheckExceptionOnInvalidReferenceBehaviorPass implements CompilerPassInterface
|
|
|
|
{
|
|
|
|
private $container;
|
|
|
|
private $sourceId;
|
|
|
|
|
|
|
|
public function process(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
|
|
|
|
foreach ($container->getDefinitions() as $id => $definition) {
|
|
|
|
$this->sourceId = $id;
|
2011-04-15 07:47:28 +01:00
|
|
|
$this->processDefinition($definition);
|
2011-04-14 23:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-15 07:47:28 +01:00
|
|
|
private function processDefinition(Definition $definition)
|
|
|
|
{
|
|
|
|
$this->processReferences($definition->getArguments());
|
|
|
|
$this->processReferences($definition->getMethodCalls());
|
|
|
|
$this->processReferences($definition->getProperties());
|
|
|
|
}
|
|
|
|
|
2011-04-14 23:11:50 +01:00
|
|
|
private function processReferences(array $arguments)
|
|
|
|
{
|
|
|
|
foreach ($arguments as $argument) {
|
|
|
|
if (is_array($argument)) {
|
|
|
|
$this->processReferences($argument);
|
2011-12-18 13:42:59 +00:00
|
|
|
} elseif ($argument instanceof Definition) {
|
2011-04-15 07:47:28 +01:00
|
|
|
$this->processDefinition($argument);
|
2011-12-18 13:42:59 +00:00
|
|
|
} elseif ($argument instanceof Reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $argument->getInvalidBehavior()) {
|
2011-04-14 23:11:50 +01:00
|
|
|
$destId = (string) $argument;
|
|
|
|
|
|
|
|
if (!$this->container->has($destId)) {
|
2011-05-17 15:26:08 +01:00
|
|
|
throw new ServiceNotFoundException($destId, $this->sourceId);
|
2011-04-14 23:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|