4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\DependencyInjection\Definition;
17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18 use Symfony\Component\DependencyInjection\Reference;
21 * @author Nicolas Grekas <p@tchwork.com>
23 abstract class AbstractRecursivePass implements CompilerPassInterface
26 * @var ContainerBuilder
34 public function process(ContainerBuilder $container)
36 $this->container = $container;
39 $this->processValue($container->getDefinitions(), true);
41 $this->container = null;
46 * Processes a value found in a definition tree.
51 * @return mixed The processed value
53 protected function processValue($value, $isRoot = false)
55 if (\is_array($value)) {
56 foreach ($value as $k => $v) {
58 $this->currentId = $k;
60 if ($v !== $processedValue = $this->processValue($v, $isRoot)) {
61 $value[$k] = $processedValue;
64 } elseif ($value instanceof ArgumentInterface) {
65 $value->setValues($this->processValue($value->getValues()));
66 } elseif ($value instanceof Definition) {
67 $value->setArguments($this->processValue($value->getArguments()));
68 $value->setProperties($this->processValue($value->getProperties()));
69 $value->setMethodCalls($this->processValue($value->getMethodCalls()));
71 $changes = $value->getChanges();
72 if (isset($changes['factory'])) {
73 $value->setFactory($this->processValue($value->getFactory()));
75 if (isset($changes['configurator'])) {
76 $value->setConfigurator($this->processValue($value->getConfigurator()));
84 * @param Definition $definition
85 * @param bool $required
87 * @return \ReflectionFunctionAbstract|null
89 * @throws RuntimeException
91 protected function getConstructor(Definition $definition, $required)
93 if (is_string($factory = $definition->getFactory())) {
94 if (!function_exists($factory)) {
95 throw new RuntimeException(sprintf('Invalid service "%s": function "%s" does not exist.', $this->currentId, $factory));
97 $r = new \ReflectionFunction($factory);
98 if (false !== $r->getFileName() && file_exists($r->getFileName())) {
99 $this->container->fileExists($r->getFileName());
106 list($class, $method) = $factory;
107 if ($class instanceof Reference) {
108 $class = $this->container->findDefinition((string) $class)->getClass();
109 } elseif (null === $class) {
110 $class = $definition->getClass();
112 if ('__construct' === $method) {
113 throw new RuntimeException(sprintf('Invalid service "%s": "__construct()" cannot be used as a factory method.', $this->currentId));
116 return $this->getReflectionMethod(new Definition($class), $method);
119 $class = $definition->getClass();
121 if (!$r = $this->container->getReflectionClass($class)) {
122 throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
124 if (!$r = $r->getConstructor()) {
126 throw new RuntimeException(sprintf('Invalid service "%s": class%s has no constructor.', $this->currentId, sprintf($class !== $this->currentId ? ' "%s"' : '', $class)));
128 } elseif (!$r->isPublic()) {
129 throw new RuntimeException(sprintf('Invalid service "%s": %s must be public.', $this->currentId, sprintf($class !== $this->currentId ? 'constructor of class "%s"' : 'its constructor', $class)));
136 * @param Definition $definition
137 * @param string $method
139 * @throws RuntimeException
141 * @return \ReflectionFunctionAbstract
143 protected function getReflectionMethod(Definition $definition, $method)
145 if ('__construct' === $method) {
146 return $this->getConstructor($definition, true);
149 if (!$class = $definition->getClass()) {
150 throw new RuntimeException(sprintf('Invalid service "%s": the class is not set.', $this->currentId));
153 if (!$r = $this->container->getReflectionClass($class)) {
154 throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
157 if (!$r->hasMethod($method)) {
158 throw new RuntimeException(sprintf('Invalid service "%s": method "%s()" does not exist.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
161 $r = $r->getMethod($method);
162 if (!$r->isPublic()) {
163 throw new RuntimeException(sprintf('Invalid service "%s": method "%s()" must be public.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));