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\Dumper;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
|
Chris@0
|
17 use Symfony\Component\DependencyInjection\Variable;
|
Chris@0
|
18 use Symfony\Component\DependencyInjection\Definition;
|
Chris@0
|
19 use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
|
Chris@0
|
20 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@0
|
21 use Symfony\Component\DependencyInjection\Container;
|
Chris@0
|
22 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
23 use Symfony\Component\DependencyInjection\Reference;
|
Chris@0
|
24 use Symfony\Component\DependencyInjection\TypedReference;
|
Chris@0
|
25 use Symfony\Component\DependencyInjection\Parameter;
|
Chris@0
|
26 use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
|
Chris@0
|
27 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
28 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
29 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
Chris@0
|
30 use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface as ProxyDumper;
|
Chris@0
|
31 use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\NullDumper;
|
Chris@0
|
32 use Symfony\Component\DependencyInjection\ExpressionLanguage;
|
Chris@0
|
33 use Symfony\Component\ExpressionLanguage\Expression;
|
Chris@0
|
34 use Symfony\Component\HttpKernel\Kernel;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * PhpDumper dumps a service container as a PHP class.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
40 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
Chris@0
|
41 */
|
Chris@0
|
42 class PhpDumper extends Dumper
|
Chris@0
|
43 {
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Characters that might appear in the generated variable name as first character.
|
Chris@0
|
46 */
|
Chris@0
|
47 const FIRST_CHARS = 'abcdefghijklmnopqrstuvwxyz';
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Characters that might appear in the generated variable name as any but the first character.
|
Chris@0
|
51 */
|
Chris@0
|
52 const NON_FIRST_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789_';
|
Chris@0
|
53
|
Chris@0
|
54 private $definitionVariables;
|
Chris@0
|
55 private $referenceVariables;
|
Chris@0
|
56 private $variableCount;
|
Chris@0
|
57 private $reservedVariables = array('instance', 'class');
|
Chris@0
|
58 private $expressionLanguage;
|
Chris@0
|
59 private $targetDirRegex;
|
Chris@0
|
60 private $targetDirMaxMatches;
|
Chris@0
|
61 private $docStar;
|
Chris@0
|
62 private $serviceIdToMethodNameMap;
|
Chris@0
|
63 private $usedMethodNames;
|
Chris@0
|
64 private $namespace;
|
Chris@0
|
65 private $asFiles;
|
Chris@0
|
66 private $hotPathTag;
|
Chris@0
|
67 private $inlineRequires;
|
Chris@0
|
68 private $inlinedRequires = array();
|
Chris@0
|
69 private $circularReferences = array();
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * @var ProxyDumper
|
Chris@0
|
73 */
|
Chris@0
|
74 private $proxyDumper;
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * {@inheritdoc}
|
Chris@0
|
78 */
|
Chris@0
|
79 public function __construct(ContainerBuilder $container)
|
Chris@0
|
80 {
|
Chris@0
|
81 if (!$container->isCompiled()) {
|
Chris@0
|
82 @trigger_error('Dumping an uncompiled ContainerBuilder is deprecated since Symfony 3.3 and will not be supported anymore in 4.0. Compile the container beforehand.', E_USER_DEPRECATED);
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 parent::__construct($container);
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * Sets the dumper to be used when dumping proxies in the generated container.
|
Chris@0
|
90 */
|
Chris@0
|
91 public function setProxyDumper(ProxyDumper $proxyDumper)
|
Chris@0
|
92 {
|
Chris@0
|
93 $this->proxyDumper = $proxyDumper;
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * Dumps the service container as a PHP class.
|
Chris@0
|
98 *
|
Chris@0
|
99 * Available options:
|
Chris@0
|
100 *
|
Chris@0
|
101 * * class: The class name
|
Chris@0
|
102 * * base_class: The base class name
|
Chris@0
|
103 * * namespace: The class namespace
|
Chris@0
|
104 * * as_files: To split the container in several files
|
Chris@0
|
105 *
|
Chris@0
|
106 * @return string|array A PHP class representing the service container or an array of PHP files if the "as_files" option is set
|
Chris@0
|
107 *
|
Chris@0
|
108 * @throws EnvParameterException When an env var exists but has not been dumped
|
Chris@0
|
109 */
|
Chris@0
|
110 public function dump(array $options = array())
|
Chris@0
|
111 {
|
Chris@0
|
112 $this->targetDirRegex = null;
|
Chris@0
|
113 $this->inlinedRequires = array();
|
Chris@0
|
114 $options = array_merge(array(
|
Chris@0
|
115 'class' => 'ProjectServiceContainer',
|
Chris@0
|
116 'base_class' => 'Container',
|
Chris@0
|
117 'namespace' => '',
|
Chris@0
|
118 'as_files' => false,
|
Chris@0
|
119 'debug' => true,
|
Chris@0
|
120 'hot_path_tag' => 'container.hot_path',
|
Chris@0
|
121 'inline_class_loader_parameter' => 'container.dumper.inline_class_loader',
|
Chris@0
|
122 'build_time' => time(),
|
Chris@0
|
123 ), $options);
|
Chris@0
|
124
|
Chris@0
|
125 $this->namespace = $options['namespace'];
|
Chris@0
|
126 $this->asFiles = $options['as_files'];
|
Chris@0
|
127 $this->hotPathTag = $options['hot_path_tag'];
|
Chris@0
|
128 $this->inlineRequires = $options['inline_class_loader_parameter'] && $this->container->hasParameter($options['inline_class_loader_parameter']) && $this->container->getParameter($options['inline_class_loader_parameter']);
|
Chris@0
|
129
|
Chris@0
|
130 if (0 !== strpos($baseClass = $options['base_class'], '\\') && 'Container' !== $baseClass) {
|
Chris@0
|
131 $baseClass = sprintf('%s\%s', $options['namespace'] ? '\\'.$options['namespace'] : '', $baseClass);
|
Chris@0
|
132 $baseClassWithNamespace = $baseClass;
|
Chris@0
|
133 } elseif ('Container' === $baseClass) {
|
Chris@0
|
134 $baseClassWithNamespace = Container::class;
|
Chris@0
|
135 } else {
|
Chris@0
|
136 $baseClassWithNamespace = $baseClass;
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 $this->initializeMethodNamesMap('Container' === $baseClass ? Container::class : $baseClass);
|
Chris@0
|
140
|
Chris@0
|
141 (new AnalyzeServiceReferencesPass())->process($this->container);
|
Chris@0
|
142 $this->circularReferences = array();
|
Chris@0
|
143 $checkedNodes = array();
|
Chris@0
|
144 foreach ($this->container->getCompiler()->getServiceReferenceGraph()->getNodes() as $id => $node) {
|
Chris@0
|
145 $currentPath = array($id => $id);
|
Chris@0
|
146 $this->analyzeCircularReferences($node->getOutEdges(), $checkedNodes, $currentPath);
|
Chris@0
|
147 }
|
Chris@0
|
148 $this->container->getCompiler()->getServiceReferenceGraph()->clear();
|
Chris@0
|
149
|
Chris@0
|
150 $this->docStar = $options['debug'] ? '*' : '';
|
Chris@0
|
151
|
Chris@0
|
152 if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) {
|
Chris@0
|
153 // Build a regexp where the first root dirs are mandatory,
|
Chris@0
|
154 // but every other sub-dir is optional up to the full path in $dir
|
Chris@0
|
155 // Mandate at least 2 root dirs and not more that 5 optional dirs.
|
Chris@0
|
156
|
Chris@0
|
157 $dir = explode(DIRECTORY_SEPARATOR, realpath($dir));
|
Chris@0
|
158 $i = count($dir);
|
Chris@0
|
159
|
Chris@0
|
160 if (3 <= $i) {
|
Chris@0
|
161 $regex = '';
|
Chris@0
|
162 $lastOptionalDir = $i > 8 ? $i - 5 : 3;
|
Chris@0
|
163 $this->targetDirMaxMatches = $i - $lastOptionalDir;
|
Chris@0
|
164
|
Chris@0
|
165 while (--$i >= $lastOptionalDir) {
|
Chris@0
|
166 $regex = sprintf('(%s%s)?', preg_quote(DIRECTORY_SEPARATOR.$dir[$i], '#'), $regex);
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 do {
|
Chris@0
|
170 $regex = preg_quote(DIRECTORY_SEPARATOR.$dir[$i], '#').$regex;
|
Chris@0
|
171 } while (0 < --$i);
|
Chris@0
|
172
|
Chris@0
|
173 $this->targetDirRegex = '#'.preg_quote($dir[0], '#').$regex.'#';
|
Chris@0
|
174 }
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 $code =
|
Chris@0
|
178 $this->startClass($options['class'], $baseClass, $baseClassWithNamespace).
|
Chris@0
|
179 $this->addServices().
|
Chris@0
|
180 $this->addDefaultParametersMethod().
|
Chris@0
|
181 $this->endClass()
|
Chris@0
|
182 ;
|
Chris@0
|
183
|
Chris@0
|
184 if ($this->asFiles) {
|
Chris@0
|
185 $fileStart = <<<EOF
|
Chris@0
|
186 <?php
|
Chris@0
|
187
|
Chris@0
|
188 use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
|
Chris@0
|
189
|
Chris@0
|
190 // This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
|
Chris@0
|
191
|
Chris@0
|
192 EOF;
|
Chris@0
|
193 $files = array();
|
Chris@0
|
194
|
Chris@0
|
195 if ($ids = array_keys($this->container->getRemovedIds())) {
|
Chris@0
|
196 sort($ids);
|
Chris@0
|
197 $c = "<?php\n\nreturn array(\n";
|
Chris@0
|
198 foreach ($ids as $id) {
|
Chris@0
|
199 $c .= ' '.$this->doExport($id)." => true,\n";
|
Chris@0
|
200 }
|
Chris@0
|
201 $files['removed-ids.php'] = $c .= ");\n";
|
Chris@0
|
202 }
|
Chris@0
|
203
|
Chris@0
|
204 foreach ($this->generateServiceFiles() as $file => $c) {
|
Chris@0
|
205 $files[$file] = $fileStart.$c;
|
Chris@0
|
206 }
|
Chris@0
|
207 foreach ($this->generateProxyClasses() as $file => $c) {
|
Chris@0
|
208 $files[$file] = "<?php\n".$c;
|
Chris@0
|
209 }
|
Chris@0
|
210 $files[$options['class'].'.php'] = $code;
|
Chris@0
|
211 $hash = ucfirst(strtr(ContainerBuilder::hash($files), '._', 'xx'));
|
Chris@0
|
212 $code = array();
|
Chris@0
|
213
|
Chris@0
|
214 foreach ($files as $file => $c) {
|
Chris@0
|
215 $code["Container{$hash}/{$file}"] = $c;
|
Chris@0
|
216 }
|
Chris@0
|
217 array_pop($code);
|
Chris@0
|
218 $code["Container{$hash}/{$options['class']}.php"] = substr_replace($files[$options['class'].'.php'], "<?php\n\nnamespace Container{$hash};\n", 0, 6);
|
Chris@0
|
219 $namespaceLine = $this->namespace ? "\nnamespace {$this->namespace};\n" : '';
|
Chris@0
|
220 $time = $options['build_time'];
|
Chris@0
|
221 $id = hash('crc32', $hash.$time);
|
Chris@0
|
222
|
Chris@0
|
223 $code[$options['class'].'.php'] = <<<EOF
|
Chris@0
|
224 <?php
|
Chris@0
|
225 {$namespaceLine}
|
Chris@0
|
226 // This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
|
Chris@0
|
227
|
Chris@0
|
228 if (\\class_exists(\\Container{$hash}\\{$options['class']}::class, false)) {
|
Chris@0
|
229 // no-op
|
Chris@0
|
230 } elseif (!include __DIR__.'/Container{$hash}/{$options['class']}.php') {
|
Chris@0
|
231 touch(__DIR__.'/Container{$hash}.legacy');
|
Chris@0
|
232
|
Chris@0
|
233 return;
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 if (!\\class_exists({$options['class']}::class, false)) {
|
Chris@0
|
237 \\class_alias(\\Container{$hash}\\{$options['class']}::class, {$options['class']}::class, false);
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 return new \\Container{$hash}\\{$options['class']}(array(
|
Chris@0
|
241 'container.build_hash' => '$hash',
|
Chris@0
|
242 'container.build_id' => '$id',
|
Chris@0
|
243 'container.build_time' => $time,
|
Chris@0
|
244 ), __DIR__.\\DIRECTORY_SEPARATOR.'Container{$hash}');
|
Chris@0
|
245
|
Chris@0
|
246 EOF;
|
Chris@0
|
247 } else {
|
Chris@0
|
248 foreach ($this->generateProxyClasses() as $c) {
|
Chris@0
|
249 $code .= $c;
|
Chris@0
|
250 }
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 $this->targetDirRegex = null;
|
Chris@0
|
254 $this->inlinedRequires = array();
|
Chris@0
|
255 $this->circularReferences = array();
|
Chris@0
|
256
|
Chris@0
|
257 $unusedEnvs = array();
|
Chris@0
|
258 foreach ($this->container->getEnvCounters() as $env => $use) {
|
Chris@0
|
259 if (!$use) {
|
Chris@0
|
260 $unusedEnvs[] = $env;
|
Chris@0
|
261 }
|
Chris@0
|
262 }
|
Chris@0
|
263 if ($unusedEnvs) {
|
Chris@0
|
264 throw new EnvParameterException($unusedEnvs, null, 'Environment variables "%s" are never used. Please, check your container\'s configuration.');
|
Chris@0
|
265 }
|
Chris@0
|
266
|
Chris@0
|
267 return $code;
|
Chris@0
|
268 }
|
Chris@0
|
269
|
Chris@0
|
270 /**
|
Chris@0
|
271 * Retrieves the currently set proxy dumper or instantiates one.
|
Chris@0
|
272 *
|
Chris@0
|
273 * @return ProxyDumper
|
Chris@0
|
274 */
|
Chris@0
|
275 private function getProxyDumper()
|
Chris@0
|
276 {
|
Chris@0
|
277 if (!$this->proxyDumper) {
|
Chris@0
|
278 $this->proxyDumper = new NullDumper();
|
Chris@0
|
279 }
|
Chris@0
|
280
|
Chris@0
|
281 return $this->proxyDumper;
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 /**
|
Chris@0
|
285 * Generates Service local temp variables.
|
Chris@0
|
286 *
|
Chris@0
|
287 * @return string
|
Chris@0
|
288 */
|
Chris@0
|
289 private function addServiceLocalTempVariables($cId, Definition $definition, \SplObjectStorage $inlinedDefinitions, \SplObjectStorage $allInlinedDefinitions)
|
Chris@0
|
290 {
|
Chris@0
|
291 $allCalls = $calls = $behavior = array();
|
Chris@0
|
292
|
Chris@0
|
293 foreach ($allInlinedDefinitions as $def) {
|
Chris@0
|
294 $arguments = array($def->getArguments(), $def->getFactory(), $def->getProperties(), $def->getMethodCalls(), $def->getConfigurator());
|
Chris@0
|
295 $this->getServiceCallsFromArguments($arguments, $allCalls, false, $cId, $behavior, $allInlinedDefinitions[$def]);
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@0
|
298 $isPreInstance = isset($inlinedDefinitions[$definition]) && isset($this->circularReferences[$cId]) && !$this->getProxyDumper()->isProxyCandidate($definition) && $definition->isShared();
|
Chris@0
|
299 foreach ($inlinedDefinitions as $def) {
|
Chris@0
|
300 $this->getServiceCallsFromArguments(array($def->getArguments(), $def->getFactory()), $calls, $isPreInstance, $cId);
|
Chris@0
|
301 if ($def !== $definition) {
|
Chris@0
|
302 $arguments = array($def->getProperties(), $def->getMethodCalls(), $def->getConfigurator());
|
Chris@0
|
303 $this->getServiceCallsFromArguments($arguments, $calls, $isPreInstance && !$this->hasReference($cId, $arguments, true), $cId);
|
Chris@0
|
304 }
|
Chris@0
|
305 }
|
Chris@0
|
306 if (!isset($inlinedDefinitions[$definition])) {
|
Chris@0
|
307 $arguments = array($definition->getProperties(), $definition->getMethodCalls(), $definition->getConfigurator());
|
Chris@0
|
308 $this->getServiceCallsFromArguments($arguments, $calls, false, $cId);
|
Chris@0
|
309 }
|
Chris@0
|
310
|
Chris@0
|
311 $code = '';
|
Chris@0
|
312 foreach ($calls as $id => $callCount) {
|
Chris@0
|
313 if ('service_container' === $id || $id === $cId || isset($this->referenceVariables[$id])) {
|
Chris@0
|
314 continue;
|
Chris@0
|
315 }
|
Chris@0
|
316 if ($callCount <= 1 && $allCalls[$id] <= 1) {
|
Chris@0
|
317 continue;
|
Chris@0
|
318 }
|
Chris@0
|
319
|
Chris@0
|
320 $name = $this->getNextVariableName();
|
Chris@0
|
321 $this->referenceVariables[$id] = new Variable($name);
|
Chris@0
|
322
|
Chris@0
|
323 $reference = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $behavior[$id] ? new Reference($id, $behavior[$id]) : null;
|
Chris@0
|
324 $code .= sprintf(" \$%s = %s;\n", $name, $this->getServiceCall($id, $reference));
|
Chris@0
|
325 }
|
Chris@0
|
326
|
Chris@0
|
327 if ('' !== $code) {
|
Chris@0
|
328 if ($isPreInstance) {
|
Chris@0
|
329 $code .= <<<EOTXT
|
Chris@0
|
330
|
Chris@0
|
331 if (isset(\$this->services['$cId'])) {
|
Chris@0
|
332 return \$this->services['$cId'];
|
Chris@0
|
333 }
|
Chris@0
|
334
|
Chris@0
|
335 EOTXT;
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 $code .= "\n";
|
Chris@0
|
339 }
|
Chris@0
|
340
|
Chris@0
|
341 return $code;
|
Chris@0
|
342 }
|
Chris@0
|
343
|
Chris@0
|
344 private function analyzeCircularReferences(array $edges, &$checkedNodes, &$currentPath)
|
Chris@0
|
345 {
|
Chris@0
|
346 foreach ($edges as $edge) {
|
Chris@0
|
347 $node = $edge->getDestNode();
|
Chris@0
|
348 $id = $node->getId();
|
Chris@0
|
349
|
Chris@0
|
350 if ($node->getValue() && ($edge->isLazy() || $edge->isWeak())) {
|
Chris@0
|
351 // no-op
|
Chris@0
|
352 } elseif (isset($currentPath[$id])) {
|
Chris@0
|
353 foreach (array_reverse($currentPath) as $parentId) {
|
Chris@0
|
354 $this->circularReferences[$parentId][$id] = $id;
|
Chris@0
|
355 $id = $parentId;
|
Chris@0
|
356 }
|
Chris@0
|
357 } elseif (!isset($checkedNodes[$id])) {
|
Chris@0
|
358 $checkedNodes[$id] = true;
|
Chris@0
|
359 $currentPath[$id] = $id;
|
Chris@0
|
360 $this->analyzeCircularReferences($node->getOutEdges(), $checkedNodes, $currentPath);
|
Chris@0
|
361 unset($currentPath[$id]);
|
Chris@0
|
362 }
|
Chris@0
|
363 }
|
Chris@0
|
364 }
|
Chris@0
|
365
|
Chris@0
|
366 private function collectLineage($class, array &$lineage)
|
Chris@0
|
367 {
|
Chris@0
|
368 if (isset($lineage[$class])) {
|
Chris@0
|
369 return;
|
Chris@0
|
370 }
|
Chris@0
|
371 if (!$r = $this->container->getReflectionClass($class, false)) {
|
Chris@0
|
372 return;
|
Chris@0
|
373 }
|
Chris@0
|
374 if ($this->container instanceof $class) {
|
Chris@0
|
375 return;
|
Chris@0
|
376 }
|
Chris@0
|
377 $file = $r->getFileName();
|
Chris@0
|
378 if (!$file || $this->doExport($file) === $exportedFile = $this->export($file)) {
|
Chris@0
|
379 return;
|
Chris@0
|
380 }
|
Chris@0
|
381
|
Chris@0
|
382 if ($parent = $r->getParentClass()) {
|
Chris@0
|
383 $this->collectLineage($parent->name, $lineage);
|
Chris@0
|
384 }
|
Chris@0
|
385
|
Chris@0
|
386 foreach ($r->getInterfaces() as $parent) {
|
Chris@0
|
387 $this->collectLineage($parent->name, $lineage);
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 foreach ($r->getTraits() as $parent) {
|
Chris@0
|
391 $this->collectLineage($parent->name, $lineage);
|
Chris@0
|
392 }
|
Chris@0
|
393
|
Chris@0
|
394 $lineage[$class] = substr($exportedFile, 1, -1);
|
Chris@0
|
395 }
|
Chris@0
|
396
|
Chris@0
|
397 private function generateProxyClasses()
|
Chris@0
|
398 {
|
Chris@2
|
399 $alreadyGenerated = array();
|
Chris@0
|
400 $definitions = $this->container->getDefinitions();
|
Chris@0
|
401 $strip = '' === $this->docStar && method_exists('Symfony\Component\HttpKernel\Kernel', 'stripComments');
|
Chris@0
|
402 $proxyDumper = $this->getProxyDumper();
|
Chris@0
|
403 ksort($definitions);
|
Chris@0
|
404 foreach ($definitions as $definition) {
|
Chris@0
|
405 if (!$proxyDumper->isProxyCandidate($definition)) {
|
Chris@0
|
406 continue;
|
Chris@0
|
407 }
|
Chris@2
|
408 if (isset($alreadyGenerated[$class = $definition->getClass()])) {
|
Chris@2
|
409 continue;
|
Chris@2
|
410 }
|
Chris@2
|
411 $alreadyGenerated[$class] = true;
|
Chris@0
|
412 // register class' reflector for resource tracking
|
Chris@2
|
413 $this->container->getReflectionClass($class);
|
Chris@0
|
414 $proxyCode = "\n".$proxyDumper->getProxyCode($definition);
|
Chris@0
|
415 if ($strip) {
|
Chris@0
|
416 $proxyCode = "<?php\n".$proxyCode;
|
Chris@0
|
417 $proxyCode = substr(Kernel::stripComments($proxyCode), 5);
|
Chris@0
|
418 }
|
Chris@0
|
419 yield sprintf('%s.php', explode(' ', $proxyCode, 3)[1]) => $proxyCode;
|
Chris@0
|
420 }
|
Chris@0
|
421 }
|
Chris@0
|
422
|
Chris@0
|
423 /**
|
Chris@0
|
424 * Generates the require_once statement for service includes.
|
Chris@0
|
425 *
|
Chris@0
|
426 * @return string
|
Chris@0
|
427 */
|
Chris@0
|
428 private function addServiceInclude($cId, Definition $definition, \SplObjectStorage $inlinedDefinitions)
|
Chris@0
|
429 {
|
Chris@0
|
430 $code = '';
|
Chris@0
|
431
|
Chris@0
|
432 if ($this->inlineRequires && !$this->isHotPath($definition)) {
|
Chris@0
|
433 $lineage = $calls = $behavior = array();
|
Chris@0
|
434 foreach ($inlinedDefinitions as $def) {
|
Chris@0
|
435 if (!$def->isDeprecated() && is_string($class = is_array($factory = $def->getFactory()) && is_string($factory[0]) ? $factory[0] : $def->getClass())) {
|
Chris@0
|
436 $this->collectLineage($class, $lineage);
|
Chris@0
|
437 }
|
Chris@0
|
438 $arguments = array($def->getArguments(), $def->getFactory(), $def->getProperties(), $def->getMethodCalls(), $def->getConfigurator());
|
Chris@0
|
439 $this->getServiceCallsFromArguments($arguments, $calls, false, $cId, $behavior, $inlinedDefinitions[$def]);
|
Chris@0
|
440 }
|
Chris@0
|
441
|
Chris@0
|
442 foreach ($calls as $id => $callCount) {
|
Chris@0
|
443 if ('service_container' !== $id && $id !== $cId
|
Chris@0
|
444 && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE !== $behavior[$id]
|
Chris@0
|
445 && $this->container->has($id)
|
Chris@0
|
446 && $this->isTrivialInstance($def = $this->container->findDefinition($id))
|
Chris@0
|
447 && is_string($class = is_array($factory = $def->getFactory()) && is_string($factory[0]) ? $factory[0] : $def->getClass())
|
Chris@0
|
448 ) {
|
Chris@0
|
449 $this->collectLineage($class, $lineage);
|
Chris@0
|
450 }
|
Chris@0
|
451 }
|
Chris@0
|
452
|
Chris@0
|
453 foreach (array_diff_key(array_flip($lineage), $this->inlinedRequires) as $file => $class) {
|
Chris@0
|
454 $code .= sprintf(" include_once %s;\n", $file);
|
Chris@0
|
455 }
|
Chris@0
|
456 }
|
Chris@0
|
457
|
Chris@0
|
458 foreach ($inlinedDefinitions as $def) {
|
Chris@0
|
459 if ($file = $def->getFile()) {
|
Chris@0
|
460 $code .= sprintf(" include_once %s;\n", $this->dumpValue($file));
|
Chris@0
|
461 }
|
Chris@0
|
462 }
|
Chris@0
|
463
|
Chris@0
|
464 if ('' !== $code) {
|
Chris@0
|
465 $code .= "\n";
|
Chris@0
|
466 }
|
Chris@0
|
467
|
Chris@0
|
468 return $code;
|
Chris@0
|
469 }
|
Chris@0
|
470
|
Chris@0
|
471 /**
|
Chris@0
|
472 * Generates the inline definition of a service.
|
Chris@0
|
473 *
|
Chris@0
|
474 * @return string
|
Chris@0
|
475 *
|
Chris@0
|
476 * @throws RuntimeException When the factory definition is incomplete
|
Chris@0
|
477 * @throws ServiceCircularReferenceException When a circular reference is detected
|
Chris@0
|
478 */
|
Chris@0
|
479 private function addServiceInlinedDefinitions($id, Definition $definition, \SplObjectStorage $inlinedDefinitions, &$isSimpleInstance)
|
Chris@0
|
480 {
|
Chris@0
|
481 $code = '';
|
Chris@0
|
482
|
Chris@0
|
483 foreach ($inlinedDefinitions as $def) {
|
Chris@0
|
484 if ($definition === $def) {
|
Chris@0
|
485 continue;
|
Chris@0
|
486 }
|
Chris@0
|
487 if ($inlinedDefinitions[$def] <= 1 && !$def->getMethodCalls() && !$def->getProperties() && !$def->getConfigurator() && false === strpos($this->dumpValue($def->getClass()), '$')) {
|
Chris@0
|
488 continue;
|
Chris@0
|
489 }
|
Chris@0
|
490 if (isset($this->definitionVariables[$def])) {
|
Chris@0
|
491 $name = $this->definitionVariables[$def];
|
Chris@0
|
492 } else {
|
Chris@0
|
493 $name = $this->getNextVariableName();
|
Chris@0
|
494 $this->definitionVariables[$def] = new Variable($name);
|
Chris@0
|
495 }
|
Chris@0
|
496
|
Chris@0
|
497 // a construct like:
|
Chris@0
|
498 // $a = new ServiceA(ServiceB $b); $b = new ServiceB(ServiceA $a);
|
Chris@0
|
499 // this is an indication for a wrong implementation, you can circumvent this problem
|
Chris@0
|
500 // by setting up your service structure like this:
|
Chris@0
|
501 // $b = new ServiceB();
|
Chris@0
|
502 // $a = new ServiceA(ServiceB $b);
|
Chris@0
|
503 // $b->setServiceA(ServiceA $a);
|
Chris@2
|
504 if (isset($inlinedDefinitions[$definition]) && $this->hasReference($id, array($def->getArguments(), $def->getFactory()))) {
|
Chris@0
|
505 throw new ServiceCircularReferenceException($id, array($id));
|
Chris@0
|
506 }
|
Chris@0
|
507
|
Chris@0
|
508 $code .= $this->addNewInstance($def, '$'.$name, ' = ', $id);
|
Chris@0
|
509
|
Chris@0
|
510 if (!$this->hasReference($id, array($def->getProperties(), $def->getMethodCalls(), $def->getConfigurator()), true)) {
|
Chris@0
|
511 $code .= $this->addServiceProperties($def, $name);
|
Chris@0
|
512 $code .= $this->addServiceMethodCalls($def, $name);
|
Chris@0
|
513 $code .= $this->addServiceConfigurator($def, $name);
|
Chris@0
|
514 } else {
|
Chris@0
|
515 $isSimpleInstance = false;
|
Chris@0
|
516 }
|
Chris@0
|
517
|
Chris@0
|
518 $code .= "\n";
|
Chris@0
|
519 }
|
Chris@0
|
520
|
Chris@0
|
521 return $code;
|
Chris@0
|
522 }
|
Chris@0
|
523
|
Chris@0
|
524 /**
|
Chris@0
|
525 * Generates the service instance.
|
Chris@0
|
526 *
|
Chris@0
|
527 * @param string $id
|
Chris@0
|
528 * @param Definition $definition
|
Chris@0
|
529 * @param bool $isSimpleInstance
|
Chris@0
|
530 *
|
Chris@0
|
531 * @return string
|
Chris@0
|
532 *
|
Chris@0
|
533 * @throws InvalidArgumentException
|
Chris@0
|
534 * @throws RuntimeException
|
Chris@0
|
535 */
|
Chris@0
|
536 private function addServiceInstance($id, Definition $definition, $isSimpleInstance)
|
Chris@0
|
537 {
|
Chris@0
|
538 $class = $this->dumpValue($definition->getClass());
|
Chris@0
|
539
|
Chris@0
|
540 if (0 === strpos($class, "'") && false === strpos($class, '$') && !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
|
Chris@0
|
541 throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
|
Chris@0
|
542 }
|
Chris@0
|
543
|
Chris@0
|
544 $isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
|
Chris@0
|
545 $instantiation = '';
|
Chris@0
|
546
|
Chris@0
|
547 if (!$isProxyCandidate && $definition->isShared()) {
|
Chris@0
|
548 $instantiation = "\$this->services['$id'] = ".($isSimpleInstance ? '' : '$instance');
|
Chris@0
|
549 } elseif (!$isSimpleInstance) {
|
Chris@0
|
550 $instantiation = '$instance';
|
Chris@0
|
551 }
|
Chris@0
|
552
|
Chris@0
|
553 $return = '';
|
Chris@0
|
554 if ($isSimpleInstance) {
|
Chris@0
|
555 $return = 'return ';
|
Chris@0
|
556 } else {
|
Chris@0
|
557 $instantiation .= ' = ';
|
Chris@0
|
558 }
|
Chris@0
|
559
|
Chris@0
|
560 $code = $this->addNewInstance($definition, $return, $instantiation, $id);
|
Chris@0
|
561
|
Chris@0
|
562 if (!$isSimpleInstance) {
|
Chris@0
|
563 $code .= "\n";
|
Chris@0
|
564 }
|
Chris@0
|
565
|
Chris@0
|
566 return $code;
|
Chris@0
|
567 }
|
Chris@0
|
568
|
Chris@0
|
569 /**
|
Chris@0
|
570 * Checks if the definition is a trivial instance.
|
Chris@0
|
571 *
|
Chris@0
|
572 * @param Definition $definition
|
Chris@0
|
573 *
|
Chris@0
|
574 * @return bool
|
Chris@0
|
575 */
|
Chris@0
|
576 private function isTrivialInstance(Definition $definition)
|
Chris@0
|
577 {
|
Chris@0
|
578 if ($definition->isSynthetic() || $definition->getFile() || $definition->getMethodCalls() || $definition->getProperties() || $definition->getConfigurator()) {
|
Chris@0
|
579 return false;
|
Chris@0
|
580 }
|
Chris@0
|
581 if ($definition->isDeprecated() || $definition->isLazy() || $definition->getFactory() || 3 < count($definition->getArguments())) {
|
Chris@0
|
582 return false;
|
Chris@0
|
583 }
|
Chris@0
|
584
|
Chris@0
|
585 foreach ($definition->getArguments() as $arg) {
|
Chris@0
|
586 if (!$arg || $arg instanceof Parameter) {
|
Chris@0
|
587 continue;
|
Chris@0
|
588 }
|
Chris@0
|
589 if (is_array($arg) && 3 >= count($arg)) {
|
Chris@0
|
590 foreach ($arg as $k => $v) {
|
Chris@0
|
591 if ($this->dumpValue($k) !== $this->dumpValue($k, false)) {
|
Chris@0
|
592 return false;
|
Chris@0
|
593 }
|
Chris@0
|
594 if (!$v || $v instanceof Parameter) {
|
Chris@0
|
595 continue;
|
Chris@0
|
596 }
|
Chris@0
|
597 if ($v instanceof Reference && $this->container->has($id = (string) $v) && $this->container->findDefinition($id)->isSynthetic()) {
|
Chris@0
|
598 continue;
|
Chris@0
|
599 }
|
Chris@0
|
600 if (!is_scalar($v) || $this->dumpValue($v) !== $this->dumpValue($v, false)) {
|
Chris@0
|
601 return false;
|
Chris@0
|
602 }
|
Chris@0
|
603 }
|
Chris@0
|
604 } elseif ($arg instanceof Reference && $this->container->has($id = (string) $arg) && $this->container->findDefinition($id)->isSynthetic()) {
|
Chris@0
|
605 continue;
|
Chris@0
|
606 } elseif (!is_scalar($arg) || $this->dumpValue($arg) !== $this->dumpValue($arg, false)) {
|
Chris@0
|
607 return false;
|
Chris@0
|
608 }
|
Chris@0
|
609 }
|
Chris@0
|
610
|
Chris@0
|
611 if (false !== strpos($this->dumpLiteralClass($this->dumpValue($definition->getClass())), '$')) {
|
Chris@0
|
612 return false;
|
Chris@0
|
613 }
|
Chris@0
|
614
|
Chris@0
|
615 return true;
|
Chris@0
|
616 }
|
Chris@0
|
617
|
Chris@0
|
618 /**
|
Chris@0
|
619 * Adds method calls to a service definition.
|
Chris@0
|
620 *
|
Chris@0
|
621 * @param Definition $definition
|
Chris@0
|
622 * @param string $variableName
|
Chris@0
|
623 *
|
Chris@0
|
624 * @return string
|
Chris@0
|
625 */
|
Chris@0
|
626 private function addServiceMethodCalls(Definition $definition, $variableName = 'instance')
|
Chris@0
|
627 {
|
Chris@0
|
628 $calls = '';
|
Chris@0
|
629 foreach ($definition->getMethodCalls() as $call) {
|
Chris@0
|
630 $arguments = array();
|
Chris@0
|
631 foreach ($call[1] as $value) {
|
Chris@0
|
632 $arguments[] = $this->dumpValue($value);
|
Chris@0
|
633 }
|
Chris@0
|
634
|
Chris@0
|
635 $calls .= $this->wrapServiceConditionals($call[1], sprintf(" \$%s->%s(%s);\n", $variableName, $call[0], implode(', ', $arguments)));
|
Chris@0
|
636 }
|
Chris@0
|
637
|
Chris@0
|
638 return $calls;
|
Chris@0
|
639 }
|
Chris@0
|
640
|
Chris@0
|
641 private function addServiceProperties(Definition $definition, $variableName = 'instance')
|
Chris@0
|
642 {
|
Chris@0
|
643 $code = '';
|
Chris@0
|
644 foreach ($definition->getProperties() as $name => $value) {
|
Chris@0
|
645 $code .= sprintf(" \$%s->%s = %s;\n", $variableName, $name, $this->dumpValue($value));
|
Chris@0
|
646 }
|
Chris@0
|
647
|
Chris@0
|
648 return $code;
|
Chris@0
|
649 }
|
Chris@0
|
650
|
Chris@0
|
651 /**
|
Chris@0
|
652 * Generates the inline definition setup.
|
Chris@0
|
653 *
|
Chris@0
|
654 * @return string
|
Chris@0
|
655 *
|
Chris@0
|
656 * @throws ServiceCircularReferenceException when the container contains a circular reference
|
Chris@0
|
657 */
|
Chris@0
|
658 private function addServiceInlinedDefinitionsSetup($id, Definition $definition, \SplObjectStorage $inlinedDefinitions, $isSimpleInstance)
|
Chris@0
|
659 {
|
Chris@0
|
660 $this->referenceVariables[$id] = new Variable('instance');
|
Chris@0
|
661
|
Chris@0
|
662 $code = '';
|
Chris@0
|
663 foreach ($inlinedDefinitions as $def) {
|
Chris@0
|
664 if ($definition === $def || !$this->hasReference($id, array($def->getProperties(), $def->getMethodCalls(), $def->getConfigurator()), true)) {
|
Chris@0
|
665 continue;
|
Chris@0
|
666 }
|
Chris@0
|
667
|
Chris@0
|
668 // if the instance is simple, the return statement has already been generated
|
Chris@0
|
669 // so, the only possible way to get there is because of a circular reference
|
Chris@0
|
670 if ($isSimpleInstance) {
|
Chris@0
|
671 throw new ServiceCircularReferenceException($id, array($id));
|
Chris@0
|
672 }
|
Chris@0
|
673
|
Chris@0
|
674 $name = (string) $this->definitionVariables[$def];
|
Chris@0
|
675 $code .= $this->addServiceProperties($def, $name);
|
Chris@0
|
676 $code .= $this->addServiceMethodCalls($def, $name);
|
Chris@0
|
677 $code .= $this->addServiceConfigurator($def, $name);
|
Chris@0
|
678 }
|
Chris@0
|
679
|
Chris@0
|
680 if ('' !== $code && ($definition->getProperties() || $definition->getMethodCalls() || $definition->getConfigurator())) {
|
Chris@0
|
681 $code .= "\n";
|
Chris@0
|
682 }
|
Chris@0
|
683
|
Chris@0
|
684 return $code;
|
Chris@0
|
685 }
|
Chris@0
|
686
|
Chris@0
|
687 /**
|
Chris@0
|
688 * Adds configurator definition.
|
Chris@0
|
689 *
|
Chris@0
|
690 * @param Definition $definition
|
Chris@0
|
691 * @param string $variableName
|
Chris@0
|
692 *
|
Chris@0
|
693 * @return string
|
Chris@0
|
694 */
|
Chris@0
|
695 private function addServiceConfigurator(Definition $definition, $variableName = 'instance')
|
Chris@0
|
696 {
|
Chris@0
|
697 if (!$callable = $definition->getConfigurator()) {
|
Chris@0
|
698 return '';
|
Chris@0
|
699 }
|
Chris@0
|
700
|
Chris@0
|
701 if (is_array($callable)) {
|
Chris@0
|
702 if ($callable[0] instanceof Reference
|
Chris@0
|
703 || ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))) {
|
Chris@0
|
704 return sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
|
Chris@0
|
705 }
|
Chris@0
|
706
|
Chris@0
|
707 $class = $this->dumpValue($callable[0]);
|
Chris@0
|
708 // If the class is a string we can optimize call_user_func away
|
Chris@0
|
709 if (0 === strpos($class, "'") && false === strpos($class, '$')) {
|
Chris@0
|
710 return sprintf(" %s::%s(\$%s);\n", $this->dumpLiteralClass($class), $callable[1], $variableName);
|
Chris@0
|
711 }
|
Chris@0
|
712
|
Chris@0
|
713 if (0 === strpos($class, 'new ')) {
|
Chris@0
|
714 return sprintf(" (%s)->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
|
Chris@0
|
715 }
|
Chris@0
|
716
|
Chris@0
|
717 return sprintf(" \\call_user_func(array(%s, '%s'), \$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
|
Chris@0
|
718 }
|
Chris@0
|
719
|
Chris@0
|
720 return sprintf(" %s(\$%s);\n", $callable, $variableName);
|
Chris@0
|
721 }
|
Chris@0
|
722
|
Chris@0
|
723 /**
|
Chris@0
|
724 * Adds a service.
|
Chris@0
|
725 *
|
Chris@0
|
726 * @param string $id
|
Chris@0
|
727 * @param Definition $definition
|
Chris@0
|
728 * @param string &$file
|
Chris@0
|
729 *
|
Chris@0
|
730 * @return string
|
Chris@0
|
731 */
|
Chris@0
|
732 private function addService($id, Definition $definition, &$file = null)
|
Chris@0
|
733 {
|
Chris@0
|
734 $this->definitionVariables = new \SplObjectStorage();
|
Chris@0
|
735 $this->referenceVariables = array();
|
Chris@0
|
736 $this->variableCount = 0;
|
Chris@0
|
737
|
Chris@0
|
738 $return = array();
|
Chris@0
|
739
|
Chris@0
|
740 if ($class = $definition->getClass()) {
|
Chris@0
|
741 $class = $this->container->resolveEnvPlaceholders($class);
|
Chris@0
|
742 $return[] = sprintf(0 === strpos($class, '%') ? '@return object A %1$s instance' : '@return \%s', ltrim($class, '\\'));
|
Chris@0
|
743 } elseif ($definition->getFactory()) {
|
Chris@0
|
744 $factory = $definition->getFactory();
|
Chris@0
|
745 if (is_string($factory)) {
|
Chris@0
|
746 $return[] = sprintf('@return object An instance returned by %s()', $factory);
|
Chris@0
|
747 } elseif (is_array($factory) && (is_string($factory[0]) || $factory[0] instanceof Definition || $factory[0] instanceof Reference)) {
|
Chris@0
|
748 if (is_string($factory[0]) || $factory[0] instanceof Reference) {
|
Chris@0
|
749 $return[] = sprintf('@return object An instance returned by %s::%s()', (string) $factory[0], $factory[1]);
|
Chris@0
|
750 } elseif ($factory[0] instanceof Definition) {
|
Chris@0
|
751 $return[] = sprintf('@return object An instance returned by %s::%s()', $factory[0]->getClass(), $factory[1]);
|
Chris@0
|
752 }
|
Chris@0
|
753 }
|
Chris@0
|
754 }
|
Chris@0
|
755
|
Chris@0
|
756 if ($definition->isDeprecated()) {
|
Chris@0
|
757 if ($return && 0 === strpos($return[count($return) - 1], '@return')) {
|
Chris@0
|
758 $return[] = '';
|
Chris@0
|
759 }
|
Chris@0
|
760
|
Chris@0
|
761 $return[] = sprintf('@deprecated %s', $definition->getDeprecationMessage($id));
|
Chris@0
|
762 }
|
Chris@0
|
763
|
Chris@0
|
764 $return = str_replace("\n * \n", "\n *\n", implode("\n * ", $return));
|
Chris@0
|
765 $return = $this->container->resolveEnvPlaceholders($return);
|
Chris@0
|
766
|
Chris@0
|
767 $shared = $definition->isShared() ? ' shared' : '';
|
Chris@0
|
768 $public = $definition->isPublic() ? 'public' : 'private';
|
Chris@0
|
769 $autowired = $definition->isAutowired() ? ' autowired' : '';
|
Chris@0
|
770
|
Chris@0
|
771 if ($definition->isLazy()) {
|
Chris@0
|
772 $lazyInitialization = '$lazyLoad = true';
|
Chris@0
|
773 } else {
|
Chris@0
|
774 $lazyInitialization = '';
|
Chris@0
|
775 }
|
Chris@0
|
776
|
Chris@0
|
777 $asFile = $this->asFiles && $definition->isShared() && !$this->isHotPath($definition);
|
Chris@0
|
778 $methodName = $this->generateMethodName($id);
|
Chris@0
|
779 if ($asFile) {
|
Chris@0
|
780 $file = $methodName.'.php';
|
Chris@0
|
781 $code = " // Returns the $public '$id'$shared$autowired service.\n\n";
|
Chris@0
|
782 } else {
|
Chris@0
|
783 $code = <<<EOF
|
Chris@0
|
784
|
Chris@0
|
785 /*{$this->docStar}
|
Chris@0
|
786 * Gets the $public '$id'$shared$autowired service.
|
Chris@0
|
787 *
|
Chris@0
|
788 * $return
|
Chris@0
|
789 */
|
Chris@0
|
790 protected function {$methodName}($lazyInitialization)
|
Chris@0
|
791 {
|
Chris@0
|
792
|
Chris@0
|
793 EOF;
|
Chris@0
|
794 }
|
Chris@0
|
795
|
Chris@0
|
796 if ($this->getProxyDumper()->isProxyCandidate($definition)) {
|
Chris@0
|
797 $factoryCode = $asFile ? "\$this->load('%s.php', false)" : '$this->%s(false)';
|
Chris@0
|
798 $code .= $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName));
|
Chris@0
|
799 }
|
Chris@0
|
800
|
Chris@0
|
801 if ($definition->isDeprecated()) {
|
Chris@0
|
802 $code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id)));
|
Chris@0
|
803 }
|
Chris@0
|
804
|
Chris@0
|
805 $inlinedDefinitions = $this->getDefinitionsFromArguments(array($definition));
|
Chris@0
|
806 $constructorDefinitions = $this->getDefinitionsFromArguments(array($definition->getArguments(), $definition->getFactory()));
|
Chris@0
|
807 $otherDefinitions = new \SplObjectStorage();
|
Chris@0
|
808
|
Chris@0
|
809 foreach ($inlinedDefinitions as $def) {
|
Chris@0
|
810 if ($def === $definition || isset($constructorDefinitions[$def])) {
|
Chris@0
|
811 $constructorDefinitions[$def] = $inlinedDefinitions[$def];
|
Chris@0
|
812 } else {
|
Chris@0
|
813 $otherDefinitions[$def] = $inlinedDefinitions[$def];
|
Chris@0
|
814 }
|
Chris@0
|
815 }
|
Chris@0
|
816
|
Chris@0
|
817 $isSimpleInstance = !$definition->getProperties() && !$definition->getMethodCalls() && !$definition->getConfigurator();
|
Chris@0
|
818
|
Chris@0
|
819 $code .=
|
Chris@0
|
820 $this->addServiceInclude($id, $definition, $inlinedDefinitions).
|
Chris@0
|
821 $this->addServiceLocalTempVariables($id, $definition, $constructorDefinitions, $inlinedDefinitions).
|
Chris@0
|
822 $this->addServiceInlinedDefinitions($id, $definition, $constructorDefinitions, $isSimpleInstance).
|
Chris@0
|
823 $this->addServiceInstance($id, $definition, $isSimpleInstance).
|
Chris@0
|
824 $this->addServiceLocalTempVariables($id, $definition, $otherDefinitions, $inlinedDefinitions).
|
Chris@0
|
825 $this->addServiceInlinedDefinitions($id, $definition, $otherDefinitions, $isSimpleInstance).
|
Chris@0
|
826 $this->addServiceInlinedDefinitionsSetup($id, $definition, $inlinedDefinitions, $isSimpleInstance).
|
Chris@0
|
827 $this->addServiceProperties($definition).
|
Chris@0
|
828 $this->addServiceMethodCalls($definition).
|
Chris@0
|
829 $this->addServiceConfigurator($definition).
|
Chris@0
|
830 (!$isSimpleInstance ? "\n return \$instance;\n" : '')
|
Chris@0
|
831 ;
|
Chris@0
|
832
|
Chris@0
|
833 if ($asFile) {
|
Chris@0
|
834 $code = implode("\n", array_map(function ($line) { return $line ? substr($line, 8) : $line; }, explode("\n", $code)));
|
Chris@0
|
835 } else {
|
Chris@0
|
836 $code .= " }\n";
|
Chris@0
|
837 }
|
Chris@0
|
838
|
Chris@0
|
839 $this->definitionVariables = null;
|
Chris@0
|
840 $this->referenceVariables = null;
|
Chris@0
|
841
|
Chris@0
|
842 return $code;
|
Chris@0
|
843 }
|
Chris@0
|
844
|
Chris@0
|
845 /**
|
Chris@0
|
846 * Adds multiple services.
|
Chris@0
|
847 *
|
Chris@0
|
848 * @return string
|
Chris@0
|
849 */
|
Chris@0
|
850 private function addServices()
|
Chris@0
|
851 {
|
Chris@0
|
852 $publicServices = $privateServices = '';
|
Chris@0
|
853 $definitions = $this->container->getDefinitions();
|
Chris@0
|
854 ksort($definitions);
|
Chris@0
|
855 foreach ($definitions as $id => $definition) {
|
Chris@0
|
856 if ($definition->isSynthetic() || ($this->asFiles && $definition->isShared() && !$this->isHotPath($definition))) {
|
Chris@0
|
857 continue;
|
Chris@0
|
858 }
|
Chris@0
|
859 if ($definition->isPublic()) {
|
Chris@0
|
860 $publicServices .= $this->addService($id, $definition);
|
Chris@0
|
861 } else {
|
Chris@0
|
862 $privateServices .= $this->addService($id, $definition);
|
Chris@0
|
863 }
|
Chris@0
|
864 }
|
Chris@0
|
865
|
Chris@0
|
866 return $publicServices.$privateServices;
|
Chris@0
|
867 }
|
Chris@0
|
868
|
Chris@0
|
869 private function generateServiceFiles()
|
Chris@0
|
870 {
|
Chris@0
|
871 $definitions = $this->container->getDefinitions();
|
Chris@0
|
872 ksort($definitions);
|
Chris@0
|
873 foreach ($definitions as $id => $definition) {
|
Chris@0
|
874 if (!$definition->isSynthetic() && $definition->isShared() && !$this->isHotPath($definition)) {
|
Chris@0
|
875 $code = $this->addService($id, $definition, $file);
|
Chris@0
|
876 yield $file => $code;
|
Chris@0
|
877 }
|
Chris@0
|
878 }
|
Chris@0
|
879 }
|
Chris@0
|
880
|
Chris@0
|
881 private function addNewInstance(Definition $definition, $return, $instantiation, $id)
|
Chris@0
|
882 {
|
Chris@0
|
883 $class = $this->dumpValue($definition->getClass());
|
Chris@0
|
884 $return = ' '.$return.$instantiation;
|
Chris@0
|
885
|
Chris@0
|
886 $arguments = array();
|
Chris@0
|
887 foreach ($definition->getArguments() as $value) {
|
Chris@0
|
888 $arguments[] = $this->dumpValue($value);
|
Chris@0
|
889 }
|
Chris@0
|
890
|
Chris@0
|
891 if (null !== $definition->getFactory()) {
|
Chris@0
|
892 $callable = $definition->getFactory();
|
Chris@0
|
893 if (is_array($callable)) {
|
Chris@0
|
894 if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $callable[1])) {
|
Chris@0
|
895 throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s)', $callable[1] ?: 'n/a'));
|
Chris@0
|
896 }
|
Chris@0
|
897
|
Chris@0
|
898 if ($callable[0] instanceof Reference
|
Chris@0
|
899 || ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))) {
|
Chris@0
|
900 return $return.sprintf("%s->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
|
Chris@0
|
901 }
|
Chris@0
|
902
|
Chris@0
|
903 $class = $this->dumpValue($callable[0]);
|
Chris@0
|
904 // If the class is a string we can optimize call_user_func away
|
Chris@0
|
905 if (0 === strpos($class, "'") && false === strpos($class, '$')) {
|
Chris@0
|
906 if ("''" === $class) {
|
Chris@0
|
907 throw new RuntimeException(sprintf('Cannot dump definition: The "%s" service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class?', $id));
|
Chris@0
|
908 }
|
Chris@0
|
909
|
Chris@0
|
910 return $return.sprintf("%s::%s(%s);\n", $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '');
|
Chris@0
|
911 }
|
Chris@0
|
912
|
Chris@0
|
913 if (0 === strpos($class, 'new ')) {
|
Chris@0
|
914 return $return.sprintf("(%s)->%s(%s);\n", $class, $callable[1], $arguments ? implode(', ', $arguments) : '');
|
Chris@0
|
915 }
|
Chris@0
|
916
|
Chris@0
|
917 return $return.sprintf("\\call_user_func(array(%s, '%s')%s);\n", $class, $callable[1], $arguments ? ', '.implode(', ', $arguments) : '');
|
Chris@0
|
918 }
|
Chris@0
|
919
|
Chris@0
|
920 return $return.sprintf("%s(%s);\n", $this->dumpLiteralClass($this->dumpValue($callable)), $arguments ? implode(', ', $arguments) : '');
|
Chris@0
|
921 }
|
Chris@0
|
922
|
Chris@0
|
923 if (false !== strpos($class, '$')) {
|
Chris@0
|
924 return sprintf(" \$class = %s;\n\n%snew \$class(%s);\n", $class, $return, implode(', ', $arguments));
|
Chris@0
|
925 }
|
Chris@0
|
926
|
Chris@0
|
927 return $return.sprintf("new %s(%s);\n", $this->dumpLiteralClass($class), implode(', ', $arguments));
|
Chris@0
|
928 }
|
Chris@0
|
929
|
Chris@0
|
930 /**
|
Chris@0
|
931 * Adds the class headers.
|
Chris@0
|
932 *
|
Chris@0
|
933 * @param string $class Class name
|
Chris@0
|
934 * @param string $baseClass The name of the base class
|
Chris@0
|
935 * @param string $baseClassWithNamespace Fully qualified base class name
|
Chris@0
|
936 *
|
Chris@0
|
937 * @return string
|
Chris@0
|
938 */
|
Chris@0
|
939 private function startClass($class, $baseClass, $baseClassWithNamespace)
|
Chris@0
|
940 {
|
Chris@0
|
941 $bagClass = $this->container->isCompiled() ? 'use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;' : 'use Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag;';
|
Chris@0
|
942 $namespaceLine = !$this->asFiles && $this->namespace ? "\nnamespace {$this->namespace};\n" : '';
|
Chris@0
|
943
|
Chris@0
|
944 $code = <<<EOF
|
Chris@0
|
945 <?php
|
Chris@0
|
946 $namespaceLine
|
Chris@0
|
947 use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
|
Chris@0
|
948 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
949 use Symfony\Component\DependencyInjection\Container;
|
Chris@0
|
950 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
951 use Symfony\Component\DependencyInjection\Exception\LogicException;
|
Chris@0
|
952 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
953 $bagClass
|
Chris@0
|
954
|
Chris@0
|
955 /*{$this->docStar}
|
Chris@0
|
956 * This class has been auto-generated
|
Chris@0
|
957 * by the Symfony Dependency Injection Component.
|
Chris@0
|
958 *
|
Chris@0
|
959 * @final since Symfony 3.3
|
Chris@0
|
960 */
|
Chris@0
|
961 class $class extends $baseClass
|
Chris@0
|
962 {
|
Chris@0
|
963 private \$parameters;
|
Chris@0
|
964 private \$targetDirs = array();
|
Chris@0
|
965
|
Chris@0
|
966 public function __construct()
|
Chris@0
|
967 {
|
Chris@0
|
968
|
Chris@0
|
969 EOF;
|
Chris@0
|
970 if (null !== $this->targetDirRegex) {
|
Chris@0
|
971 $dir = $this->asFiles ? '$this->targetDirs[0] = \\dirname($containerDir)' : '__DIR__';
|
Chris@0
|
972 $code .= <<<EOF
|
Chris@0
|
973 \$dir = {$dir};
|
Chris@0
|
974 for (\$i = 1; \$i <= {$this->targetDirMaxMatches}; ++\$i) {
|
Chris@0
|
975 \$this->targetDirs[\$i] = \$dir = \\dirname(\$dir);
|
Chris@0
|
976 }
|
Chris@0
|
977
|
Chris@0
|
978 EOF;
|
Chris@0
|
979 }
|
Chris@0
|
980 if ($this->asFiles) {
|
Chris@0
|
981 $code = str_replace('$parameters', "\$buildParameters;\n private \$containerDir;\n private \$parameters", $code);
|
Chris@0
|
982 $code = str_replace('__construct()', '__construct(array $buildParameters = array(), $containerDir = __DIR__)', $code);
|
Chris@0
|
983 $code .= " \$this->buildParameters = \$buildParameters;\n";
|
Chris@0
|
984 $code .= " \$this->containerDir = \$containerDir;\n";
|
Chris@0
|
985 }
|
Chris@0
|
986
|
Chris@0
|
987 if ($this->container->isCompiled()) {
|
Chris@0
|
988 if (Container::class !== $baseClassWithNamespace) {
|
Chris@0
|
989 $r = $this->container->getReflectionClass($baseClassWithNamespace, false);
|
Chris@0
|
990 if (null !== $r
|
Chris@0
|
991 && (null !== $constructor = $r->getConstructor())
|
Chris@0
|
992 && 0 === $constructor->getNumberOfRequiredParameters()
|
Chris@0
|
993 && Container::class !== $constructor->getDeclaringClass()->name
|
Chris@0
|
994 ) {
|
Chris@0
|
995 $code .= " parent::__construct();\n";
|
Chris@0
|
996 $code .= " \$this->parameterBag = null;\n\n";
|
Chris@0
|
997 }
|
Chris@0
|
998 }
|
Chris@0
|
999
|
Chris@0
|
1000 if ($this->container->getParameterBag()->all()) {
|
Chris@0
|
1001 $code .= " \$this->parameters = \$this->getDefaultParameters();\n\n";
|
Chris@0
|
1002 }
|
Chris@0
|
1003
|
Chris@0
|
1004 $code .= " \$this->services = array();\n";
|
Chris@0
|
1005 } else {
|
Chris@0
|
1006 $arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null;
|
Chris@0
|
1007 $code .= " parent::__construct($arguments);\n";
|
Chris@0
|
1008 }
|
Chris@0
|
1009
|
Chris@0
|
1010 $code .= $this->addNormalizedIds();
|
Chris@0
|
1011 $code .= $this->addSyntheticIds();
|
Chris@0
|
1012 $code .= $this->addMethodMap();
|
Chris@0
|
1013 $code .= $this->asFiles ? $this->addFileMap() : '';
|
Chris@0
|
1014 $code .= $this->addPrivateServices();
|
Chris@0
|
1015 $code .= $this->addAliases();
|
Chris@0
|
1016 $code .= $this->addInlineRequires();
|
Chris@0
|
1017 $code .= <<<'EOF'
|
Chris@0
|
1018 }
|
Chris@0
|
1019
|
Chris@0
|
1020 EOF;
|
Chris@0
|
1021 $code .= $this->addRemovedIds();
|
Chris@0
|
1022
|
Chris@0
|
1023 if ($this->container->isCompiled()) {
|
Chris@0
|
1024 $code .= <<<EOF
|
Chris@0
|
1025
|
Chris@0
|
1026 public function compile()
|
Chris@0
|
1027 {
|
Chris@0
|
1028 throw new LogicException('You cannot compile a dumped container that was already compiled.');
|
Chris@0
|
1029 }
|
Chris@0
|
1030
|
Chris@0
|
1031 public function isCompiled()
|
Chris@0
|
1032 {
|
Chris@0
|
1033 return true;
|
Chris@0
|
1034 }
|
Chris@0
|
1035
|
Chris@0
|
1036 public function isFrozen()
|
Chris@0
|
1037 {
|
Chris@0
|
1038 @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
|
Chris@0
|
1039
|
Chris@0
|
1040 return true;
|
Chris@0
|
1041 }
|
Chris@0
|
1042
|
Chris@0
|
1043 EOF;
|
Chris@0
|
1044 }
|
Chris@0
|
1045
|
Chris@0
|
1046 if ($this->asFiles) {
|
Chris@0
|
1047 $code .= <<<EOF
|
Chris@0
|
1048
|
Chris@0
|
1049 protected function load(\$file, \$lazyLoad = true)
|
Chris@0
|
1050 {
|
Chris@0
|
1051 return require \$this->containerDir.\\DIRECTORY_SEPARATOR.\$file;
|
Chris@0
|
1052 }
|
Chris@0
|
1053
|
Chris@0
|
1054 EOF;
|
Chris@0
|
1055 }
|
Chris@0
|
1056
|
Chris@0
|
1057 $proxyDumper = $this->getProxyDumper();
|
Chris@0
|
1058 foreach ($this->container->getDefinitions() as $definition) {
|
Chris@0
|
1059 if (!$proxyDumper->isProxyCandidate($definition)) {
|
Chris@0
|
1060 continue;
|
Chris@0
|
1061 }
|
Chris@0
|
1062 if ($this->asFiles) {
|
Chris@0
|
1063 $proxyLoader = '$this->load("{$class}.php")';
|
Chris@0
|
1064 } elseif ($this->namespace) {
|
Chris@0
|
1065 $proxyLoader = 'class_alias("'.$this->namespace.'\\\\{$class}", $class, false)';
|
Chris@0
|
1066 } else {
|
Chris@0
|
1067 $proxyLoader = '';
|
Chris@0
|
1068 }
|
Chris@0
|
1069 if ($proxyLoader) {
|
Chris@0
|
1070 $proxyLoader = "class_exists(\$class, false) || {$proxyLoader};\n\n ";
|
Chris@0
|
1071 }
|
Chris@0
|
1072 $code .= <<<EOF
|
Chris@0
|
1073
|
Chris@0
|
1074 protected function createProxy(\$class, \Closure \$factory)
|
Chris@0
|
1075 {
|
Chris@0
|
1076 {$proxyLoader}return \$factory();
|
Chris@0
|
1077 }
|
Chris@0
|
1078
|
Chris@0
|
1079 EOF;
|
Chris@0
|
1080 break;
|
Chris@0
|
1081 }
|
Chris@0
|
1082
|
Chris@0
|
1083 return $code;
|
Chris@0
|
1084 }
|
Chris@0
|
1085
|
Chris@0
|
1086 /**
|
Chris@0
|
1087 * Adds the normalizedIds property definition.
|
Chris@0
|
1088 *
|
Chris@0
|
1089 * @return string
|
Chris@0
|
1090 */
|
Chris@0
|
1091 private function addNormalizedIds()
|
Chris@0
|
1092 {
|
Chris@0
|
1093 $code = '';
|
Chris@0
|
1094 $normalizedIds = $this->container->getNormalizedIds();
|
Chris@0
|
1095 ksort($normalizedIds);
|
Chris@0
|
1096 foreach ($normalizedIds as $id => $normalizedId) {
|
Chris@0
|
1097 if ($this->container->has($normalizedId)) {
|
Chris@0
|
1098 $code .= ' '.$this->doExport($id).' => '.$this->doExport($normalizedId).",\n";
|
Chris@0
|
1099 }
|
Chris@0
|
1100 }
|
Chris@0
|
1101
|
Chris@0
|
1102 return $code ? " \$this->normalizedIds = array(\n".$code." );\n" : '';
|
Chris@0
|
1103 }
|
Chris@0
|
1104
|
Chris@0
|
1105 /**
|
Chris@0
|
1106 * Adds the syntheticIds definition.
|
Chris@0
|
1107 *
|
Chris@0
|
1108 * @return string
|
Chris@0
|
1109 */
|
Chris@0
|
1110 private function addSyntheticIds()
|
Chris@0
|
1111 {
|
Chris@0
|
1112 $code = '';
|
Chris@0
|
1113 $definitions = $this->container->getDefinitions();
|
Chris@0
|
1114 ksort($definitions);
|
Chris@0
|
1115 foreach ($definitions as $id => $definition) {
|
Chris@0
|
1116 if ($definition->isSynthetic() && 'service_container' !== $id) {
|
Chris@0
|
1117 $code .= ' '.$this->doExport($id)." => true,\n";
|
Chris@0
|
1118 }
|
Chris@0
|
1119 }
|
Chris@0
|
1120
|
Chris@0
|
1121 return $code ? " \$this->syntheticIds = array(\n{$code} );\n" : '';
|
Chris@0
|
1122 }
|
Chris@0
|
1123
|
Chris@0
|
1124 /**
|
Chris@0
|
1125 * Adds the removedIds definition.
|
Chris@0
|
1126 *
|
Chris@0
|
1127 * @return string
|
Chris@0
|
1128 */
|
Chris@0
|
1129 private function addRemovedIds()
|
Chris@0
|
1130 {
|
Chris@0
|
1131 if (!$ids = $this->container->getRemovedIds()) {
|
Chris@0
|
1132 return '';
|
Chris@0
|
1133 }
|
Chris@0
|
1134 if ($this->asFiles) {
|
Chris@0
|
1135 $code = "require \$this->containerDir.\\DIRECTORY_SEPARATOR.'removed-ids.php'";
|
Chris@0
|
1136 } else {
|
Chris@0
|
1137 $code = '';
|
Chris@0
|
1138 $ids = array_keys($ids);
|
Chris@0
|
1139 sort($ids);
|
Chris@0
|
1140 foreach ($ids as $id) {
|
Chris@0
|
1141 $code .= ' '.$this->doExport($id)." => true,\n";
|
Chris@0
|
1142 }
|
Chris@0
|
1143
|
Chris@0
|
1144 $code = "array(\n{$code} )";
|
Chris@0
|
1145 }
|
Chris@0
|
1146
|
Chris@0
|
1147 return <<<EOF
|
Chris@0
|
1148
|
Chris@0
|
1149 public function getRemovedIds()
|
Chris@0
|
1150 {
|
Chris@0
|
1151 return {$code};
|
Chris@0
|
1152 }
|
Chris@0
|
1153
|
Chris@0
|
1154 EOF;
|
Chris@0
|
1155 }
|
Chris@0
|
1156
|
Chris@0
|
1157 /**
|
Chris@0
|
1158 * Adds the methodMap property definition.
|
Chris@0
|
1159 *
|
Chris@0
|
1160 * @return string
|
Chris@0
|
1161 */
|
Chris@0
|
1162 private function addMethodMap()
|
Chris@0
|
1163 {
|
Chris@0
|
1164 $code = '';
|
Chris@0
|
1165 $definitions = $this->container->getDefinitions();
|
Chris@0
|
1166 ksort($definitions);
|
Chris@0
|
1167 foreach ($definitions as $id => $definition) {
|
Chris@0
|
1168 if (!$definition->isSynthetic() && (!$this->asFiles || !$definition->isShared() || $this->isHotPath($definition))) {
|
Chris@0
|
1169 $code .= ' '.$this->doExport($id).' => '.$this->doExport($this->generateMethodName($id)).",\n";
|
Chris@0
|
1170 }
|
Chris@0
|
1171 }
|
Chris@0
|
1172
|
Chris@0
|
1173 return $code ? " \$this->methodMap = array(\n{$code} );\n" : '';
|
Chris@0
|
1174 }
|
Chris@0
|
1175
|
Chris@0
|
1176 /**
|
Chris@0
|
1177 * Adds the fileMap property definition.
|
Chris@0
|
1178 *
|
Chris@0
|
1179 * @return string
|
Chris@0
|
1180 */
|
Chris@0
|
1181 private function addFileMap()
|
Chris@0
|
1182 {
|
Chris@0
|
1183 $code = '';
|
Chris@0
|
1184 $definitions = $this->container->getDefinitions();
|
Chris@0
|
1185 ksort($definitions);
|
Chris@0
|
1186 foreach ($definitions as $id => $definition) {
|
Chris@0
|
1187 if (!$definition->isSynthetic() && $definition->isShared() && !$this->isHotPath($definition)) {
|
Chris@0
|
1188 $code .= sprintf(" %s => '%s.php',\n", $this->doExport($id), $this->generateMethodName($id));
|
Chris@0
|
1189 }
|
Chris@0
|
1190 }
|
Chris@0
|
1191
|
Chris@0
|
1192 return $code ? " \$this->fileMap = array(\n{$code} );\n" : '';
|
Chris@0
|
1193 }
|
Chris@0
|
1194
|
Chris@0
|
1195 /**
|
Chris@0
|
1196 * Adds the privates property definition.
|
Chris@0
|
1197 *
|
Chris@0
|
1198 * @return string
|
Chris@0
|
1199 */
|
Chris@0
|
1200 private function addPrivateServices()
|
Chris@0
|
1201 {
|
Chris@0
|
1202 $code = '';
|
Chris@0
|
1203
|
Chris@0
|
1204 $aliases = $this->container->getAliases();
|
Chris@0
|
1205 ksort($aliases);
|
Chris@0
|
1206 foreach ($aliases as $id => $alias) {
|
Chris@0
|
1207 if ($alias->isPrivate()) {
|
Chris@0
|
1208 $code .= ' '.$this->doExport($id)." => true,\n";
|
Chris@0
|
1209 }
|
Chris@0
|
1210 }
|
Chris@0
|
1211
|
Chris@0
|
1212 $definitions = $this->container->getDefinitions();
|
Chris@0
|
1213 ksort($definitions);
|
Chris@0
|
1214 foreach ($definitions as $id => $definition) {
|
Chris@0
|
1215 if (!$definition->isPublic()) {
|
Chris@0
|
1216 $code .= ' '.$this->doExport($id)." => true,\n";
|
Chris@0
|
1217 }
|
Chris@0
|
1218 }
|
Chris@0
|
1219
|
Chris@0
|
1220 if (empty($code)) {
|
Chris@0
|
1221 return '';
|
Chris@0
|
1222 }
|
Chris@0
|
1223
|
Chris@0
|
1224 $out = " \$this->privates = array(\n";
|
Chris@0
|
1225 $out .= $code;
|
Chris@0
|
1226 $out .= " );\n";
|
Chris@0
|
1227
|
Chris@0
|
1228 return $out;
|
Chris@0
|
1229 }
|
Chris@0
|
1230
|
Chris@0
|
1231 /**
|
Chris@0
|
1232 * Adds the aliases property definition.
|
Chris@0
|
1233 *
|
Chris@0
|
1234 * @return string
|
Chris@0
|
1235 */
|
Chris@0
|
1236 private function addAliases()
|
Chris@0
|
1237 {
|
Chris@0
|
1238 if (!$aliases = $this->container->getAliases()) {
|
Chris@0
|
1239 return $this->container->isCompiled() ? "\n \$this->aliases = array();\n" : '';
|
Chris@0
|
1240 }
|
Chris@0
|
1241
|
Chris@0
|
1242 $code = " \$this->aliases = array(\n";
|
Chris@0
|
1243 ksort($aliases);
|
Chris@0
|
1244 foreach ($aliases as $alias => $id) {
|
Chris@0
|
1245 $id = $this->container->normalizeId($id);
|
Chris@0
|
1246 while (isset($aliases[$id])) {
|
Chris@0
|
1247 $id = $this->container->normalizeId($aliases[$id]);
|
Chris@0
|
1248 }
|
Chris@0
|
1249 $code .= ' '.$this->doExport($alias).' => '.$this->doExport($id).",\n";
|
Chris@0
|
1250 }
|
Chris@0
|
1251
|
Chris@0
|
1252 return $code." );\n";
|
Chris@0
|
1253 }
|
Chris@0
|
1254
|
Chris@0
|
1255 private function addInlineRequires()
|
Chris@0
|
1256 {
|
Chris@0
|
1257 if (!$this->hotPathTag || !$this->inlineRequires) {
|
Chris@0
|
1258 return '';
|
Chris@0
|
1259 }
|
Chris@0
|
1260
|
Chris@0
|
1261 $lineage = array();
|
Chris@0
|
1262
|
Chris@0
|
1263 foreach ($this->container->findTaggedServiceIds($this->hotPathTag) as $id => $tags) {
|
Chris@0
|
1264 $definition = $this->container->getDefinition($id);
|
Chris@0
|
1265 $inlinedDefinitions = $this->getDefinitionsFromArguments(array($definition));
|
Chris@0
|
1266
|
Chris@0
|
1267 foreach ($inlinedDefinitions as $def) {
|
Chris@0
|
1268 if (is_string($class = is_array($factory = $def->getFactory()) && is_string($factory[0]) ? $factory[0] : $def->getClass())) {
|
Chris@0
|
1269 $this->collectLineage($class, $lineage);
|
Chris@0
|
1270 }
|
Chris@0
|
1271 }
|
Chris@0
|
1272 }
|
Chris@0
|
1273
|
Chris@0
|
1274 $code = '';
|
Chris@0
|
1275
|
Chris@0
|
1276 foreach ($lineage as $file) {
|
Chris@0
|
1277 if (!isset($this->inlinedRequires[$file])) {
|
Chris@0
|
1278 $this->inlinedRequires[$file] = true;
|
Chris@0
|
1279 $code .= sprintf("\n include_once %s;", $file);
|
Chris@0
|
1280 }
|
Chris@0
|
1281 }
|
Chris@0
|
1282
|
Chris@0
|
1283 return $code ? sprintf("\n \$this->privates['service_container'] = function () {%s\n };\n", $code) : '';
|
Chris@0
|
1284 }
|
Chris@0
|
1285
|
Chris@0
|
1286 /**
|
Chris@0
|
1287 * Adds default parameters method.
|
Chris@0
|
1288 *
|
Chris@0
|
1289 * @return string
|
Chris@0
|
1290 */
|
Chris@0
|
1291 private function addDefaultParametersMethod()
|
Chris@0
|
1292 {
|
Chris@0
|
1293 if (!$this->container->getParameterBag()->all()) {
|
Chris@0
|
1294 return '';
|
Chris@0
|
1295 }
|
Chris@0
|
1296
|
Chris@0
|
1297 $php = array();
|
Chris@0
|
1298 $dynamicPhp = array();
|
Chris@0
|
1299 $normalizedParams = array();
|
Chris@0
|
1300
|
Chris@0
|
1301 foreach ($this->container->getParameterBag()->all() as $key => $value) {
|
Chris@0
|
1302 if ($key !== $resolvedKey = $this->container->resolveEnvPlaceholders($key)) {
|
Chris@0
|
1303 throw new InvalidArgumentException(sprintf('Parameter name cannot use env parameters: %s.', $resolvedKey));
|
Chris@0
|
1304 }
|
Chris@0
|
1305 if ($key !== $lcKey = strtolower($key)) {
|
Chris@0
|
1306 $normalizedParams[] = sprintf(' %s => %s,', $this->export($lcKey), $this->export($key));
|
Chris@0
|
1307 }
|
Chris@0
|
1308 $export = $this->exportParameters(array($value));
|
Chris@0
|
1309 $export = explode('0 => ', substr(rtrim($export, " )\n"), 7, -1), 2);
|
Chris@0
|
1310
|
Chris@0
|
1311 if (preg_match("/\\\$this->(?:getEnv\('(?:\w++:)*+\w++'\)|targetDirs\[\d++\])/", $export[1])) {
|
Chris@0
|
1312 $dynamicPhp[$key] = sprintf('%scase %s: $value = %s; break;', $export[0], $this->export($key), $export[1]);
|
Chris@0
|
1313 } else {
|
Chris@0
|
1314 $php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
|
Chris@0
|
1315 }
|
Chris@0
|
1316 }
|
Chris@0
|
1317 $parameters = sprintf("array(\n%s\n%s)", implode("\n", $php), str_repeat(' ', 8));
|
Chris@0
|
1318
|
Chris@0
|
1319 $code = '';
|
Chris@0
|
1320 if ($this->container->isCompiled()) {
|
Chris@0
|
1321 $code .= <<<'EOF'
|
Chris@0
|
1322
|
Chris@0
|
1323 public function getParameter($name)
|
Chris@0
|
1324 {
|
Chris@0
|
1325 $name = (string) $name;
|
Chris@0
|
1326 if (isset($this->buildParameters[$name])) {
|
Chris@0
|
1327 return $this->buildParameters[$name];
|
Chris@0
|
1328 }
|
Chris@0
|
1329 if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) {
|
Chris@0
|
1330 $name = $this->normalizeParameterName($name);
|
Chris@0
|
1331
|
Chris@0
|
1332 if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) {
|
Chris@0
|
1333 throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
|
Chris@0
|
1334 }
|
Chris@0
|
1335 }
|
Chris@0
|
1336 if (isset($this->loadedDynamicParameters[$name])) {
|
Chris@0
|
1337 return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
|
Chris@0
|
1338 }
|
Chris@0
|
1339
|
Chris@0
|
1340 return $this->parameters[$name];
|
Chris@0
|
1341 }
|
Chris@0
|
1342
|
Chris@0
|
1343 public function hasParameter($name)
|
Chris@0
|
1344 {
|
Chris@0
|
1345 $name = (string) $name;
|
Chris@0
|
1346 if (isset($this->buildParameters[$name])) {
|
Chris@0
|
1347 return true;
|
Chris@0
|
1348 }
|
Chris@0
|
1349 $name = $this->normalizeParameterName($name);
|
Chris@0
|
1350
|
Chris@0
|
1351 return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters);
|
Chris@0
|
1352 }
|
Chris@0
|
1353
|
Chris@0
|
1354 public function setParameter($name, $value)
|
Chris@0
|
1355 {
|
Chris@0
|
1356 throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
|
Chris@0
|
1357 }
|
Chris@0
|
1358
|
Chris@0
|
1359 public function getParameterBag()
|
Chris@0
|
1360 {
|
Chris@0
|
1361 if (null === $this->parameterBag) {
|
Chris@0
|
1362 $parameters = $this->parameters;
|
Chris@0
|
1363 foreach ($this->loadedDynamicParameters as $name => $loaded) {
|
Chris@0
|
1364 $parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
|
Chris@0
|
1365 }
|
Chris@0
|
1366 foreach ($this->buildParameters as $name => $value) {
|
Chris@0
|
1367 $parameters[$name] = $value;
|
Chris@0
|
1368 }
|
Chris@0
|
1369 $this->parameterBag = new FrozenParameterBag($parameters);
|
Chris@0
|
1370 }
|
Chris@0
|
1371
|
Chris@0
|
1372 return $this->parameterBag;
|
Chris@0
|
1373 }
|
Chris@0
|
1374
|
Chris@0
|
1375 EOF;
|
Chris@0
|
1376 if (!$this->asFiles) {
|
Chris@0
|
1377 $code = preg_replace('/^.*buildParameters.*\n.*\n.*\n/m', '', $code);
|
Chris@0
|
1378 }
|
Chris@0
|
1379
|
Chris@0
|
1380 if ($dynamicPhp) {
|
Chris@0
|
1381 $loadedDynamicParameters = $this->exportParameters(array_combine(array_keys($dynamicPhp), array_fill(0, count($dynamicPhp), false)), '', 8);
|
Chris@0
|
1382 $getDynamicParameter = <<<'EOF'
|
Chris@0
|
1383 switch ($name) {
|
Chris@0
|
1384 %s
|
Chris@0
|
1385 default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%%s" must be defined.', $name));
|
Chris@0
|
1386 }
|
Chris@0
|
1387 $this->loadedDynamicParameters[$name] = true;
|
Chris@0
|
1388
|
Chris@0
|
1389 return $this->dynamicParameters[$name] = $value;
|
Chris@0
|
1390 EOF;
|
Chris@0
|
1391 $getDynamicParameter = sprintf($getDynamicParameter, implode("\n", $dynamicPhp));
|
Chris@0
|
1392 } else {
|
Chris@0
|
1393 $loadedDynamicParameters = 'array()';
|
Chris@0
|
1394 $getDynamicParameter = str_repeat(' ', 8).'throw new InvalidArgumentException(sprintf(\'The dynamic parameter "%s" must be defined.\', $name));';
|
Chris@0
|
1395 }
|
Chris@0
|
1396
|
Chris@0
|
1397 $code .= <<<EOF
|
Chris@0
|
1398
|
Chris@0
|
1399 private \$loadedDynamicParameters = {$loadedDynamicParameters};
|
Chris@0
|
1400 private \$dynamicParameters = array();
|
Chris@0
|
1401
|
Chris@0
|
1402 /*{$this->docStar}
|
Chris@0
|
1403 * Computes a dynamic parameter.
|
Chris@0
|
1404 *
|
Chris@0
|
1405 * @param string The name of the dynamic parameter to load
|
Chris@0
|
1406 *
|
Chris@0
|
1407 * @return mixed The value of the dynamic parameter
|
Chris@0
|
1408 *
|
Chris@0
|
1409 * @throws InvalidArgumentException When the dynamic parameter does not exist
|
Chris@0
|
1410 */
|
Chris@0
|
1411 private function getDynamicParameter(\$name)
|
Chris@0
|
1412 {
|
Chris@0
|
1413 {$getDynamicParameter}
|
Chris@0
|
1414 }
|
Chris@0
|
1415
|
Chris@0
|
1416
|
Chris@0
|
1417 EOF;
|
Chris@0
|
1418
|
Chris@0
|
1419 $code .= ' private $normalizedParameterNames = '.($normalizedParams ? sprintf("array(\n%s\n );", implode("\n", $normalizedParams)) : 'array();')."\n";
|
Chris@0
|
1420 $code .= <<<'EOF'
|
Chris@0
|
1421
|
Chris@0
|
1422 private function normalizeParameterName($name)
|
Chris@0
|
1423 {
|
Chris@0
|
1424 if (isset($this->normalizedParameterNames[$normalizedName = strtolower($name)]) || isset($this->parameters[$normalizedName]) || array_key_exists($normalizedName, $this->parameters)) {
|
Chris@0
|
1425 $normalizedName = isset($this->normalizedParameterNames[$normalizedName]) ? $this->normalizedParameterNames[$normalizedName] : $normalizedName;
|
Chris@0
|
1426 if ((string) $name !== $normalizedName) {
|
Chris@0
|
1427 @trigger_error(sprintf('Parameter names will be made case sensitive in Symfony 4.0. Using "%s" instead of "%s" is deprecated since Symfony 3.4.', $name, $normalizedName), E_USER_DEPRECATED);
|
Chris@0
|
1428 }
|
Chris@0
|
1429 } else {
|
Chris@0
|
1430 $normalizedName = $this->normalizedParameterNames[$normalizedName] = (string) $name;
|
Chris@0
|
1431 }
|
Chris@0
|
1432
|
Chris@0
|
1433 return $normalizedName;
|
Chris@0
|
1434 }
|
Chris@0
|
1435
|
Chris@0
|
1436 EOF;
|
Chris@0
|
1437 } elseif ($dynamicPhp) {
|
Chris@0
|
1438 throw new RuntimeException('You cannot dump a not-frozen container with dynamic parameters.');
|
Chris@0
|
1439 }
|
Chris@0
|
1440
|
Chris@0
|
1441 $code .= <<<EOF
|
Chris@0
|
1442
|
Chris@0
|
1443 /*{$this->docStar}
|
Chris@0
|
1444 * Gets the default parameters.
|
Chris@0
|
1445 *
|
Chris@0
|
1446 * @return array An array of the default parameters
|
Chris@0
|
1447 */
|
Chris@0
|
1448 protected function getDefaultParameters()
|
Chris@0
|
1449 {
|
Chris@0
|
1450 return $parameters;
|
Chris@0
|
1451 }
|
Chris@0
|
1452
|
Chris@0
|
1453 EOF;
|
Chris@0
|
1454
|
Chris@0
|
1455 return $code;
|
Chris@0
|
1456 }
|
Chris@0
|
1457
|
Chris@0
|
1458 /**
|
Chris@0
|
1459 * Exports parameters.
|
Chris@0
|
1460 *
|
Chris@0
|
1461 * @param array $parameters
|
Chris@0
|
1462 * @param string $path
|
Chris@0
|
1463 * @param int $indent
|
Chris@0
|
1464 *
|
Chris@0
|
1465 * @return string
|
Chris@0
|
1466 *
|
Chris@0
|
1467 * @throws InvalidArgumentException
|
Chris@0
|
1468 */
|
Chris@0
|
1469 private function exportParameters(array $parameters, $path = '', $indent = 12)
|
Chris@0
|
1470 {
|
Chris@0
|
1471 $php = array();
|
Chris@0
|
1472 foreach ($parameters as $key => $value) {
|
Chris@0
|
1473 if (is_array($value)) {
|
Chris@0
|
1474 $value = $this->exportParameters($value, $path.'/'.$key, $indent + 4);
|
Chris@0
|
1475 } elseif ($value instanceof ArgumentInterface) {
|
Chris@0
|
1476 throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain special arguments. "%s" found in "%s".', get_class($value), $path.'/'.$key));
|
Chris@0
|
1477 } elseif ($value instanceof Variable) {
|
Chris@0
|
1478 throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain variable references. Variable "%s" found in "%s".', $value, $path.'/'.$key));
|
Chris@0
|
1479 } elseif ($value instanceof Definition) {
|
Chris@0
|
1480 throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain service definitions. Definition for "%s" found in "%s".', $value->getClass(), $path.'/'.$key));
|
Chris@0
|
1481 } elseif ($value instanceof Reference) {
|
Chris@0
|
1482 throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain references to other services (reference to service "%s" found in "%s").', $value, $path.'/'.$key));
|
Chris@0
|
1483 } elseif ($value instanceof Expression) {
|
Chris@0
|
1484 throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain expressions. Expression "%s" found in "%s".', $value, $path.'/'.$key));
|
Chris@0
|
1485 } else {
|
Chris@0
|
1486 $value = $this->export($value);
|
Chris@0
|
1487 }
|
Chris@0
|
1488
|
Chris@0
|
1489 $php[] = sprintf('%s%s => %s,', str_repeat(' ', $indent), $this->export($key), $value);
|
Chris@0
|
1490 }
|
Chris@0
|
1491
|
Chris@0
|
1492 return sprintf("array(\n%s\n%s)", implode("\n", $php), str_repeat(' ', $indent - 4));
|
Chris@0
|
1493 }
|
Chris@0
|
1494
|
Chris@0
|
1495 /**
|
Chris@0
|
1496 * Ends the class definition.
|
Chris@0
|
1497 *
|
Chris@0
|
1498 * @return string
|
Chris@0
|
1499 */
|
Chris@0
|
1500 private function endClass()
|
Chris@0
|
1501 {
|
Chris@0
|
1502 return <<<'EOF'
|
Chris@0
|
1503 }
|
Chris@0
|
1504
|
Chris@0
|
1505 EOF;
|
Chris@0
|
1506 }
|
Chris@0
|
1507
|
Chris@0
|
1508 /**
|
Chris@0
|
1509 * Wraps the service conditionals.
|
Chris@0
|
1510 *
|
Chris@0
|
1511 * @param string $value
|
Chris@0
|
1512 * @param string $code
|
Chris@0
|
1513 *
|
Chris@0
|
1514 * @return string
|
Chris@0
|
1515 */
|
Chris@0
|
1516 private function wrapServiceConditionals($value, $code)
|
Chris@0
|
1517 {
|
Chris@0
|
1518 if (!$condition = $this->getServiceConditionals($value)) {
|
Chris@0
|
1519 return $code;
|
Chris@0
|
1520 }
|
Chris@0
|
1521
|
Chris@0
|
1522 // re-indent the wrapped code
|
Chris@0
|
1523 $code = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code)));
|
Chris@0
|
1524
|
Chris@0
|
1525 return sprintf(" if (%s) {\n%s }\n", $condition, $code);
|
Chris@0
|
1526 }
|
Chris@0
|
1527
|
Chris@0
|
1528 /**
|
Chris@0
|
1529 * Get the conditions to execute for conditional services.
|
Chris@0
|
1530 *
|
Chris@0
|
1531 * @param string $value
|
Chris@0
|
1532 *
|
Chris@0
|
1533 * @return null|string
|
Chris@0
|
1534 */
|
Chris@0
|
1535 private function getServiceConditionals($value)
|
Chris@0
|
1536 {
|
Chris@0
|
1537 $conditions = array();
|
Chris@0
|
1538 foreach (ContainerBuilder::getInitializedConditionals($value) as $service) {
|
Chris@0
|
1539 if (!$this->container->hasDefinition($service)) {
|
Chris@0
|
1540 return 'false';
|
Chris@0
|
1541 }
|
Chris@0
|
1542 $conditions[] = sprintf("isset(\$this->services['%s'])", $service);
|
Chris@0
|
1543 }
|
Chris@0
|
1544 foreach (ContainerBuilder::getServiceConditionals($value) as $service) {
|
Chris@0
|
1545 if ($this->container->hasDefinition($service) && !$this->container->getDefinition($service)->isPublic()) {
|
Chris@0
|
1546 continue;
|
Chris@0
|
1547 }
|
Chris@0
|
1548
|
Chris@0
|
1549 $conditions[] = sprintf("\$this->has('%s')", $service);
|
Chris@0
|
1550 }
|
Chris@0
|
1551
|
Chris@0
|
1552 if (!$conditions) {
|
Chris@0
|
1553 return '';
|
Chris@0
|
1554 }
|
Chris@0
|
1555
|
Chris@0
|
1556 return implode(' && ', $conditions);
|
Chris@0
|
1557 }
|
Chris@0
|
1558
|
Chris@0
|
1559 /**
|
Chris@0
|
1560 * Builds service calls from arguments.
|
Chris@0
|
1561 */
|
Chris@0
|
1562 private function getServiceCallsFromArguments(array $arguments, array &$calls, $isPreInstance, $callerId, array &$behavior = array(), $step = 1)
|
Chris@0
|
1563 {
|
Chris@0
|
1564 foreach ($arguments as $argument) {
|
Chris@0
|
1565 if (is_array($argument)) {
|
Chris@0
|
1566 $this->getServiceCallsFromArguments($argument, $calls, $isPreInstance, $callerId, $behavior, $step);
|
Chris@0
|
1567 } elseif ($argument instanceof Reference) {
|
Chris@0
|
1568 $id = $this->container->normalizeId($argument);
|
Chris@0
|
1569
|
Chris@0
|
1570 if (!isset($calls[$id])) {
|
Chris@0
|
1571 $calls[$id] = (int) ($isPreInstance && isset($this->circularReferences[$callerId][$id]));
|
Chris@0
|
1572 }
|
Chris@0
|
1573 if (!isset($behavior[$id])) {
|
Chris@0
|
1574 $behavior[$id] = $argument->getInvalidBehavior();
|
Chris@0
|
1575 } else {
|
Chris@0
|
1576 $behavior[$id] = min($behavior[$id], $argument->getInvalidBehavior());
|
Chris@0
|
1577 }
|
Chris@0
|
1578
|
Chris@0
|
1579 $calls[$id] += $step;
|
Chris@0
|
1580 }
|
Chris@0
|
1581 }
|
Chris@0
|
1582 }
|
Chris@0
|
1583
|
Chris@0
|
1584 private function getDefinitionsFromArguments(array $arguments, \SplObjectStorage $definitions = null)
|
Chris@0
|
1585 {
|
Chris@0
|
1586 if (null === $definitions) {
|
Chris@0
|
1587 $definitions = new \SplObjectStorage();
|
Chris@0
|
1588 }
|
Chris@0
|
1589
|
Chris@0
|
1590 foreach ($arguments as $argument) {
|
Chris@0
|
1591 if (is_array($argument)) {
|
Chris@0
|
1592 $this->getDefinitionsFromArguments($argument, $definitions);
|
Chris@0
|
1593 } elseif (!$argument instanceof Definition) {
|
Chris@0
|
1594 // no-op
|
Chris@0
|
1595 } elseif (isset($definitions[$argument])) {
|
Chris@0
|
1596 $definitions[$argument] = 1 + $definitions[$argument];
|
Chris@0
|
1597 } else {
|
Chris@0
|
1598 $definitions[$argument] = 1;
|
Chris@0
|
1599 $this->getDefinitionsFromArguments($argument->getArguments(), $definitions);
|
Chris@0
|
1600 $this->getDefinitionsFromArguments(array($argument->getFactory()), $definitions);
|
Chris@0
|
1601 $this->getDefinitionsFromArguments($argument->getProperties(), $definitions);
|
Chris@0
|
1602 $this->getDefinitionsFromArguments($argument->getMethodCalls(), $definitions);
|
Chris@0
|
1603 $this->getDefinitionsFromArguments(array($argument->getConfigurator()), $definitions);
|
Chris@0
|
1604 // move current definition last in the list
|
Chris@0
|
1605 $nbOccurences = $definitions[$argument];
|
Chris@0
|
1606 unset($definitions[$argument]);
|
Chris@0
|
1607 $definitions[$argument] = $nbOccurences;
|
Chris@0
|
1608 }
|
Chris@0
|
1609 }
|
Chris@0
|
1610
|
Chris@0
|
1611 return $definitions;
|
Chris@0
|
1612 }
|
Chris@0
|
1613
|
Chris@0
|
1614 /**
|
Chris@0
|
1615 * Checks if a service id has a reference.
|
Chris@0
|
1616 *
|
Chris@0
|
1617 * @param string $id
|
Chris@0
|
1618 * @param array $arguments
|
Chris@0
|
1619 * @param bool $deep
|
Chris@0
|
1620 * @param array $visited
|
Chris@0
|
1621 *
|
Chris@0
|
1622 * @return bool
|
Chris@0
|
1623 */
|
Chris@0
|
1624 private function hasReference($id, array $arguments, $deep = false, array &$visited = array())
|
Chris@0
|
1625 {
|
Chris@0
|
1626 if (!isset($this->circularReferences[$id])) {
|
Chris@0
|
1627 return false;
|
Chris@0
|
1628 }
|
Chris@0
|
1629
|
Chris@0
|
1630 foreach ($arguments as $argument) {
|
Chris@0
|
1631 if (is_array($argument)) {
|
Chris@0
|
1632 if ($this->hasReference($id, $argument, $deep, $visited)) {
|
Chris@0
|
1633 return true;
|
Chris@0
|
1634 }
|
Chris@0
|
1635
|
Chris@0
|
1636 continue;
|
Chris@0
|
1637 } elseif ($argument instanceof Reference) {
|
Chris@0
|
1638 $argumentId = $this->container->normalizeId($argument);
|
Chris@0
|
1639 if ($id === $argumentId) {
|
Chris@0
|
1640 return true;
|
Chris@0
|
1641 }
|
Chris@0
|
1642
|
Chris@0
|
1643 if (!$deep || isset($visited[$argumentId]) || !isset($this->circularReferences[$id][$argumentId])) {
|
Chris@0
|
1644 continue;
|
Chris@0
|
1645 }
|
Chris@0
|
1646
|
Chris@0
|
1647 $visited[$argumentId] = true;
|
Chris@0
|
1648
|
Chris@0
|
1649 $service = $this->container->getDefinition($argumentId);
|
Chris@0
|
1650 } elseif ($argument instanceof Definition) {
|
Chris@0
|
1651 $service = $argument;
|
Chris@0
|
1652 } else {
|
Chris@0
|
1653 continue;
|
Chris@0
|
1654 }
|
Chris@0
|
1655
|
Chris@0
|
1656 // if the proxy manager is enabled, disable searching for references in lazy services,
|
Chris@0
|
1657 // as these services will be instantiated lazily and don't have direct related references.
|
Chris@0
|
1658 if ($service->isLazy() && !$this->getProxyDumper() instanceof NullDumper) {
|
Chris@0
|
1659 continue;
|
Chris@0
|
1660 }
|
Chris@0
|
1661
|
Chris@0
|
1662 if ($this->hasReference($id, array($service->getArguments(), $service->getFactory(), $service->getProperties(), $service->getMethodCalls(), $service->getConfigurator()), $deep, $visited)) {
|
Chris@0
|
1663 return true;
|
Chris@0
|
1664 }
|
Chris@0
|
1665 }
|
Chris@0
|
1666
|
Chris@0
|
1667 return false;
|
Chris@0
|
1668 }
|
Chris@0
|
1669
|
Chris@0
|
1670 /**
|
Chris@0
|
1671 * Dumps values.
|
Chris@0
|
1672 *
|
Chris@0
|
1673 * @param mixed $value
|
Chris@0
|
1674 * @param bool $interpolate
|
Chris@0
|
1675 *
|
Chris@0
|
1676 * @return string
|
Chris@0
|
1677 *
|
Chris@0
|
1678 * @throws RuntimeException
|
Chris@0
|
1679 */
|
Chris@0
|
1680 private function dumpValue($value, $interpolate = true)
|
Chris@0
|
1681 {
|
Chris@0
|
1682 if (is_array($value)) {
|
Chris@0
|
1683 if ($value && $interpolate && false !== $param = array_search($value, $this->container->getParameterBag()->all(), true)) {
|
Chris@0
|
1684 return $this->dumpValue("%$param%");
|
Chris@0
|
1685 }
|
Chris@0
|
1686 $code = array();
|
Chris@0
|
1687 foreach ($value as $k => $v) {
|
Chris@0
|
1688 $code[] = sprintf('%s => %s', $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate));
|
Chris@0
|
1689 }
|
Chris@0
|
1690
|
Chris@0
|
1691 return sprintf('array(%s)', implode(', ', $code));
|
Chris@0
|
1692 } elseif ($value instanceof ArgumentInterface) {
|
Chris@0
|
1693 $scope = array($this->definitionVariables, $this->referenceVariables, $this->variableCount);
|
Chris@0
|
1694 $this->definitionVariables = $this->referenceVariables = null;
|
Chris@0
|
1695
|
Chris@0
|
1696 try {
|
Chris@0
|
1697 if ($value instanceof ServiceClosureArgument) {
|
Chris@0
|
1698 $value = $value->getValues()[0];
|
Chris@0
|
1699 $code = $this->dumpValue($value, $interpolate);
|
Chris@0
|
1700
|
Chris@0
|
1701 if ($value instanceof TypedReference) {
|
Chris@0
|
1702 $code = sprintf('$f = function (\\%s $v%s) { return $v; }; return $f(%s);', $value->getType(), ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $value->getInvalidBehavior() ? ' = null' : '', $code);
|
Chris@0
|
1703 } else {
|
Chris@0
|
1704 $code = sprintf('return %s;', $code);
|
Chris@0
|
1705 }
|
Chris@0
|
1706
|
Chris@0
|
1707 return sprintf("function () {\n %s\n }", $code);
|
Chris@0
|
1708 }
|
Chris@0
|
1709
|
Chris@0
|
1710 if ($value instanceof IteratorArgument) {
|
Chris@0
|
1711 $operands = array(0);
|
Chris@0
|
1712 $code = array();
|
Chris@0
|
1713 $code[] = 'new RewindableGenerator(function () {';
|
Chris@0
|
1714
|
Chris@0
|
1715 if (!$values = $value->getValues()) {
|
Chris@0
|
1716 $code[] = ' return new \EmptyIterator();';
|
Chris@0
|
1717 } else {
|
Chris@0
|
1718 $countCode = array();
|
Chris@0
|
1719 $countCode[] = 'function () {';
|
Chris@0
|
1720
|
Chris@0
|
1721 foreach ($values as $k => $v) {
|
Chris@0
|
1722 ($c = $this->getServiceConditionals($v)) ? $operands[] = "(int) ($c)" : ++$operands[0];
|
Chris@0
|
1723 $v = $this->wrapServiceConditionals($v, sprintf(" yield %s => %s;\n", $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate)));
|
Chris@0
|
1724 foreach (explode("\n", $v) as $v) {
|
Chris@0
|
1725 if ($v) {
|
Chris@0
|
1726 $code[] = ' '.$v;
|
Chris@0
|
1727 }
|
Chris@0
|
1728 }
|
Chris@0
|
1729 }
|
Chris@0
|
1730
|
Chris@0
|
1731 $countCode[] = sprintf(' return %s;', implode(' + ', $operands));
|
Chris@0
|
1732 $countCode[] = ' }';
|
Chris@0
|
1733 }
|
Chris@0
|
1734
|
Chris@0
|
1735 $code[] = sprintf(' }, %s)', count($operands) > 1 ? implode("\n", $countCode) : $operands[0]);
|
Chris@0
|
1736
|
Chris@0
|
1737 return implode("\n", $code);
|
Chris@0
|
1738 }
|
Chris@0
|
1739 } finally {
|
Chris@0
|
1740 list($this->definitionVariables, $this->referenceVariables, $this->variableCount) = $scope;
|
Chris@0
|
1741 }
|
Chris@0
|
1742 } elseif ($value instanceof Definition) {
|
Chris@0
|
1743 if (null !== $this->definitionVariables && $this->definitionVariables->contains($value)) {
|
Chris@0
|
1744 return $this->dumpValue($this->definitionVariables[$value], $interpolate);
|
Chris@0
|
1745 }
|
Chris@0
|
1746 if ($value->getMethodCalls()) {
|
Chris@0
|
1747 throw new RuntimeException('Cannot dump definitions which have method calls.');
|
Chris@0
|
1748 }
|
Chris@0
|
1749 if ($value->getProperties()) {
|
Chris@0
|
1750 throw new RuntimeException('Cannot dump definitions which have properties.');
|
Chris@0
|
1751 }
|
Chris@0
|
1752 if (null !== $value->getConfigurator()) {
|
Chris@0
|
1753 throw new RuntimeException('Cannot dump definitions which have a configurator.');
|
Chris@0
|
1754 }
|
Chris@0
|
1755
|
Chris@0
|
1756 $arguments = array();
|
Chris@0
|
1757 foreach ($value->getArguments() as $argument) {
|
Chris@0
|
1758 $arguments[] = $this->dumpValue($argument);
|
Chris@0
|
1759 }
|
Chris@0
|
1760
|
Chris@0
|
1761 if (null !== $value->getFactory()) {
|
Chris@0
|
1762 $factory = $value->getFactory();
|
Chris@0
|
1763
|
Chris@0
|
1764 if (is_string($factory)) {
|
Chris@0
|
1765 return sprintf('%s(%s)', $this->dumpLiteralClass($this->dumpValue($factory)), implode(', ', $arguments));
|
Chris@0
|
1766 }
|
Chris@0
|
1767
|
Chris@0
|
1768 if (is_array($factory)) {
|
Chris@0
|
1769 if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $factory[1])) {
|
Chris@0
|
1770 throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s)', $factory[1] ?: 'n/a'));
|
Chris@0
|
1771 }
|
Chris@0
|
1772
|
Chris@0
|
1773 $class = $this->dumpValue($factory[0]);
|
Chris@0
|
1774 if (is_string($factory[0])) {
|
Chris@0
|
1775 return sprintf('%s::%s(%s)', $this->dumpLiteralClass($class), $factory[1], implode(', ', $arguments));
|
Chris@0
|
1776 }
|
Chris@0
|
1777
|
Chris@0
|
1778 if ($factory[0] instanceof Definition) {
|
Chris@0
|
1779 if (0 === strpos($class, 'new ')) {
|
Chris@0
|
1780 return sprintf('(%s)->%s(%s)', $class, $factory[1], implode(', ', $arguments));
|
Chris@0
|
1781 }
|
Chris@0
|
1782
|
Chris@0
|
1783 return sprintf("\\call_user_func(array(%s, '%s')%s)", $class, $factory[1], count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
|
Chris@0
|
1784 }
|
Chris@0
|
1785
|
Chris@0
|
1786 if ($factory[0] instanceof Reference) {
|
Chris@0
|
1787 return sprintf('%s->%s(%s)', $class, $factory[1], implode(', ', $arguments));
|
Chris@0
|
1788 }
|
Chris@0
|
1789 }
|
Chris@0
|
1790
|
Chris@0
|
1791 throw new RuntimeException('Cannot dump definition because of invalid factory');
|
Chris@0
|
1792 }
|
Chris@0
|
1793
|
Chris@0
|
1794 $class = $value->getClass();
|
Chris@0
|
1795 if (null === $class) {
|
Chris@0
|
1796 throw new RuntimeException('Cannot dump definitions which have no class nor factory.');
|
Chris@0
|
1797 }
|
Chris@0
|
1798
|
Chris@0
|
1799 return sprintf('new %s(%s)', $this->dumpLiteralClass($this->dumpValue($class)), implode(', ', $arguments));
|
Chris@0
|
1800 } elseif ($value instanceof Variable) {
|
Chris@0
|
1801 return '$'.$value;
|
Chris@0
|
1802 } elseif ($value instanceof Reference) {
|
Chris@0
|
1803 $id = $this->container->normalizeId($value);
|
Chris@0
|
1804 if (null !== $this->referenceVariables && isset($this->referenceVariables[$id])) {
|
Chris@0
|
1805 return $this->dumpValue($this->referenceVariables[$id], $interpolate);
|
Chris@0
|
1806 }
|
Chris@0
|
1807
|
Chris@0
|
1808 return $this->getServiceCall($id, $value);
|
Chris@0
|
1809 } elseif ($value instanceof Expression) {
|
Chris@0
|
1810 return $this->getExpressionLanguage()->compile((string) $value, array('this' => 'container'));
|
Chris@0
|
1811 } elseif ($value instanceof Parameter) {
|
Chris@0
|
1812 return $this->dumpParameter($value);
|
Chris@0
|
1813 } elseif (true === $interpolate && is_string($value)) {
|
Chris@0
|
1814 if (preg_match('/^%([^%]+)%$/', $value, $match)) {
|
Chris@0
|
1815 // we do this to deal with non string values (Boolean, integer, ...)
|
Chris@0
|
1816 // the preg_replace_callback converts them to strings
|
Chris@0
|
1817 return $this->dumpParameter($match[1]);
|
Chris@0
|
1818 } else {
|
Chris@0
|
1819 $replaceParameters = function ($match) {
|
Chris@0
|
1820 return "'.".$this->dumpParameter($match[2]).".'";
|
Chris@0
|
1821 };
|
Chris@0
|
1822
|
Chris@0
|
1823 $code = str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\1/', $replaceParameters, $this->export($value)));
|
Chris@0
|
1824
|
Chris@0
|
1825 return $code;
|
Chris@0
|
1826 }
|
Chris@0
|
1827 } elseif (is_object($value) || is_resource($value)) {
|
Chris@0
|
1828 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
|
Chris@0
|
1829 }
|
Chris@0
|
1830
|
Chris@0
|
1831 return $this->export($value);
|
Chris@0
|
1832 }
|
Chris@0
|
1833
|
Chris@0
|
1834 /**
|
Chris@0
|
1835 * Dumps a string to a literal (aka PHP Code) class value.
|
Chris@0
|
1836 *
|
Chris@0
|
1837 * @param string $class
|
Chris@0
|
1838 *
|
Chris@0
|
1839 * @return string
|
Chris@0
|
1840 *
|
Chris@0
|
1841 * @throws RuntimeException
|
Chris@0
|
1842 */
|
Chris@0
|
1843 private function dumpLiteralClass($class)
|
Chris@0
|
1844 {
|
Chris@0
|
1845 if (false !== strpos($class, '$')) {
|
Chris@0
|
1846 return sprintf('${($_ = %s) && false ?: "_"}', $class);
|
Chris@0
|
1847 }
|
Chris@0
|
1848 if (0 !== strpos($class, "'") || !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
|
Chris@0
|
1849 throw new RuntimeException(sprintf('Cannot dump definition because of invalid class name (%s)', $class ?: 'n/a'));
|
Chris@0
|
1850 }
|
Chris@0
|
1851
|
Chris@0
|
1852 $class = substr(str_replace('\\\\', '\\', $class), 1, -1);
|
Chris@0
|
1853
|
Chris@0
|
1854 return 0 === strpos($class, '\\') ? $class : '\\'.$class;
|
Chris@0
|
1855 }
|
Chris@0
|
1856
|
Chris@0
|
1857 /**
|
Chris@0
|
1858 * Dumps a parameter.
|
Chris@0
|
1859 *
|
Chris@0
|
1860 * @param string $name
|
Chris@0
|
1861 *
|
Chris@0
|
1862 * @return string
|
Chris@0
|
1863 */
|
Chris@0
|
1864 private function dumpParameter($name)
|
Chris@0
|
1865 {
|
Chris@0
|
1866 if ($this->container->isCompiled() && $this->container->hasParameter($name)) {
|
Chris@0
|
1867 $value = $this->container->getParameter($name);
|
Chris@0
|
1868 $dumpedValue = $this->dumpValue($value, false);
|
Chris@0
|
1869
|
Chris@0
|
1870 if (!$value || !is_array($value)) {
|
Chris@0
|
1871 return $dumpedValue;
|
Chris@0
|
1872 }
|
Chris@0
|
1873
|
Chris@0
|
1874 if (!preg_match("/\\\$this->(?:getEnv\('(?:\w++:)*+\w++'\)|targetDirs\[\d++\])/", $dumpedValue)) {
|
Chris@0
|
1875 return sprintf("\$this->parameters['%s']", $name);
|
Chris@0
|
1876 }
|
Chris@0
|
1877 }
|
Chris@0
|
1878
|
Chris@0
|
1879 return sprintf("\$this->getParameter('%s')", $name);
|
Chris@0
|
1880 }
|
Chris@0
|
1881
|
Chris@0
|
1882 /**
|
Chris@0
|
1883 * Gets a service call.
|
Chris@0
|
1884 *
|
Chris@0
|
1885 * @param string $id
|
Chris@0
|
1886 * @param Reference $reference
|
Chris@0
|
1887 *
|
Chris@0
|
1888 * @return string
|
Chris@0
|
1889 */
|
Chris@0
|
1890 private function getServiceCall($id, Reference $reference = null)
|
Chris@0
|
1891 {
|
Chris@0
|
1892 while ($this->container->hasAlias($id)) {
|
Chris@0
|
1893 $id = (string) $this->container->getAlias($id);
|
Chris@0
|
1894 }
|
Chris@0
|
1895 $id = $this->container->normalizeId($id);
|
Chris@0
|
1896
|
Chris@0
|
1897 if ('service_container' === $id) {
|
Chris@0
|
1898 return '$this';
|
Chris@0
|
1899 }
|
Chris@0
|
1900
|
Chris@0
|
1901 if ($this->container->hasDefinition($id) && ($definition = $this->container->getDefinition($id)) && !$definition->isSynthetic()) {
|
Chris@0
|
1902 if (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
|
Chris@0
|
1903 $code = 'null';
|
Chris@0
|
1904 if (!$definition->isShared()) {
|
Chris@0
|
1905 return $code;
|
Chris@0
|
1906 }
|
Chris@0
|
1907 } elseif ($this->isTrivialInstance($definition)) {
|
Chris@0
|
1908 $code = substr($this->addNewInstance($definition, '', '', $id), 8, -2);
|
Chris@0
|
1909 if ($definition->isShared()) {
|
Chris@0
|
1910 $code = sprintf('$this->services[\'%s\'] = %s', $id, $code);
|
Chris@0
|
1911 }
|
Chris@0
|
1912 } elseif ($this->asFiles && $definition->isShared() && !$this->isHotPath($definition)) {
|
Chris@0
|
1913 $code = sprintf("\$this->load('%s.php')", $this->generateMethodName($id));
|
Chris@0
|
1914 } else {
|
Chris@0
|
1915 $code = sprintf('$this->%s()', $this->generateMethodName($id));
|
Chris@0
|
1916 }
|
Chris@0
|
1917 } elseif (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
|
Chris@0
|
1918 return 'null';
|
Chris@0
|
1919 } elseif (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
|
Chris@0
|
1920 $code = sprintf('$this->get(\'%s\', /* ContainerInterface::NULL_ON_INVALID_REFERENCE */ %d)', $id, ContainerInterface::NULL_ON_INVALID_REFERENCE);
|
Chris@0
|
1921 } else {
|
Chris@0
|
1922 $code = sprintf('$this->get(\'%s\')', $id);
|
Chris@0
|
1923 }
|
Chris@0
|
1924
|
Chris@0
|
1925 // The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? $code)" on PHP>=7.0
|
Chris@0
|
1926
|
Chris@0
|
1927 return "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : $code) && false ?: '_'}";
|
Chris@0
|
1928 }
|
Chris@0
|
1929
|
Chris@0
|
1930 /**
|
Chris@0
|
1931 * Initializes the method names map to avoid conflicts with the Container methods.
|
Chris@0
|
1932 *
|
Chris@0
|
1933 * @param string $class the container base class
|
Chris@0
|
1934 */
|
Chris@0
|
1935 private function initializeMethodNamesMap($class)
|
Chris@0
|
1936 {
|
Chris@0
|
1937 $this->serviceIdToMethodNameMap = array();
|
Chris@0
|
1938 $this->usedMethodNames = array();
|
Chris@0
|
1939
|
Chris@0
|
1940 if ($reflectionClass = $this->container->getReflectionClass($class)) {
|
Chris@0
|
1941 foreach ($reflectionClass->getMethods() as $method) {
|
Chris@0
|
1942 $this->usedMethodNames[strtolower($method->getName())] = true;
|
Chris@0
|
1943 }
|
Chris@0
|
1944 }
|
Chris@0
|
1945 }
|
Chris@0
|
1946
|
Chris@0
|
1947 /**
|
Chris@0
|
1948 * Convert a service id to a valid PHP method name.
|
Chris@0
|
1949 *
|
Chris@0
|
1950 * @param string $id
|
Chris@0
|
1951 *
|
Chris@0
|
1952 * @return string
|
Chris@0
|
1953 *
|
Chris@0
|
1954 * @throws InvalidArgumentException
|
Chris@0
|
1955 */
|
Chris@0
|
1956 private function generateMethodName($id)
|
Chris@0
|
1957 {
|
Chris@0
|
1958 if (isset($this->serviceIdToMethodNameMap[$id])) {
|
Chris@0
|
1959 return $this->serviceIdToMethodNameMap[$id];
|
Chris@0
|
1960 }
|
Chris@0
|
1961
|
Chris@0
|
1962 $i = strrpos($id, '\\');
|
Chris@0
|
1963 $name = Container::camelize(false !== $i && isset($id[1 + $i]) ? substr($id, 1 + $i) : $id);
|
Chris@0
|
1964 $name = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '', $name);
|
Chris@0
|
1965 $methodName = 'get'.$name.'Service';
|
Chris@0
|
1966 $suffix = 1;
|
Chris@0
|
1967
|
Chris@0
|
1968 while (isset($this->usedMethodNames[strtolower($methodName)])) {
|
Chris@0
|
1969 ++$suffix;
|
Chris@0
|
1970 $methodName = 'get'.$name.$suffix.'Service';
|
Chris@0
|
1971 }
|
Chris@0
|
1972
|
Chris@0
|
1973 $this->serviceIdToMethodNameMap[$id] = $methodName;
|
Chris@0
|
1974 $this->usedMethodNames[strtolower($methodName)] = true;
|
Chris@0
|
1975
|
Chris@0
|
1976 return $methodName;
|
Chris@0
|
1977 }
|
Chris@0
|
1978
|
Chris@0
|
1979 /**
|
Chris@0
|
1980 * Returns the next name to use.
|
Chris@0
|
1981 *
|
Chris@0
|
1982 * @return string
|
Chris@0
|
1983 */
|
Chris@0
|
1984 private function getNextVariableName()
|
Chris@0
|
1985 {
|
Chris@0
|
1986 $firstChars = self::FIRST_CHARS;
|
Chris@0
|
1987 $firstCharsLength = strlen($firstChars);
|
Chris@0
|
1988 $nonFirstChars = self::NON_FIRST_CHARS;
|
Chris@0
|
1989 $nonFirstCharsLength = strlen($nonFirstChars);
|
Chris@0
|
1990
|
Chris@0
|
1991 while (true) {
|
Chris@0
|
1992 $name = '';
|
Chris@0
|
1993 $i = $this->variableCount;
|
Chris@0
|
1994
|
Chris@0
|
1995 if ('' === $name) {
|
Chris@0
|
1996 $name .= $firstChars[$i % $firstCharsLength];
|
Chris@0
|
1997 $i = (int) ($i / $firstCharsLength);
|
Chris@0
|
1998 }
|
Chris@0
|
1999
|
Chris@0
|
2000 while ($i > 0) {
|
Chris@0
|
2001 --$i;
|
Chris@0
|
2002 $name .= $nonFirstChars[$i % $nonFirstCharsLength];
|
Chris@0
|
2003 $i = (int) ($i / $nonFirstCharsLength);
|
Chris@0
|
2004 }
|
Chris@0
|
2005
|
Chris@0
|
2006 ++$this->variableCount;
|
Chris@0
|
2007
|
Chris@0
|
2008 // check that the name is not reserved
|
Chris@0
|
2009 if (in_array($name, $this->reservedVariables, true)) {
|
Chris@0
|
2010 continue;
|
Chris@0
|
2011 }
|
Chris@0
|
2012
|
Chris@0
|
2013 return $name;
|
Chris@0
|
2014 }
|
Chris@0
|
2015 }
|
Chris@0
|
2016
|
Chris@0
|
2017 private function getExpressionLanguage()
|
Chris@0
|
2018 {
|
Chris@0
|
2019 if (null === $this->expressionLanguage) {
|
Chris@0
|
2020 if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
|
Chris@0
|
2021 throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
|
Chris@0
|
2022 }
|
Chris@0
|
2023 $providers = $this->container->getExpressionLanguageProviders();
|
Chris@0
|
2024 $this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
|
Chris@0
|
2025 $id = '""' === substr_replace($arg, '', 1, -1) ? stripcslashes(substr($arg, 1, -1)) : null;
|
Chris@0
|
2026
|
Chris@0
|
2027 if (null !== $id && ($this->container->hasAlias($id) || $this->container->hasDefinition($id))) {
|
Chris@0
|
2028 return $this->getServiceCall($id);
|
Chris@0
|
2029 }
|
Chris@0
|
2030
|
Chris@0
|
2031 return sprintf('$this->get(%s)', $arg);
|
Chris@0
|
2032 });
|
Chris@0
|
2033
|
Chris@0
|
2034 if ($this->container->isTrackingResources()) {
|
Chris@0
|
2035 foreach ($providers as $provider) {
|
Chris@0
|
2036 $this->container->addObjectResource($provider);
|
Chris@0
|
2037 }
|
Chris@0
|
2038 }
|
Chris@0
|
2039 }
|
Chris@0
|
2040
|
Chris@0
|
2041 return $this->expressionLanguage;
|
Chris@0
|
2042 }
|
Chris@0
|
2043
|
Chris@0
|
2044 private function isHotPath(Definition $definition)
|
Chris@0
|
2045 {
|
Chris@0
|
2046 return $this->hotPathTag && $definition->hasTag($this->hotPathTag) && !$definition->isDeprecated();
|
Chris@0
|
2047 }
|
Chris@0
|
2048
|
Chris@0
|
2049 private function export($value)
|
Chris@0
|
2050 {
|
Chris@0
|
2051 if (null !== $this->targetDirRegex && is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) {
|
Chris@0
|
2052 $prefix = $matches[0][1] ? $this->doExport(substr($value, 0, $matches[0][1]), true).'.' : '';
|
Chris@0
|
2053 $suffix = $matches[0][1] + strlen($matches[0][0]);
|
Chris@0
|
2054 $suffix = isset($value[$suffix]) ? '.'.$this->doExport(substr($value, $suffix), true) : '';
|
Chris@0
|
2055 $dirname = $this->asFiles ? '$this->containerDir' : '__DIR__';
|
Chris@0
|
2056 $offset = 1 + $this->targetDirMaxMatches - count($matches);
|
Chris@0
|
2057
|
Chris@0
|
2058 if ($this->asFiles || 0 < $offset) {
|
Chris@0
|
2059 $dirname = sprintf('$this->targetDirs[%d]', $offset);
|
Chris@0
|
2060 }
|
Chris@0
|
2061
|
Chris@0
|
2062 if ($prefix || $suffix) {
|
Chris@0
|
2063 return sprintf('(%s%s%s)', $prefix, $dirname, $suffix);
|
Chris@0
|
2064 }
|
Chris@0
|
2065
|
Chris@0
|
2066 return $dirname;
|
Chris@0
|
2067 }
|
Chris@0
|
2068
|
Chris@0
|
2069 return $this->doExport($value, true);
|
Chris@0
|
2070 }
|
Chris@0
|
2071
|
Chris@0
|
2072 private function doExport($value, $resolveEnv = false)
|
Chris@0
|
2073 {
|
Chris@0
|
2074 if (is_string($value) && false !== strpos($value, "\n")) {
|
Chris@0
|
2075 $cleanParts = explode("\n", $value);
|
Chris@0
|
2076 $cleanParts = array_map(function ($part) { return var_export($part, true); }, $cleanParts);
|
Chris@0
|
2077 $export = implode('."\n".', $cleanParts);
|
Chris@0
|
2078 } else {
|
Chris@0
|
2079 $export = var_export($value, true);
|
Chris@0
|
2080 }
|
Chris@0
|
2081
|
Chris@0
|
2082 if ($resolveEnv && "'" === $export[0] && $export !== $resolvedExport = $this->container->resolveEnvPlaceholders($export, "'.\$this->getEnv('string:%s').'")) {
|
Chris@0
|
2083 $export = $resolvedExport;
|
Chris@0
|
2084 if (".''" === substr($export, -3)) {
|
Chris@0
|
2085 $export = substr($export, 0, -3);
|
Chris@0
|
2086 if ("'" === $export[1]) {
|
Chris@0
|
2087 $export = substr_replace($export, '', 18, 7);
|
Chris@0
|
2088 }
|
Chris@0
|
2089 }
|
Chris@0
|
2090 if ("'" === $export[1]) {
|
Chris@0
|
2091 $export = substr($export, 3);
|
Chris@0
|
2092 }
|
Chris@0
|
2093 }
|
Chris@0
|
2094
|
Chris@0
|
2095 return $export;
|
Chris@0
|
2096 }
|
Chris@0
|
2097 }
|