comparison vendor/symfony/dependency-injection/Compiler/MergeExtensionConfigurationPass.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
10 */ 10 */
11 11
12 namespace Symfony\Component\DependencyInjection\Compiler; 12 namespace Symfony\Component\DependencyInjection\Compiler;
13 13
14 use Symfony\Component\DependencyInjection\ContainerBuilder; 14 use Symfony\Component\DependencyInjection\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Exception\LogicException;
16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
15 use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface; 17 use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
18 use Symfony\Component\DependencyInjection\Extension\Extension;
19 use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
16 use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; 20 use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
21 use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
22 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
17 23
18 /** 24 /**
19 * Merges extension configs into the container builder. 25 * Merges extension configs into the container builder.
20 * 26 *
21 * @author Fabien Potencier <fabien@symfony.com> 27 * @author Fabien Potencier <fabien@symfony.com>
41 foreach ($container->getExtensions() as $name => $extension) { 47 foreach ($container->getExtensions() as $name => $extension) {
42 if (!$config = $container->getExtensionConfig($name)) { 48 if (!$config = $container->getExtensionConfig($name)) {
43 // this extension was not called 49 // this extension was not called
44 continue; 50 continue;
45 } 51 }
46 $config = $container->getParameterBag()->resolveValue($config); 52 $resolvingBag = $container->getParameterBag();
47 53 if ($resolvingBag instanceof EnvPlaceholderParameterBag && $extension instanceof Extension) {
48 $tmpContainer = new ContainerBuilder($container->getParameterBag()); 54 // create a dedicated bag so that we can track env vars per-extension
49 $tmpContainer->setResourceTracking($container->isTrackingResources()); 55 $resolvingBag = new MergeExtensionConfigurationParameterBag($resolvingBag);
50 $tmpContainer->addObjectResource($extension); 56 }
51 if ($extension instanceof ConfigurationExtensionInterface && null !== $configuration = $extension->getConfiguration($config, $tmpContainer)) { 57 $config = $resolvingBag->resolveValue($config);
52 $tmpContainer->addObjectResource($configuration); 58
53 } 59 try {
54 60 $tmpContainer = new MergeExtensionConfigurationContainerBuilder($extension, $resolvingBag);
55 foreach ($exprLangProviders as $provider) { 61 $tmpContainer->setResourceTracking($container->isTrackingResources());
56 $tmpContainer->addExpressionLanguageProvider($provider); 62 $tmpContainer->addObjectResource($extension);
57 } 63 if ($extension instanceof ConfigurationExtensionInterface && null !== $configuration = $extension->getConfiguration($config, $tmpContainer)) {
58 64 $tmpContainer->addObjectResource($configuration);
59 $extension->load($config, $tmpContainer); 65 }
66
67 foreach ($exprLangProviders as $provider) {
68 $tmpContainer->addExpressionLanguageProvider($provider);
69 }
70
71 $extension->load($config, $tmpContainer);
72 } catch (\Exception $e) {
73 if ($resolvingBag instanceof MergeExtensionConfigurationParameterBag) {
74 $container->getParameterBag()->mergeEnvPlaceholders($resolvingBag);
75 }
76
77 throw $e;
78 }
79
80 if ($resolvingBag instanceof MergeExtensionConfigurationParameterBag) {
81 // don't keep track of env vars that are *overridden* when configs are merged
82 $resolvingBag->freezeAfterProcessing($extension, $tmpContainer);
83 }
60 84
61 $container->merge($tmpContainer); 85 $container->merge($tmpContainer);
62 $container->getParameterBag()->add($parameters); 86 $container->getParameterBag()->add($parameters);
63 } 87 }
64 88
65 $container->addDefinitions($definitions); 89 $container->addDefinitions($definitions);
66 $container->addAliases($aliases); 90 $container->addAliases($aliases);
67 } 91 }
68 } 92 }
93
94 /**
95 * @internal
96 */
97 class MergeExtensionConfigurationParameterBag extends EnvPlaceholderParameterBag
98 {
99 private $processedEnvPlaceholders;
100
101 public function __construct(parent $parameterBag)
102 {
103 parent::__construct($parameterBag->all());
104 $this->mergeEnvPlaceholders($parameterBag);
105 }
106
107 public function freezeAfterProcessing(Extension $extension, ContainerBuilder $container)
108 {
109 if (!$config = $extension->getProcessedConfigs()) {
110 // Extension::processConfiguration() wasn't called, we cannot know how configs were merged
111 return;
112 }
113 $this->processedEnvPlaceholders = array();
114
115 // serialize config and container to catch env vars nested in object graphs
116 $config = serialize($config).serialize($container->getDefinitions()).serialize($container->getAliases()).serialize($container->getParameterBag()->all());
117
118 foreach (parent::getEnvPlaceholders() as $env => $placeholders) {
119 foreach ($placeholders as $placeholder) {
120 if (false !== stripos($config, $placeholder)) {
121 $this->processedEnvPlaceholders[$env] = $placeholders;
122 break;
123 }
124 }
125 }
126 }
127
128 /**
129 * {@inheritdoc}
130 */
131 public function getEnvPlaceholders()
132 {
133 return null !== $this->processedEnvPlaceholders ? $this->processedEnvPlaceholders : parent::getEnvPlaceholders();
134 }
135 }
136
137 /**
138 * A container builder preventing using methods that wouldn't have any effect from extensions.
139 *
140 * @internal
141 */
142 class MergeExtensionConfigurationContainerBuilder extends ContainerBuilder
143 {
144 private $extensionClass;
145
146 public function __construct(ExtensionInterface $extension, ParameterBagInterface $parameterBag = null)
147 {
148 parent::__construct($parameterBag);
149
150 $this->extensionClass = get_class($extension);
151 }
152
153 /**
154 * {@inheritdoc}
155 */
156 public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION/*, int $priority = 0*/)
157 {
158 throw new LogicException(sprintf('You cannot add compiler pass "%s" from extension "%s". Compiler passes must be registered before the container is compiled.', get_class($pass), $this->extensionClass));
159 }
160
161 /**
162 * {@inheritdoc}
163 */
164 public function registerExtension(ExtensionInterface $extension)
165 {
166 throw new LogicException(sprintf('You cannot register extension "%s" from "%s". Extensions must be registered before the container is compiled.', get_class($extension), $this->extensionClass));
167 }
168
169 /**
170 * {@inheritdoc}
171 */
172 public function compile($resolveEnvPlaceholders = false)
173 {
174 throw new LogicException(sprintf('Cannot compile the container in extension "%s".', $this->extensionClass));
175 }
176
177 /**
178 * {@inheritdoc}
179 */
180 public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs = null)
181 {
182 if (true !== $format || !\is_string($value)) {
183 return parent::resolveEnvPlaceholders($value, $format, $usedEnvs);
184 }
185
186 $bag = $this->getParameterBag();
187 $value = $bag->resolveValue($value);
188
189 foreach ($bag->getEnvPlaceholders() as $env => $placeholders) {
190 if (false === strpos($env, ':')) {
191 continue;
192 }
193 foreach ($placeholders as $placeholder) {
194 if (false !== stripos($value, $placeholder)) {
195 throw new RuntimeException(sprintf('Using a cast in "env(%s)" is incompatible with resolution at compile time in "%s". The logic in the extension should be moved to a compiler pass, or an env parameter with no cast should be used instead.', $env, $this->extensionClass));
196 }
197 }
198 }
199
200 return parent::resolveEnvPlaceholders($value, $format, $usedEnvs);
201 }
202 }