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

103 lines
3.4 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@symfony.com>
2011-01-05 11:13:27 +00:00
*
* 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
{
private $container;
2011-01-05 11:13:27 +00:00
2011-02-13 18:06:41 +00:00
/**
* Process the ContainerBuilder to resolve invalid references.
*
* @param ContainerBuilder $container
2011-02-13 18:06:41 +00:00
*/
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);
$properties = array();
foreach ($definition->getProperties() as $name => $value) {
try {
$value = $this->processArguments(array($value), true);
$properties[$name] = reset($value);
} catch (\RuntimeException $ignore) {
// ignore property
}
}
$definition->setProperties($properties);
2011-01-05 11:13:27 +00:00
}
}
2011-02-13 18:06:41 +00:00
/**
2011-03-01 17:48:35 +00:00
* Processes arguments to determine invalid references.
2011-02-13 18:06:41 +00:00
*
2011-05-30 00:25:35 +01:00
* @param array $arguments An array of Reference objects
2011-04-27 06:25:26 +01:00
* @param Boolean $inMethodCall
2011-02-13 18:06:41 +00:00
*/
private function processArguments(array $arguments, $inMethodCall = false)
2011-01-05 11:13:27 +00:00
{
foreach ($arguments as $k => $argument) {
if (is_array($argument)) {
$arguments[$k] = $this->processArguments($argument, $inMethodCall);
2011-12-18 13:42:59 +00:00
} elseif ($argument instanceof Reference) {
2011-01-05 11:13:27 +00:00
$id = (string) $argument;
$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);
2011-12-18 13:42:59 +00:00
} elseif (!$exists && ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
2011-01-05 11:13:27 +00:00
$arguments[$k] = null;
2011-12-18 13:42:59 +00:00
} elseif (!$exists && ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
2011-01-05 11:13:27 +00:00
if ($inMethodCall) {
throw new \RuntimeException('Method shouldn\'t be called.');
}
$arguments[$k] = null;
}
}
}
return $arguments;
}
}