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