annotate vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 /*
Chris@14 4 * This file is part of the Symfony package.
Chris@14 5 *
Chris@14 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@14 7 *
Chris@14 8 * For the full copyright and license information, please view the LICENSE
Chris@14 9 * file that was distributed with this source code.
Chris@14 10 */
Chris@14 11
Chris@14 12 namespace Symfony\Component\DependencyInjection\Compiler;
Chris@14 13
Chris@14 14 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
Chris@14 15 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@14 16 use Symfony\Component\DependencyInjection\Definition;
Chris@14 17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
Chris@14 18 use Symfony\Component\DependencyInjection\Reference;
Chris@14 19
Chris@14 20 /**
Chris@14 21 * @author Nicolas Grekas <p@tchwork.com>
Chris@14 22 */
Chris@14 23 abstract class AbstractRecursivePass implements CompilerPassInterface
Chris@14 24 {
Chris@14 25 /**
Chris@14 26 * @var ContainerBuilder
Chris@14 27 */
Chris@14 28 protected $container;
Chris@14 29 protected $currentId;
Chris@14 30
Chris@14 31 /**
Chris@14 32 * {@inheritdoc}
Chris@14 33 */
Chris@14 34 public function process(ContainerBuilder $container)
Chris@14 35 {
Chris@14 36 $this->container = $container;
Chris@14 37
Chris@14 38 try {
Chris@14 39 $this->processValue($container->getDefinitions(), true);
Chris@14 40 } finally {
Chris@14 41 $this->container = null;
Chris@14 42 }
Chris@14 43 }
Chris@14 44
Chris@14 45 /**
Chris@14 46 * Processes a value found in a definition tree.
Chris@14 47 *
Chris@14 48 * @param mixed $value
Chris@14 49 * @param bool $isRoot
Chris@14 50 *
Chris@14 51 * @return mixed The processed value
Chris@14 52 */
Chris@14 53 protected function processValue($value, $isRoot = false)
Chris@14 54 {
Chris@14 55 if (\is_array($value)) {
Chris@14 56 foreach ($value as $k => $v) {
Chris@14 57 if ($isRoot) {
Chris@14 58 $this->currentId = $k;
Chris@14 59 }
Chris@14 60 if ($v !== $processedValue = $this->processValue($v, $isRoot)) {
Chris@14 61 $value[$k] = $processedValue;
Chris@14 62 }
Chris@14 63 }
Chris@14 64 } elseif ($value instanceof ArgumentInterface) {
Chris@14 65 $value->setValues($this->processValue($value->getValues()));
Chris@14 66 } elseif ($value instanceof Definition) {
Chris@14 67 $value->setArguments($this->processValue($value->getArguments()));
Chris@14 68 $value->setProperties($this->processValue($value->getProperties()));
Chris@14 69 $value->setMethodCalls($this->processValue($value->getMethodCalls()));
Chris@14 70
Chris@14 71 $changes = $value->getChanges();
Chris@14 72 if (isset($changes['factory'])) {
Chris@14 73 $value->setFactory($this->processValue($value->getFactory()));
Chris@14 74 }
Chris@14 75 if (isset($changes['configurator'])) {
Chris@14 76 $value->setConfigurator($this->processValue($value->getConfigurator()));
Chris@14 77 }
Chris@14 78 }
Chris@14 79
Chris@14 80 return $value;
Chris@14 81 }
Chris@14 82
Chris@14 83 /**
Chris@14 84 * @param Definition $definition
Chris@14 85 * @param bool $required
Chris@14 86 *
Chris@14 87 * @return \ReflectionFunctionAbstract|null
Chris@14 88 *
Chris@14 89 * @throws RuntimeException
Chris@14 90 */
Chris@14 91 protected function getConstructor(Definition $definition, $required)
Chris@14 92 {
Chris@17 93 if (\is_string($factory = $definition->getFactory())) {
Chris@17 94 if (!\function_exists($factory)) {
Chris@14 95 throw new RuntimeException(sprintf('Invalid service "%s": function "%s" does not exist.', $this->currentId, $factory));
Chris@14 96 }
Chris@14 97 $r = new \ReflectionFunction($factory);
Chris@14 98 if (false !== $r->getFileName() && file_exists($r->getFileName())) {
Chris@14 99 $this->container->fileExists($r->getFileName());
Chris@14 100 }
Chris@14 101
Chris@14 102 return $r;
Chris@14 103 }
Chris@14 104
Chris@14 105 if ($factory) {
Chris@14 106 list($class, $method) = $factory;
Chris@14 107 if ($class instanceof Reference) {
Chris@14 108 $class = $this->container->findDefinition((string) $class)->getClass();
Chris@14 109 } elseif (null === $class) {
Chris@14 110 $class = $definition->getClass();
Chris@14 111 }
Chris@14 112 if ('__construct' === $method) {
Chris@14 113 throw new RuntimeException(sprintf('Invalid service "%s": "__construct()" cannot be used as a factory method.', $this->currentId));
Chris@14 114 }
Chris@14 115
Chris@14 116 return $this->getReflectionMethod(new Definition($class), $method);
Chris@14 117 }
Chris@14 118
Chris@14 119 $class = $definition->getClass();
Chris@14 120
Chris@16 121 try {
Chris@16 122 if (!$r = $this->container->getReflectionClass($class)) {
Chris@16 123 throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
Chris@16 124 }
Chris@16 125 } catch (\ReflectionException $e) {
Chris@16 126 throw new RuntimeException(sprintf('Invalid service "%s": %s.', $this->currentId, lcfirst(rtrim($e->getMessage(), '.'))));
Chris@14 127 }
Chris@14 128 if (!$r = $r->getConstructor()) {
Chris@14 129 if ($required) {
Chris@14 130 throw new RuntimeException(sprintf('Invalid service "%s": class%s has no constructor.', $this->currentId, sprintf($class !== $this->currentId ? ' "%s"' : '', $class)));
Chris@14 131 }
Chris@14 132 } elseif (!$r->isPublic()) {
Chris@14 133 throw new RuntimeException(sprintf('Invalid service "%s": %s must be public.', $this->currentId, sprintf($class !== $this->currentId ? 'constructor of class "%s"' : 'its constructor', $class)));
Chris@14 134 }
Chris@14 135
Chris@14 136 return $r;
Chris@14 137 }
Chris@14 138
Chris@14 139 /**
Chris@14 140 * @param Definition $definition
Chris@14 141 * @param string $method
Chris@14 142 *
Chris@14 143 * @throws RuntimeException
Chris@14 144 *
Chris@14 145 * @return \ReflectionFunctionAbstract
Chris@14 146 */
Chris@14 147 protected function getReflectionMethod(Definition $definition, $method)
Chris@14 148 {
Chris@14 149 if ('__construct' === $method) {
Chris@14 150 return $this->getConstructor($definition, true);
Chris@14 151 }
Chris@14 152
Chris@14 153 if (!$class = $definition->getClass()) {
Chris@14 154 throw new RuntimeException(sprintf('Invalid service "%s": the class is not set.', $this->currentId));
Chris@14 155 }
Chris@14 156
Chris@14 157 if (!$r = $this->container->getReflectionClass($class)) {
Chris@14 158 throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
Chris@14 159 }
Chris@14 160
Chris@14 161 if (!$r->hasMethod($method)) {
Chris@14 162 throw new RuntimeException(sprintf('Invalid service "%s": method "%s()" does not exist.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
Chris@14 163 }
Chris@14 164
Chris@14 165 $r = $r->getMethod($method);
Chris@14 166 if (!$r->isPublic()) {
Chris@14 167 throw new RuntimeException(sprintf('Invalid service "%s": method "%s()" must be public.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
Chris@14 168 }
Chris@14 169
Chris@14 170 return $r;
Chris@14 171 }
Chris@14 172 }