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/ResolveInvalidReferencesPass.php

116 lines
3.6 KiB
PHP
Raw Normal View History

2011-01-05 11:13:27 +00:00
<?php
/*
* This file is part of the Symfony package.
2011-01-05 11:13:27 +00:00
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2011-01-05 11:13:27 +00:00
*/
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
2011-01-05 11:13:27 +00:00
/**
* Emulates the invalid behavior if the reference is not found within the
* container.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ResolveInvalidReferencesPass implements CompilerPassInterface
{
protected $container;
protected $exceptions;
2011-02-13 18:06:41 +00:00
/**
* Constructor.
*
* @param array $exceptions An array of exceptions
*/
2011-01-05 11:13:27 +00:00
public function __construct(array $exceptions = array('kernel', 'service_container', 'templating.loader.wrapped', 'pdo_connection'))
{
$this->exceptions = $exceptions;
}
2011-02-13 18:06:41 +00:00
/**
* Add an exception.
*
* @param string $id Exception identifier
*/
2011-01-05 11:13:27 +00:00
public function addException($id)
{
$this->exceptions[] = $id;
}
2011-02-13 18:06:41 +00:00
/**
* Process the ContainerBuilder to resolve invalid references.
*
* @param ContainerBuilder $container
*/
2011-01-05 11:13:27 +00:00
public function process(ContainerBuilder $container)
{
$this->container = $container;
foreach ($container->getDefinitions() as $definition) {
2011-01-26 23:14:31 +00:00
if ($definition->isSynthetic() || $definition->isAbstract()) {
continue;
}
2011-01-05 11:13:27 +00:00
$definition->setArguments(
$this->processArguments($definition->getArguments())
);
$calls = array();
foreach ($definition->getMethodCalls() as $call) {
2011-01-05 11:13:27 +00:00
try {
$calls[] = array($call[0], $this->processArguments($call[1], true));
2011-01-05 11:13:27 +00:00
} catch (\RuntimeException $ignore) {
// this call is simply removed
}
}
$definition->setMethodCalls($calls);
}
}
2011-02-13 18:06:41 +00:00
/**
* Processes arguments to determin invalid references.
*
* @param array $arguments An array of Reference objects
* @param boolean $inMethodCall
*/
2011-01-05 11:13:27 +00:00
protected function processArguments(array $arguments, $inMethodCall = false)
{
foreach ($arguments as $k => $argument) {
if (is_array($argument)) {
$arguments[$k] = $this->processArguments($argument, $inMethodCall);
} else if ($argument instanceof Reference) {
$id = (string) $argument;
if (in_array($id, $this->exceptions, true)) {
continue;
}
$invalidBehavior = $argument->getInvalidBehavior();
$exists = $this->container->has($id);
2011-01-26 23:14:31 +00:00
// resolve invalid behavior
2011-01-05 11:13:27 +00:00
if ($exists && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
$arguments[$k] = new Reference($id);
} else if (!$exists && ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
$arguments[$k] = null;
} else if (!$exists && ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
if ($inMethodCall) {
throw new \RuntimeException('Method shouldn\'t be called.');
}
$arguments[$k] = null;
}
}
}
return $arguments;
}
}