annotate vendor/symfony/dependency-injection/Compiler/AnalyzeServiceReferencesPass.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\DependencyInjection\Compiler;
Chris@0 13
Chris@0 14 use Symfony\Component\DependencyInjection\Definition;
Chris@12 15 use Symfony\Component\DependencyInjection\ExpressionLanguage;
Chris@0 16 use Symfony\Component\DependencyInjection\Reference;
Chris@0 17 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@12 18 use Symfony\Component\ExpressionLanguage\Expression;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Run this pass before passes that need to know more about the relation of
Chris@0 22 * your services.
Chris@0 23 *
Chris@0 24 * This class will populate the ServiceReferenceGraph with information. You can
Chris@0 25 * retrieve the graph in other passes from the compiler.
Chris@0 26 *
Chris@0 27 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
Chris@0 28 */
Chris@0 29 class AnalyzeServiceReferencesPass implements RepeatablePassInterface
Chris@0 30 {
Chris@0 31 private $graph;
Chris@0 32 private $container;
Chris@0 33 private $currentId;
Chris@0 34 private $currentDefinition;
Chris@0 35 private $repeatedPass;
Chris@0 36 private $onlyConstructorArguments;
Chris@12 37 private $expressionLanguage;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
Chris@0 41 */
Chris@0 42 public function __construct($onlyConstructorArguments = false)
Chris@0 43 {
Chris@0 44 $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * {@inheritdoc}
Chris@0 49 */
Chris@0 50 public function setRepeatedPass(RepeatedPass $repeatedPass)
Chris@0 51 {
Chris@0 52 $this->repeatedPass = $repeatedPass;
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * Processes a ContainerBuilder object to populate the service reference graph.
Chris@0 57 *
Chris@0 58 * @param ContainerBuilder $container
Chris@0 59 */
Chris@0 60 public function process(ContainerBuilder $container)
Chris@0 61 {
Chris@0 62 $this->container = $container;
Chris@0 63 $this->graph = $container->getCompiler()->getServiceReferenceGraph();
Chris@0 64 $this->graph->clear();
Chris@0 65
Chris@0 66 foreach ($container->getDefinitions() as $id => $definition) {
Chris@0 67 if ($definition->isSynthetic() || $definition->isAbstract()) {
Chris@0 68 continue;
Chris@0 69 }
Chris@0 70
Chris@0 71 $this->currentId = $id;
Chris@0 72 $this->currentDefinition = $definition;
Chris@0 73
Chris@0 74 $this->processArguments($definition->getArguments());
Chris@0 75 if (is_array($definition->getFactory())) {
Chris@0 76 $this->processArguments($definition->getFactory());
Chris@0 77 }
Chris@0 78
Chris@0 79 if (!$this->onlyConstructorArguments) {
Chris@0 80 $this->processArguments($definition->getMethodCalls());
Chris@0 81 $this->processArguments($definition->getProperties());
Chris@0 82 if ($definition->getConfigurator()) {
Chris@0 83 $this->processArguments(array($definition->getConfigurator()));
Chris@0 84 }
Chris@0 85 }
Chris@0 86 }
Chris@0 87
Chris@0 88 foreach ($container->getAliases() as $id => $alias) {
Chris@0 89 $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
Chris@0 90 }
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * Processes service definitions for arguments to find relationships for the service graph.
Chris@0 95 *
Chris@0 96 * @param array $arguments An array of Reference or Definition objects relating to service definitions
Chris@0 97 */
Chris@0 98 private function processArguments(array $arguments)
Chris@0 99 {
Chris@0 100 foreach ($arguments as $argument) {
Chris@0 101 if (is_array($argument)) {
Chris@0 102 $this->processArguments($argument);
Chris@12 103 } elseif ($argument instanceof Expression) {
Chris@12 104 $this->getExpressionLanguage()->compile((string) $argument, array('this' => 'container'));
Chris@0 105 } elseif ($argument instanceof Reference) {
Chris@0 106 $this->graph->connect(
Chris@0 107 $this->currentId,
Chris@0 108 $this->currentDefinition,
Chris@0 109 $this->getDefinitionId((string) $argument),
Chris@0 110 $this->getDefinition((string) $argument),
Chris@0 111 $argument
Chris@0 112 );
Chris@0 113 } elseif ($argument instanceof Definition) {
Chris@0 114 $this->processArguments($argument->getArguments());
Chris@0 115 $this->processArguments($argument->getMethodCalls());
Chris@0 116 $this->processArguments($argument->getProperties());
Chris@0 117
Chris@0 118 if (is_array($argument->getFactory())) {
Chris@0 119 $this->processArguments($argument->getFactory());
Chris@0 120 }
Chris@0 121 }
Chris@0 122 }
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * Returns a service definition given the full name or an alias.
Chris@0 127 *
Chris@0 128 * @param string $id A full id or alias for a service definition
Chris@0 129 *
Chris@0 130 * @return Definition|null The definition related to the supplied id
Chris@0 131 */
Chris@0 132 private function getDefinition($id)
Chris@0 133 {
Chris@0 134 $id = $this->getDefinitionId($id);
Chris@0 135
Chris@0 136 return null === $id ? null : $this->container->getDefinition($id);
Chris@0 137 }
Chris@0 138
Chris@0 139 private function getDefinitionId($id)
Chris@0 140 {
Chris@0 141 while ($this->container->hasAlias($id)) {
Chris@0 142 $id = (string) $this->container->getAlias($id);
Chris@0 143 }
Chris@0 144
Chris@0 145 if (!$this->container->hasDefinition($id)) {
Chris@0 146 return;
Chris@0 147 }
Chris@0 148
Chris@0 149 return $id;
Chris@0 150 }
Chris@12 151
Chris@12 152 private function getExpressionLanguage()
Chris@12 153 {
Chris@12 154 if (null === $this->expressionLanguage) {
Chris@12 155 $providers = $this->container->getExpressionLanguageProviders();
Chris@12 156 $this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
Chris@12 157 if ('""' === substr_replace($arg, '', 1, -1)) {
Chris@12 158 $id = stripcslashes(substr($arg, 1, -1));
Chris@12 159
Chris@12 160 $this->graph->connect(
Chris@12 161 $this->currentId,
Chris@12 162 $this->currentDefinition,
Chris@12 163 $this->getDefinitionId($id),
Chris@12 164 $this->getDefinition($id)
Chris@12 165 );
Chris@12 166 }
Chris@12 167
Chris@12 168 return sprintf('$this->get(%s)', $arg);
Chris@12 169 });
Chris@12 170 }
Chris@12 171
Chris@12 172 return $this->expressionLanguage;
Chris@12 173 }
Chris@0 174 }