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