Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\DependencyInjection\Dumper;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\DependencyInjection\Variable;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Definition;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@0
|
17 use Symfony\Component\DependencyInjection\Container;
|
Chris@0
|
18 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
19 use Symfony\Component\DependencyInjection\Reference;
|
Chris@0
|
20 use Symfony\Component\DependencyInjection\Parameter;
|
Chris@0
|
21 use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
|
Chris@0
|
22 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
23 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
24 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
Chris@0
|
25 use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface as ProxyDumper;
|
Chris@0
|
26 use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\NullDumper;
|
Chris@0
|
27 use Symfony\Component\DependencyInjection\ExpressionLanguage;
|
Chris@0
|
28 use Symfony\Component\ExpressionLanguage\Expression;
|
Chris@0
|
29 use Symfony\Component\HttpKernel\Kernel;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * PhpDumper dumps a service container as a PHP class.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
35 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
Chris@0
|
36 */
|
Chris@0
|
37 class PhpDumper extends Dumper
|
Chris@0
|
38 {
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Characters that might appear in the generated variable name as first character.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var string
|
Chris@0
|
43 */
|
Chris@0
|
44 const FIRST_CHARS = 'abcdefghijklmnopqrstuvwxyz';
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Characters that might appear in the generated variable name as any but the first character.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @var string
|
Chris@0
|
50 */
|
Chris@0
|
51 const NON_FIRST_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789_';
|
Chris@0
|
52
|
Chris@0
|
53 private $inlinedDefinitions;
|
Chris@0
|
54 private $definitionVariables;
|
Chris@0
|
55 private $referenceVariables;
|
Chris@0
|
56 private $variableCount;
|
Chris@0
|
57 private $reservedVariables = array('instance', 'class');
|
Chris@0
|
58 private $expressionLanguage;
|
Chris@0
|
59 private $targetDirRegex;
|
Chris@0
|
60 private $targetDirMaxMatches;
|
Chris@0
|
61 private $docStar;
|
Chris@0
|
62 private $serviceIdToMethodNameMap;
|
Chris@0
|
63 private $usedMethodNames;
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
|
Chris@0
|
67 */
|
Chris@0
|
68 private $proxyDumper;
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * {@inheritdoc}
|
Chris@0
|
72 */
|
Chris@0
|
73 public function __construct(ContainerBuilder $container)
|
Chris@0
|
74 {
|
Chris@0
|
75 parent::__construct($container);
|
Chris@0
|
76
|
Chris@0
|
77 $this->inlinedDefinitions = new \SplObjectStorage();
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Sets the dumper to be used when dumping proxies in the generated container.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @param ProxyDumper $proxyDumper
|
Chris@0
|
84 */
|
Chris@0
|
85 public function setProxyDumper(ProxyDumper $proxyDumper)
|
Chris@0
|
86 {
|
Chris@0
|
87 $this->proxyDumper = $proxyDumper;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Dumps the service container as a PHP class.
|
Chris@0
|
92 *
|
Chris@0
|
93 * Available options:
|
Chris@0
|
94 *
|
Chris@0
|
95 * * class: The class name
|
Chris@0
|
96 * * base_class: The base class name
|
Chris@0
|
97 * * namespace: The class namespace
|
Chris@0
|
98 *
|
Chris@0
|
99 * @param array $options An array of options
|
Chris@0
|
100 *
|
Chris@0
|
101 * @return string A PHP class representing of the service container
|
Chris@0
|
102 *
|
Chris@0
|
103 * @throws EnvParameterException When an env var exists but has not been dumped
|
Chris@0
|
104 */
|
Chris@0
|
105 public function dump(array $options = array())
|
Chris@0
|
106 {
|
Chris@0
|
107 $this->targetDirRegex = null;
|
Chris@0
|
108 $options = array_merge(array(
|
Chris@0
|
109 'class' => 'ProjectServiceContainer',
|
Chris@0
|
110 'base_class' => 'Container',
|
Chris@0
|
111 'namespace' => '',
|
Chris@0
|
112 'debug' => true,
|
Chris@0
|
113 ), $options);
|
Chris@0
|
114
|
Chris@0
|
115 $this->initializeMethodNamesMap($options['base_class']);
|
Chris@0
|
116
|
Chris@0
|
117 $this->docStar = $options['debug'] ? '*' : '';
|
Chris@0
|
118
|
Chris@0
|
119 if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) {
|
Chris@0
|
120 // Build a regexp where the first root dirs are mandatory,
|
Chris@0
|
121 // but every other sub-dir is optional up to the full path in $dir
|
Chris@0
|
122 // Mandate at least 2 root dirs and not more that 5 optional dirs.
|
Chris@0
|
123
|
Chris@0
|
124 $dir = explode(DIRECTORY_SEPARATOR, realpath($dir));
|
Chris@0
|
125 $i = count($dir);
|
Chris@0
|
126
|
Chris@0
|
127 if (3 <= $i) {
|
Chris@0
|
128 $regex = '';
|
Chris@0
|
129 $lastOptionalDir = $i > 8 ? $i - 5 : 3;
|
Chris@0
|
130 $this->targetDirMaxMatches = $i - $lastOptionalDir;
|
Chris@0
|
131
|
Chris@0
|
132 while (--$i >= $lastOptionalDir) {
|
Chris@0
|
133 $regex = sprintf('(%s%s)?', preg_quote(DIRECTORY_SEPARATOR.$dir[$i], '#'), $regex);
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 do {
|
Chris@0
|
137 $regex = preg_quote(DIRECTORY_SEPARATOR.$dir[$i], '#').$regex;
|
Chris@0
|
138 } while (0 < --$i);
|
Chris@0
|
139
|
Chris@0
|
140 $this->targetDirRegex = '#'.preg_quote($dir[0], '#').$regex.'#';
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 $code = $this->startClass($options['class'], $options['base_class'], $options['namespace']);
|
Chris@0
|
145
|
Chris@0
|
146 if ($this->container->isFrozen()) {
|
Chris@0
|
147 $code .= $this->addFrozenConstructor();
|
Chris@0
|
148 $code .= $this->addFrozenCompile();
|
Chris@0
|
149 $code .= $this->addIsFrozenMethod();
|
Chris@0
|
150 } else {
|
Chris@0
|
151 $code .= $this->addConstructor();
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 $code .=
|
Chris@0
|
155 $this->addServices().
|
Chris@0
|
156 $this->addDefaultParametersMethod().
|
Chris@0
|
157 $this->endClass().
|
Chris@0
|
158 $this->addProxyClasses()
|
Chris@0
|
159 ;
|
Chris@0
|
160 $this->targetDirRegex = null;
|
Chris@0
|
161
|
Chris@0
|
162 $unusedEnvs = array();
|
Chris@0
|
163 foreach ($this->container->getEnvCounters() as $env => $use) {
|
Chris@0
|
164 if (!$use) {
|
Chris@0
|
165 $unusedEnvs[] = $env;
|
Chris@0
|
166 }
|
Chris@0
|
167 }
|
Chris@0
|
168 if ($unusedEnvs) {
|
Chris@0
|
169 throw new EnvParameterException($unusedEnvs);
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 return $code;
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 /**
|
Chris@0
|
176 * Retrieves the currently set proxy dumper or instantiates one.
|
Chris@0
|
177 *
|
Chris@0
|
178 * @return ProxyDumper
|
Chris@0
|
179 */
|
Chris@0
|
180 private function getProxyDumper()
|
Chris@0
|
181 {
|
Chris@0
|
182 if (!$this->proxyDumper) {
|
Chris@0
|
183 $this->proxyDumper = new NullDumper();
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 return $this->proxyDumper;
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 /**
|
Chris@0
|
190 * Generates Service local temp variables.
|
Chris@0
|
191 *
|
Chris@0
|
192 * @param string $cId
|
Chris@0
|
193 * @param string $definition
|
Chris@0
|
194 *
|
Chris@0
|
195 * @return string
|
Chris@0
|
196 */
|
Chris@0
|
197 private function addServiceLocalTempVariables($cId, $definition)
|
Chris@0
|
198 {
|
Chris@0
|
199 static $template = " \$%s = %s;\n";
|
Chris@0
|
200
|
Chris@0
|
201 $localDefinitions = array_merge(
|
Chris@0
|
202 array($definition),
|
Chris@0
|
203 $this->getInlinedDefinitions($definition)
|
Chris@0
|
204 );
|
Chris@0
|
205
|
Chris@0
|
206 $calls = $behavior = array();
|
Chris@0
|
207 foreach ($localDefinitions as $iDefinition) {
|
Chris@0
|
208 $this->getServiceCallsFromArguments($iDefinition->getArguments(), $calls, $behavior);
|
Chris@0
|
209 $this->getServiceCallsFromArguments($iDefinition->getMethodCalls(), $calls, $behavior);
|
Chris@0
|
210 $this->getServiceCallsFromArguments($iDefinition->getProperties(), $calls, $behavior);
|
Chris@0
|
211 $this->getServiceCallsFromArguments(array($iDefinition->getConfigurator()), $calls, $behavior);
|
Chris@0
|
212 $this->getServiceCallsFromArguments(array($iDefinition->getFactory()), $calls, $behavior);
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 $code = '';
|
Chris@0
|
216 foreach ($calls as $id => $callCount) {
|
Chris@0
|
217 if ('service_container' === $id || $id === $cId) {
|
Chris@0
|
218 continue;
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 if ($callCount > 1) {
|
Chris@0
|
222 $name = $this->getNextVariableName();
|
Chris@0
|
223 $this->referenceVariables[$id] = new Variable($name);
|
Chris@0
|
224
|
Chris@0
|
225 if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $behavior[$id]) {
|
Chris@0
|
226 $code .= sprintf($template, $name, $this->getServiceCall($id));
|
Chris@0
|
227 } else {
|
Chris@0
|
228 $code .= sprintf($template, $name, $this->getServiceCall($id, new Reference($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)));
|
Chris@0
|
229 }
|
Chris@0
|
230 }
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 if ('' !== $code) {
|
Chris@0
|
234 $code .= "\n";
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 return $code;
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 /**
|
Chris@0
|
241 * Generates code for the proxies to be attached after the container class.
|
Chris@0
|
242 *
|
Chris@0
|
243 * @return string
|
Chris@0
|
244 */
|
Chris@0
|
245 private function addProxyClasses()
|
Chris@0
|
246 {
|
Chris@0
|
247 /* @var $definitions Definition[] */
|
Chris@0
|
248 $definitions = array_filter(
|
Chris@0
|
249 $this->container->getDefinitions(),
|
Chris@0
|
250 array($this->getProxyDumper(), 'isProxyCandidate')
|
Chris@0
|
251 );
|
Chris@0
|
252 $code = '';
|
Chris@0
|
253 $strip = '' === $this->docStar && method_exists('Symfony\Component\HttpKernel\Kernel', 'stripComments');
|
Chris@0
|
254
|
Chris@0
|
255 foreach ($definitions as $definition) {
|
Chris@0
|
256 $proxyCode = "\n".$this->getProxyDumper()->getProxyCode($definition);
|
Chris@0
|
257 if ($strip) {
|
Chris@0
|
258 $proxyCode = "<?php\n".$proxyCode;
|
Chris@0
|
259 $proxyCode = substr(Kernel::stripComments($proxyCode), 5);
|
Chris@0
|
260 }
|
Chris@0
|
261 $code .= $proxyCode;
|
Chris@0
|
262 }
|
Chris@0
|
263
|
Chris@0
|
264 return $code;
|
Chris@0
|
265 }
|
Chris@0
|
266
|
Chris@0
|
267 /**
|
Chris@0
|
268 * Generates the require_once statement for service includes.
|
Chris@0
|
269 *
|
Chris@0
|
270 * @param string $id The service id
|
Chris@0
|
271 * @param Definition $definition
|
Chris@0
|
272 *
|
Chris@0
|
273 * @return string
|
Chris@0
|
274 */
|
Chris@0
|
275 private function addServiceInclude($id, $definition)
|
Chris@0
|
276 {
|
Chris@0
|
277 $template = " require_once %s;\n";
|
Chris@0
|
278 $code = '';
|
Chris@0
|
279
|
Chris@0
|
280 if (null !== $file = $definition->getFile()) {
|
Chris@0
|
281 $code .= sprintf($template, $this->dumpValue($file));
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 foreach ($this->getInlinedDefinitions($definition) as $definition) {
|
Chris@0
|
285 if (null !== $file = $definition->getFile()) {
|
Chris@0
|
286 $code .= sprintf($template, $this->dumpValue($file));
|
Chris@0
|
287 }
|
Chris@0
|
288 }
|
Chris@0
|
289
|
Chris@0
|
290 if ('' !== $code) {
|
Chris@0
|
291 $code .= "\n";
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 return $code;
|
Chris@0
|
295 }
|
Chris@0
|
296
|
Chris@0
|
297 /**
|
Chris@0
|
298 * Generates the inline definition of a service.
|
Chris@0
|
299 *
|
Chris@0
|
300 * @param string $id
|
Chris@0
|
301 * @param Definition $definition
|
Chris@0
|
302 *
|
Chris@0
|
303 * @return string
|
Chris@0
|
304 *
|
Chris@0
|
305 * @throws RuntimeException When the factory definition is incomplete
|
Chris@0
|
306 * @throws ServiceCircularReferenceException When a circular reference is detected
|
Chris@0
|
307 */
|
Chris@0
|
308 private function addServiceInlinedDefinitions($id, $definition)
|
Chris@0
|
309 {
|
Chris@0
|
310 $code = '';
|
Chris@0
|
311 $variableMap = $this->definitionVariables;
|
Chris@0
|
312 $nbOccurrences = new \SplObjectStorage();
|
Chris@0
|
313 $processed = new \SplObjectStorage();
|
Chris@0
|
314 $inlinedDefinitions = $this->getInlinedDefinitions($definition);
|
Chris@0
|
315
|
Chris@0
|
316 foreach ($inlinedDefinitions as $definition) {
|
Chris@0
|
317 if (false === $nbOccurrences->contains($definition)) {
|
Chris@0
|
318 $nbOccurrences->offsetSet($definition, 1);
|
Chris@0
|
319 } else {
|
Chris@0
|
320 $i = $nbOccurrences->offsetGet($definition);
|
Chris@0
|
321 $nbOccurrences->offsetSet($definition, $i + 1);
|
Chris@0
|
322 }
|
Chris@0
|
323 }
|
Chris@0
|
324
|
Chris@0
|
325 foreach ($inlinedDefinitions as $sDefinition) {
|
Chris@0
|
326 if ($processed->contains($sDefinition)) {
|
Chris@0
|
327 continue;
|
Chris@0
|
328 }
|
Chris@0
|
329 $processed->offsetSet($sDefinition);
|
Chris@0
|
330
|
Chris@0
|
331 $class = $this->dumpValue($sDefinition->getClass());
|
Chris@0
|
332 if ($nbOccurrences->offsetGet($sDefinition) > 1 || $sDefinition->getMethodCalls() || $sDefinition->getProperties() || null !== $sDefinition->getConfigurator() || false !== strpos($class, '$')) {
|
Chris@0
|
333 $name = $this->getNextVariableName();
|
Chris@0
|
334 $variableMap->offsetSet($sDefinition, new Variable($name));
|
Chris@0
|
335
|
Chris@0
|
336 // a construct like:
|
Chris@0
|
337 // $a = new ServiceA(ServiceB $b); $b = new ServiceB(ServiceA $a);
|
Chris@0
|
338 // this is an indication for a wrong implementation, you can circumvent this problem
|
Chris@0
|
339 // by setting up your service structure like this:
|
Chris@0
|
340 // $b = new ServiceB();
|
Chris@0
|
341 // $a = new ServiceA(ServiceB $b);
|
Chris@0
|
342 // $b->setServiceA(ServiceA $a);
|
Chris@0
|
343 if ($this->hasReference($id, $sDefinition->getArguments())) {
|
Chris@0
|
344 throw new ServiceCircularReferenceException($id, array($id));
|
Chris@0
|
345 }
|
Chris@0
|
346
|
Chris@0
|
347 $code .= $this->addNewInstance($sDefinition, '$'.$name, ' = ', $id);
|
Chris@0
|
348
|
Chris@0
|
349 if (!$this->hasReference($id, $sDefinition->getMethodCalls(), true) && !$this->hasReference($id, $sDefinition->getProperties(), true)) {
|
Chris@0
|
350 $code .= $this->addServiceProperties(null, $sDefinition, $name);
|
Chris@0
|
351 $code .= $this->addServiceMethodCalls(null, $sDefinition, $name);
|
Chris@0
|
352 $code .= $this->addServiceConfigurator(null, $sDefinition, $name);
|
Chris@0
|
353 }
|
Chris@0
|
354
|
Chris@0
|
355 $code .= "\n";
|
Chris@0
|
356 }
|
Chris@0
|
357 }
|
Chris@0
|
358
|
Chris@0
|
359 return $code;
|
Chris@0
|
360 }
|
Chris@0
|
361
|
Chris@0
|
362 /**
|
Chris@0
|
363 * Adds the service return statement.
|
Chris@0
|
364 *
|
Chris@0
|
365 * @param string $id Service id
|
Chris@0
|
366 * @param Definition $definition
|
Chris@0
|
367 *
|
Chris@0
|
368 * @return string
|
Chris@0
|
369 */
|
Chris@0
|
370 private function addServiceReturn($id, $definition)
|
Chris@0
|
371 {
|
Chris@0
|
372 if ($this->isSimpleInstance($id, $definition)) {
|
Chris@0
|
373 return " }\n";
|
Chris@0
|
374 }
|
Chris@0
|
375
|
Chris@0
|
376 return "\n return \$instance;\n }\n";
|
Chris@0
|
377 }
|
Chris@0
|
378
|
Chris@0
|
379 /**
|
Chris@0
|
380 * Generates the service instance.
|
Chris@0
|
381 *
|
Chris@0
|
382 * @param string $id
|
Chris@0
|
383 * @param Definition $definition
|
Chris@0
|
384 *
|
Chris@0
|
385 * @return string
|
Chris@0
|
386 *
|
Chris@0
|
387 * @throws InvalidArgumentException
|
Chris@0
|
388 * @throws RuntimeException
|
Chris@0
|
389 */
|
Chris@0
|
390 private function addServiceInstance($id, Definition $definition)
|
Chris@0
|
391 {
|
Chris@0
|
392 $class = $definition->getClass();
|
Chris@0
|
393
|
Chris@0
|
394 if ('\\' === substr($class, 0, 1)) {
|
Chris@0
|
395 $class = substr($class, 1);
|
Chris@0
|
396 }
|
Chris@0
|
397
|
Chris@0
|
398 $class = $this->dumpValue($class);
|
Chris@0
|
399
|
Chris@0
|
400 if (0 === strpos($class, "'") && false === strpos($class, '$') && !preg_match('/^\'[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
|
401 throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
|
Chris@0
|
402 }
|
Chris@0
|
403
|
Chris@0
|
404 $simple = $this->isSimpleInstance($id, $definition);
|
Chris@0
|
405 $isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
|
Chris@0
|
406 $instantiation = '';
|
Chris@0
|
407
|
Chris@0
|
408 if (!$isProxyCandidate && $definition->isShared()) {
|
Chris@0
|
409 $instantiation = "\$this->services['$id'] = ".($simple ? '' : '$instance');
|
Chris@0
|
410 } elseif (!$simple) {
|
Chris@0
|
411 $instantiation = '$instance';
|
Chris@0
|
412 }
|
Chris@0
|
413
|
Chris@0
|
414 $return = '';
|
Chris@0
|
415 if ($simple) {
|
Chris@0
|
416 $return = 'return ';
|
Chris@0
|
417 } else {
|
Chris@0
|
418 $instantiation .= ' = ';
|
Chris@0
|
419 }
|
Chris@0
|
420
|
Chris@0
|
421 $code = $this->addNewInstance($definition, $return, $instantiation, $id);
|
Chris@0
|
422
|
Chris@0
|
423 if (!$simple) {
|
Chris@0
|
424 $code .= "\n";
|
Chris@0
|
425 }
|
Chris@0
|
426
|
Chris@0
|
427 return $code;
|
Chris@0
|
428 }
|
Chris@0
|
429
|
Chris@0
|
430 /**
|
Chris@0
|
431 * Checks if the definition is a simple instance.
|
Chris@0
|
432 *
|
Chris@0
|
433 * @param string $id
|
Chris@0
|
434 * @param Definition $definition
|
Chris@0
|
435 *
|
Chris@0
|
436 * @return bool
|
Chris@0
|
437 */
|
Chris@0
|
438 private function isSimpleInstance($id, Definition $definition)
|
Chris@0
|
439 {
|
Chris@0
|
440 foreach (array_merge(array($definition), $this->getInlinedDefinitions($definition)) as $sDefinition) {
|
Chris@0
|
441 if ($definition !== $sDefinition && !$this->hasReference($id, $sDefinition->getMethodCalls())) {
|
Chris@0
|
442 continue;
|
Chris@0
|
443 }
|
Chris@0
|
444
|
Chris@0
|
445 if ($sDefinition->getMethodCalls() || $sDefinition->getProperties() || $sDefinition->getConfigurator()) {
|
Chris@0
|
446 return false;
|
Chris@0
|
447 }
|
Chris@0
|
448 }
|
Chris@0
|
449
|
Chris@0
|
450 return true;
|
Chris@0
|
451 }
|
Chris@0
|
452
|
Chris@0
|
453 /**
|
Chris@0
|
454 * Adds method calls to a service definition.
|
Chris@0
|
455 *
|
Chris@0
|
456 * @param string $id
|
Chris@0
|
457 * @param Definition $definition
|
Chris@0
|
458 * @param string $variableName
|
Chris@0
|
459 *
|
Chris@0
|
460 * @return string
|
Chris@0
|
461 */
|
Chris@0
|
462 private function addServiceMethodCalls($id, Definition $definition, $variableName = 'instance')
|
Chris@0
|
463 {
|
Chris@0
|
464 $calls = '';
|
Chris@0
|
465 foreach ($definition->getMethodCalls() as $call) {
|
Chris@0
|
466 $arguments = array();
|
Chris@0
|
467 foreach ($call[1] as $value) {
|
Chris@0
|
468 $arguments[] = $this->dumpValue($value);
|
Chris@0
|
469 }
|
Chris@0
|
470
|
Chris@0
|
471 $calls .= $this->wrapServiceConditionals($call[1], sprintf(" \$%s->%s(%s);\n", $variableName, $call[0], implode(', ', $arguments)));
|
Chris@0
|
472 }
|
Chris@0
|
473
|
Chris@0
|
474 return $calls;
|
Chris@0
|
475 }
|
Chris@0
|
476
|
Chris@0
|
477 private function addServiceProperties($id, Definition $definition, $variableName = 'instance')
|
Chris@0
|
478 {
|
Chris@0
|
479 $code = '';
|
Chris@0
|
480 foreach ($definition->getProperties() as $name => $value) {
|
Chris@0
|
481 $code .= sprintf(" \$%s->%s = %s;\n", $variableName, $name, $this->dumpValue($value));
|
Chris@0
|
482 }
|
Chris@0
|
483
|
Chris@0
|
484 return $code;
|
Chris@0
|
485 }
|
Chris@0
|
486
|
Chris@0
|
487 /**
|
Chris@0
|
488 * Generates the inline definition setup.
|
Chris@0
|
489 *
|
Chris@0
|
490 * @param string $id
|
Chris@0
|
491 * @param Definition $definition
|
Chris@0
|
492 *
|
Chris@0
|
493 * @return string
|
Chris@0
|
494 *
|
Chris@0
|
495 * @throws ServiceCircularReferenceException when the container contains a circular reference
|
Chris@0
|
496 */
|
Chris@0
|
497 private function addServiceInlinedDefinitionsSetup($id, Definition $definition)
|
Chris@0
|
498 {
|
Chris@0
|
499 $this->referenceVariables[$id] = new Variable('instance');
|
Chris@0
|
500
|
Chris@0
|
501 $code = '';
|
Chris@0
|
502 $processed = new \SplObjectStorage();
|
Chris@0
|
503 foreach ($this->getInlinedDefinitions($definition) as $iDefinition) {
|
Chris@0
|
504 if ($processed->contains($iDefinition)) {
|
Chris@0
|
505 continue;
|
Chris@0
|
506 }
|
Chris@0
|
507 $processed->offsetSet($iDefinition);
|
Chris@0
|
508
|
Chris@0
|
509 if (!$this->hasReference($id, $iDefinition->getMethodCalls(), true) && !$this->hasReference($id, $iDefinition->getProperties(), true)) {
|
Chris@0
|
510 continue;
|
Chris@0
|
511 }
|
Chris@0
|
512
|
Chris@0
|
513 // if the instance is simple, the return statement has already been generated
|
Chris@0
|
514 // so, the only possible way to get there is because of a circular reference
|
Chris@0
|
515 if ($this->isSimpleInstance($id, $definition)) {
|
Chris@0
|
516 throw new ServiceCircularReferenceException($id, array($id));
|
Chris@0
|
517 }
|
Chris@0
|
518
|
Chris@0
|
519 $name = (string) $this->definitionVariables->offsetGet($iDefinition);
|
Chris@0
|
520 $code .= $this->addServiceProperties(null, $iDefinition, $name);
|
Chris@0
|
521 $code .= $this->addServiceMethodCalls(null, $iDefinition, $name);
|
Chris@0
|
522 $code .= $this->addServiceConfigurator(null, $iDefinition, $name);
|
Chris@0
|
523 }
|
Chris@0
|
524
|
Chris@0
|
525 if ('' !== $code) {
|
Chris@0
|
526 $code .= "\n";
|
Chris@0
|
527 }
|
Chris@0
|
528
|
Chris@0
|
529 return $code;
|
Chris@0
|
530 }
|
Chris@0
|
531
|
Chris@0
|
532 /**
|
Chris@0
|
533 * Adds configurator definition.
|
Chris@0
|
534 *
|
Chris@0
|
535 * @param string $id
|
Chris@0
|
536 * @param Definition $definition
|
Chris@0
|
537 * @param string $variableName
|
Chris@0
|
538 *
|
Chris@0
|
539 * @return string
|
Chris@0
|
540 */
|
Chris@0
|
541 private function addServiceConfigurator($id, Definition $definition, $variableName = 'instance')
|
Chris@0
|
542 {
|
Chris@0
|
543 if (!$callable = $definition->getConfigurator()) {
|
Chris@0
|
544 return '';
|
Chris@0
|
545 }
|
Chris@0
|
546
|
Chris@0
|
547 if (is_array($callable)) {
|
Chris@0
|
548 if ($callable[0] instanceof Reference
|
Chris@0
|
549 || ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))) {
|
Chris@0
|
550 return sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
|
Chris@0
|
551 }
|
Chris@0
|
552
|
Chris@0
|
553 $class = $this->dumpValue($callable[0]);
|
Chris@0
|
554 // If the class is a string we can optimize call_user_func away
|
Chris@0
|
555 if (0 === strpos($class, "'") && false === strpos($class, '$')) {
|
Chris@0
|
556 return sprintf(" %s::%s(\$%s);\n", $this->dumpLiteralClass($class), $callable[1], $variableName);
|
Chris@0
|
557 }
|
Chris@0
|
558
|
Chris@0
|
559 if (0 === strpos($class, 'new ')) {
|
Chris@0
|
560 return sprintf(" (%s)->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
|
Chris@0
|
561 }
|
Chris@0
|
562
|
Chris@0
|
563 return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
|
Chris@0
|
564 }
|
Chris@0
|
565
|
Chris@0
|
566 return sprintf(" %s(\$%s);\n", $callable, $variableName);
|
Chris@0
|
567 }
|
Chris@0
|
568
|
Chris@0
|
569 /**
|
Chris@0
|
570 * Adds a service.
|
Chris@0
|
571 *
|
Chris@0
|
572 * @param string $id
|
Chris@0
|
573 * @param Definition $definition
|
Chris@0
|
574 *
|
Chris@0
|
575 * @return string
|
Chris@0
|
576 */
|
Chris@0
|
577 private function addService($id, Definition $definition)
|
Chris@0
|
578 {
|
Chris@0
|
579 $this->definitionVariables = new \SplObjectStorage();
|
Chris@0
|
580 $this->referenceVariables = array();
|
Chris@0
|
581 $this->variableCount = 0;
|
Chris@0
|
582
|
Chris@0
|
583 $return = array();
|
Chris@0
|
584
|
Chris@0
|
585 if ($definition->isSynthetic()) {
|
Chris@0
|
586 $return[] = '@throws RuntimeException always since this service is expected to be injected dynamically';
|
Chris@0
|
587 } elseif ($class = $definition->getClass()) {
|
Chris@0
|
588 $class = $this->container->resolveEnvPlaceholders($class);
|
Chris@0
|
589 $return[] = sprintf('@return %s A %s instance', 0 === strpos($class, '%') ? 'object' : '\\'.ltrim($class, '\\'), ltrim($class, '\\'));
|
Chris@0
|
590 } elseif ($definition->getFactory()) {
|
Chris@0
|
591 $factory = $definition->getFactory();
|
Chris@0
|
592 if (is_string($factory)) {
|
Chris@0
|
593 $return[] = sprintf('@return object An instance returned by %s()', $factory);
|
Chris@0
|
594 } elseif (is_array($factory) && (is_string($factory[0]) || $factory[0] instanceof Definition || $factory[0] instanceof Reference)) {
|
Chris@0
|
595 if (is_string($factory[0]) || $factory[0] instanceof Reference) {
|
Chris@0
|
596 $return[] = sprintf('@return object An instance returned by %s::%s()', (string) $factory[0], $factory[1]);
|
Chris@0
|
597 } elseif ($factory[0] instanceof Definition) {
|
Chris@0
|
598 $return[] = sprintf('@return object An instance returned by %s::%s()', $factory[0]->getClass(), $factory[1]);
|
Chris@0
|
599 }
|
Chris@0
|
600 }
|
Chris@0
|
601 }
|
Chris@0
|
602
|
Chris@0
|
603 if ($definition->isDeprecated()) {
|
Chris@0
|
604 if ($return && 0 === strpos($return[count($return) - 1], '@return')) {
|
Chris@0
|
605 $return[] = '';
|
Chris@0
|
606 }
|
Chris@0
|
607
|
Chris@0
|
608 $return[] = sprintf('@deprecated %s', $definition->getDeprecationMessage($id));
|
Chris@0
|
609 }
|
Chris@0
|
610
|
Chris@0
|
611 $return = str_replace("\n * \n", "\n *\n", implode("\n * ", $return));
|
Chris@0
|
612 $return = $this->container->resolveEnvPlaceholders($return);
|
Chris@0
|
613
|
Chris@0
|
614 $doc = '';
|
Chris@0
|
615 if ($definition->isShared()) {
|
Chris@0
|
616 $doc .= <<<'EOF'
|
Chris@0
|
617
|
Chris@0
|
618 *
|
Chris@0
|
619 * This service is shared.
|
Chris@0
|
620 * This method always returns the same instance of the service.
|
Chris@0
|
621 EOF;
|
Chris@0
|
622 }
|
Chris@0
|
623
|
Chris@0
|
624 if (!$definition->isPublic()) {
|
Chris@0
|
625 $doc .= <<<'EOF'
|
Chris@0
|
626
|
Chris@0
|
627 *
|
Chris@0
|
628 * This service is private.
|
Chris@0
|
629 * If you want to be able to request this service from the container directly,
|
Chris@0
|
630 * make it public, otherwise you might end up with broken code.
|
Chris@0
|
631 EOF;
|
Chris@0
|
632 }
|
Chris@0
|
633
|
Chris@0
|
634 if ($definition->isAutowired()) {
|
Chris@0
|
635 $doc .= <<<EOF
|
Chris@0
|
636
|
Chris@0
|
637 *
|
Chris@0
|
638 * This service is autowired.
|
Chris@0
|
639 EOF;
|
Chris@0
|
640 }
|
Chris@0
|
641
|
Chris@0
|
642 if ($definition->isLazy()) {
|
Chris@0
|
643 $lazyInitialization = '$lazyLoad = true';
|
Chris@0
|
644 $lazyInitializationDoc = "\n * @param bool \$lazyLoad whether to try lazy-loading the service with a proxy\n *";
|
Chris@0
|
645 } else {
|
Chris@0
|
646 $lazyInitialization = '';
|
Chris@0
|
647 $lazyInitializationDoc = '';
|
Chris@0
|
648 }
|
Chris@0
|
649
|
Chris@0
|
650 // with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
|
Chris@0
|
651 $isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
|
Chris@0
|
652 $visibility = $isProxyCandidate ? 'public' : 'protected';
|
Chris@0
|
653 $methodName = $this->generateMethodName($id);
|
Chris@0
|
654 $code = <<<EOF
|
Chris@0
|
655
|
Chris@0
|
656 /*{$this->docStar}
|
Chris@0
|
657 * Gets the '$id' service.$doc
|
Chris@0
|
658 *$lazyInitializationDoc
|
Chris@0
|
659 * $return
|
Chris@0
|
660 */
|
Chris@0
|
661 {$visibility} function {$methodName}($lazyInitialization)
|
Chris@0
|
662 {
|
Chris@0
|
663
|
Chris@0
|
664 EOF;
|
Chris@0
|
665
|
Chris@0
|
666 $code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id, $methodName) : '';
|
Chris@0
|
667
|
Chris@0
|
668 if ($definition->isSynthetic()) {
|
Chris@0
|
669 $code .= sprintf(" throw new RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n }\n", $id);
|
Chris@0
|
670 } else {
|
Chris@0
|
671 if ($definition->isDeprecated()) {
|
Chris@0
|
672 $code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id)));
|
Chris@0
|
673 }
|
Chris@0
|
674
|
Chris@0
|
675 $code .=
|
Chris@0
|
676 $this->addServiceInclude($id, $definition).
|
Chris@0
|
677 $this->addServiceLocalTempVariables($id, $definition).
|
Chris@0
|
678 $this->addServiceInlinedDefinitions($id, $definition).
|
Chris@0
|
679 $this->addServiceInstance($id, $definition).
|
Chris@0
|
680 $this->addServiceInlinedDefinitionsSetup($id, $definition).
|
Chris@0
|
681 $this->addServiceProperties($id, $definition).
|
Chris@0
|
682 $this->addServiceMethodCalls($id, $definition).
|
Chris@0
|
683 $this->addServiceConfigurator($id, $definition).
|
Chris@0
|
684 $this->addServiceReturn($id, $definition)
|
Chris@0
|
685 ;
|
Chris@0
|
686 }
|
Chris@0
|
687
|
Chris@0
|
688 $this->definitionVariables = null;
|
Chris@0
|
689 $this->referenceVariables = null;
|
Chris@0
|
690
|
Chris@0
|
691 return $code;
|
Chris@0
|
692 }
|
Chris@0
|
693
|
Chris@0
|
694 /**
|
Chris@0
|
695 * Adds multiple services.
|
Chris@0
|
696 *
|
Chris@0
|
697 * @return string
|
Chris@0
|
698 */
|
Chris@0
|
699 private function addServices()
|
Chris@0
|
700 {
|
Chris@0
|
701 $publicServices = $privateServices = '';
|
Chris@0
|
702 $definitions = $this->container->getDefinitions();
|
Chris@0
|
703 ksort($definitions);
|
Chris@0
|
704 foreach ($definitions as $id => $definition) {
|
Chris@0
|
705 if ($definition->isPublic()) {
|
Chris@0
|
706 $publicServices .= $this->addService($id, $definition);
|
Chris@0
|
707 } else {
|
Chris@0
|
708 $privateServices .= $this->addService($id, $definition);
|
Chris@0
|
709 }
|
Chris@0
|
710 }
|
Chris@0
|
711
|
Chris@0
|
712 return $publicServices.$privateServices;
|
Chris@0
|
713 }
|
Chris@0
|
714
|
Chris@0
|
715 private function addNewInstance(Definition $definition, $return, $instantiation, $id)
|
Chris@0
|
716 {
|
Chris@0
|
717 $class = $this->dumpValue($definition->getClass());
|
Chris@0
|
718
|
Chris@0
|
719 $arguments = array();
|
Chris@0
|
720 foreach ($definition->getArguments() as $value) {
|
Chris@0
|
721 $arguments[] = $this->dumpValue($value);
|
Chris@0
|
722 }
|
Chris@0
|
723
|
Chris@0
|
724 if (null !== $definition->getFactory()) {
|
Chris@0
|
725 $callable = $definition->getFactory();
|
Chris@0
|
726 if (is_array($callable)) {
|
Chris@0
|
727 if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $callable[1])) {
|
Chris@0
|
728 throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s)', $callable[1] ?: 'n/a'));
|
Chris@0
|
729 }
|
Chris@0
|
730
|
Chris@0
|
731 if ($callable[0] instanceof Reference
|
Chris@0
|
732 || ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))) {
|
Chris@0
|
733 return sprintf(" $return{$instantiation}%s->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
|
Chris@0
|
734 }
|
Chris@0
|
735
|
Chris@0
|
736 $class = $this->dumpValue($callable[0]);
|
Chris@0
|
737 // If the class is a string we can optimize call_user_func away
|
Chris@0
|
738 if (0 === strpos($class, "'") && false === strpos($class, '$')) {
|
Chris@0
|
739 if ("''" === $class) {
|
Chris@0
|
740 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
|
741 }
|
Chris@0
|
742
|
Chris@0
|
743 return sprintf(" $return{$instantiation}%s::%s(%s);\n", $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '');
|
Chris@0
|
744 }
|
Chris@0
|
745
|
Chris@0
|
746 if (0 === strpos($class, 'new ')) {
|
Chris@0
|
747 return sprintf(" $return{$instantiation}(%s)->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
|
Chris@0
|
748 }
|
Chris@0
|
749
|
Chris@0
|
750 return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? ', '.implode(', ', $arguments) : '');
|
Chris@0
|
751 }
|
Chris@0
|
752
|
Chris@0
|
753 return sprintf(" $return{$instantiation}%s(%s);\n", $this->dumpLiteralClass($this->dumpValue($callable)), $arguments ? implode(', ', $arguments) : '');
|
Chris@0
|
754 }
|
Chris@0
|
755
|
Chris@0
|
756 if (false !== strpos($class, '$')) {
|
Chris@0
|
757 return sprintf(" \$class = %s;\n\n $return{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments));
|
Chris@0
|
758 }
|
Chris@0
|
759
|
Chris@0
|
760 return sprintf(" $return{$instantiation}new %s(%s);\n", $this->dumpLiteralClass($class), implode(', ', $arguments));
|
Chris@0
|
761 }
|
Chris@0
|
762
|
Chris@0
|
763 /**
|
Chris@0
|
764 * Adds the class headers.
|
Chris@0
|
765 *
|
Chris@0
|
766 * @param string $class Class name
|
Chris@0
|
767 * @param string $baseClass The name of the base class
|
Chris@0
|
768 * @param string $namespace The class namespace
|
Chris@0
|
769 *
|
Chris@0
|
770 * @return string
|
Chris@0
|
771 */
|
Chris@0
|
772 private function startClass($class, $baseClass, $namespace)
|
Chris@0
|
773 {
|
Chris@0
|
774 $bagClass = $this->container->isFrozen() ? 'use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;' : 'use Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag;';
|
Chris@0
|
775 $namespaceLine = $namespace ? "namespace $namespace;\n" : '';
|
Chris@0
|
776
|
Chris@0
|
777 return <<<EOF
|
Chris@0
|
778 <?php
|
Chris@0
|
779 $namespaceLine
|
Chris@0
|
780 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
781 use Symfony\Component\DependencyInjection\Container;
|
Chris@0
|
782 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
783 use Symfony\Component\DependencyInjection\Exception\LogicException;
|
Chris@0
|
784 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
785 $bagClass
|
Chris@0
|
786
|
Chris@0
|
787 /*{$this->docStar}
|
Chris@0
|
788 * $class.
|
Chris@0
|
789 *
|
Chris@0
|
790 * This class has been auto-generated
|
Chris@0
|
791 * by the Symfony Dependency Injection Component.
|
Chris@0
|
792 */
|
Chris@0
|
793 class $class extends $baseClass
|
Chris@0
|
794 {
|
Chris@0
|
795 private \$parameters;
|
Chris@0
|
796 private \$targetDirs = array();
|
Chris@0
|
797
|
Chris@0
|
798 EOF;
|
Chris@0
|
799 }
|
Chris@0
|
800
|
Chris@0
|
801 /**
|
Chris@0
|
802 * Adds the constructor.
|
Chris@0
|
803 *
|
Chris@0
|
804 * @return string
|
Chris@0
|
805 */
|
Chris@0
|
806 private function addConstructor()
|
Chris@0
|
807 {
|
Chris@0
|
808 $targetDirs = $this->exportTargetDirs();
|
Chris@0
|
809 $arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null;
|
Chris@0
|
810
|
Chris@0
|
811 $code = <<<EOF
|
Chris@0
|
812
|
Chris@0
|
813 /*{$this->docStar}
|
Chris@0
|
814 * Constructor.
|
Chris@0
|
815 */
|
Chris@0
|
816 public function __construct()
|
Chris@0
|
817 {{$targetDirs}
|
Chris@0
|
818 parent::__construct($arguments);
|
Chris@0
|
819
|
Chris@0
|
820 EOF;
|
Chris@0
|
821
|
Chris@0
|
822 $code .= $this->addMethodMap();
|
Chris@0
|
823 $code .= $this->addPrivateServices();
|
Chris@0
|
824 $code .= $this->addAliases();
|
Chris@0
|
825
|
Chris@0
|
826 $code .= <<<'EOF'
|
Chris@0
|
827 }
|
Chris@0
|
828
|
Chris@0
|
829 EOF;
|
Chris@0
|
830
|
Chris@0
|
831 return $code;
|
Chris@0
|
832 }
|
Chris@0
|
833
|
Chris@0
|
834 /**
|
Chris@0
|
835 * Adds the constructor for a frozen container.
|
Chris@0
|
836 *
|
Chris@0
|
837 * @return string
|
Chris@0
|
838 */
|
Chris@0
|
839 private function addFrozenConstructor()
|
Chris@0
|
840 {
|
Chris@0
|
841 $targetDirs = $this->exportTargetDirs();
|
Chris@0
|
842
|
Chris@0
|
843 $code = <<<EOF
|
Chris@0
|
844
|
Chris@0
|
845 /*{$this->docStar}
|
Chris@0
|
846 * Constructor.
|
Chris@0
|
847 */
|
Chris@0
|
848 public function __construct()
|
Chris@0
|
849 {{$targetDirs}
|
Chris@0
|
850 EOF;
|
Chris@0
|
851
|
Chris@0
|
852 if ($this->container->getParameterBag()->all()) {
|
Chris@0
|
853 $code .= "\n \$this->parameters = \$this->getDefaultParameters();\n";
|
Chris@0
|
854 }
|
Chris@0
|
855
|
Chris@0
|
856 $code .= "\n \$this->services = array();\n";
|
Chris@0
|
857 $code .= $this->addMethodMap();
|
Chris@0
|
858 $code .= $this->addPrivateServices();
|
Chris@0
|
859 $code .= $this->addAliases();
|
Chris@0
|
860
|
Chris@0
|
861 $code .= <<<'EOF'
|
Chris@0
|
862 }
|
Chris@0
|
863
|
Chris@0
|
864 EOF;
|
Chris@0
|
865
|
Chris@0
|
866 return $code;
|
Chris@0
|
867 }
|
Chris@0
|
868
|
Chris@0
|
869 /**
|
Chris@0
|
870 * Adds the constructor for a frozen container.
|
Chris@0
|
871 *
|
Chris@0
|
872 * @return string
|
Chris@0
|
873 */
|
Chris@0
|
874 private function addFrozenCompile()
|
Chris@0
|
875 {
|
Chris@0
|
876 return <<<EOF
|
Chris@0
|
877
|
Chris@0
|
878 /*{$this->docStar}
|
Chris@0
|
879 * {@inheritdoc}
|
Chris@0
|
880 */
|
Chris@0
|
881 public function compile()
|
Chris@0
|
882 {
|
Chris@0
|
883 throw new LogicException('You cannot compile a dumped frozen container.');
|
Chris@0
|
884 }
|
Chris@0
|
885
|
Chris@0
|
886 EOF;
|
Chris@0
|
887 }
|
Chris@0
|
888
|
Chris@0
|
889 /**
|
Chris@0
|
890 * Adds the isFrozen method for a frozen container.
|
Chris@0
|
891 *
|
Chris@0
|
892 * @return string
|
Chris@0
|
893 */
|
Chris@0
|
894 private function addIsFrozenMethod()
|
Chris@0
|
895 {
|
Chris@0
|
896 return <<<EOF
|
Chris@0
|
897
|
Chris@0
|
898 /*{$this->docStar}
|
Chris@0
|
899 * {@inheritdoc}
|
Chris@0
|
900 */
|
Chris@0
|
901 public function isFrozen()
|
Chris@0
|
902 {
|
Chris@0
|
903 return true;
|
Chris@0
|
904 }
|
Chris@0
|
905
|
Chris@0
|
906 EOF;
|
Chris@0
|
907 }
|
Chris@0
|
908
|
Chris@0
|
909 /**
|
Chris@0
|
910 * Adds the methodMap property definition.
|
Chris@0
|
911 *
|
Chris@0
|
912 * @return string
|
Chris@0
|
913 */
|
Chris@0
|
914 private function addMethodMap()
|
Chris@0
|
915 {
|
Chris@0
|
916 if (!$definitions = $this->container->getDefinitions()) {
|
Chris@0
|
917 return '';
|
Chris@0
|
918 }
|
Chris@0
|
919
|
Chris@0
|
920 $code = " \$this->methodMap = array(\n";
|
Chris@0
|
921 ksort($definitions);
|
Chris@0
|
922 foreach ($definitions as $id => $definition) {
|
Chris@0
|
923 $code .= ' '.$this->export($id).' => '.$this->export($this->generateMethodName($id)).",\n";
|
Chris@0
|
924 }
|
Chris@0
|
925
|
Chris@0
|
926 return $code." );\n";
|
Chris@0
|
927 }
|
Chris@0
|
928
|
Chris@0
|
929 /**
|
Chris@0
|
930 * Adds the privates property definition.
|
Chris@0
|
931 *
|
Chris@0
|
932 * @return string
|
Chris@0
|
933 */
|
Chris@0
|
934 private function addPrivateServices()
|
Chris@0
|
935 {
|
Chris@0
|
936 if (!$definitions = $this->container->getDefinitions()) {
|
Chris@0
|
937 return '';
|
Chris@0
|
938 }
|
Chris@0
|
939
|
Chris@0
|
940 $code = '';
|
Chris@0
|
941 ksort($definitions);
|
Chris@0
|
942 foreach ($definitions as $id => $definition) {
|
Chris@0
|
943 if (!$definition->isPublic()) {
|
Chris@0
|
944 $code .= ' '.$this->export($id)." => true,\n";
|
Chris@0
|
945 }
|
Chris@0
|
946 }
|
Chris@0
|
947
|
Chris@0
|
948 if (empty($code)) {
|
Chris@0
|
949 return '';
|
Chris@0
|
950 }
|
Chris@0
|
951
|
Chris@0
|
952 $out = " \$this->privates = array(\n";
|
Chris@0
|
953 $out .= $code;
|
Chris@0
|
954 $out .= " );\n";
|
Chris@0
|
955
|
Chris@0
|
956 return $out;
|
Chris@0
|
957 }
|
Chris@0
|
958
|
Chris@0
|
959 /**
|
Chris@0
|
960 * Adds the aliases property definition.
|
Chris@0
|
961 *
|
Chris@0
|
962 * @return string
|
Chris@0
|
963 */
|
Chris@0
|
964 private function addAliases()
|
Chris@0
|
965 {
|
Chris@0
|
966 if (!$aliases = $this->container->getAliases()) {
|
Chris@0
|
967 return $this->container->isFrozen() ? "\n \$this->aliases = array();\n" : '';
|
Chris@0
|
968 }
|
Chris@0
|
969
|
Chris@0
|
970 $code = " \$this->aliases = array(\n";
|
Chris@0
|
971 ksort($aliases);
|
Chris@0
|
972 foreach ($aliases as $alias => $id) {
|
Chris@0
|
973 $id = (string) $id;
|
Chris@0
|
974 while (isset($aliases[$id])) {
|
Chris@0
|
975 $id = (string) $aliases[$id];
|
Chris@0
|
976 }
|
Chris@0
|
977 $code .= ' '.$this->export($alias).' => '.$this->export($id).",\n";
|
Chris@0
|
978 }
|
Chris@0
|
979
|
Chris@0
|
980 return $code." );\n";
|
Chris@0
|
981 }
|
Chris@0
|
982
|
Chris@0
|
983 /**
|
Chris@0
|
984 * Adds default parameters method.
|
Chris@0
|
985 *
|
Chris@0
|
986 * @return string
|
Chris@0
|
987 */
|
Chris@0
|
988 private function addDefaultParametersMethod()
|
Chris@0
|
989 {
|
Chris@0
|
990 if (!$this->container->getParameterBag()->all()) {
|
Chris@0
|
991 return '';
|
Chris@0
|
992 }
|
Chris@0
|
993
|
Chris@0
|
994 $php = array();
|
Chris@0
|
995 $dynamicPhp = array();
|
Chris@0
|
996
|
Chris@0
|
997 foreach ($this->container->getParameterBag()->all() as $key => $value) {
|
Chris@0
|
998 if ($key !== $resolvedKey = $this->container->resolveEnvPlaceholders($key)) {
|
Chris@0
|
999 throw new InvalidArgumentException(sprintf('Parameter name cannot use env parameters: %s.', $resolvedKey));
|
Chris@0
|
1000 }
|
Chris@0
|
1001 $export = $this->exportParameters(array($value));
|
Chris@0
|
1002 $export = explode('0 => ', substr(rtrim($export, " )\n"), 7, -1), 2);
|
Chris@0
|
1003
|
Chris@0
|
1004 if (preg_match("/\\\$this->(?:getEnv\('\w++'\)|targetDirs\[\d++\])/", $export[1])) {
|
Chris@0
|
1005 $dynamicPhp[$key] = sprintf('%scase %s: $value = %s; break;', $export[0], $this->export($key), $export[1]);
|
Chris@0
|
1006 } else {
|
Chris@0
|
1007 $php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
|
Chris@0
|
1008 }
|
Chris@0
|
1009 }
|
Chris@0
|
1010 $parameters = sprintf("array(\n%s\n%s)", implode("\n", $php), str_repeat(' ', 8));
|
Chris@0
|
1011
|
Chris@0
|
1012 $code = '';
|
Chris@0
|
1013 if ($this->container->isFrozen()) {
|
Chris@0
|
1014 $code .= <<<'EOF'
|
Chris@0
|
1015
|
Chris@0
|
1016 /**
|
Chris@0
|
1017 * {@inheritdoc}
|
Chris@0
|
1018 */
|
Chris@0
|
1019 public function getParameter($name)
|
Chris@0
|
1020 {
|
Chris@0
|
1021 $name = strtolower($name);
|
Chris@0
|
1022
|
Chris@0
|
1023 if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters) || isset($this->loadedDynamicParameters[$name]))) {
|
Chris@0
|
1024 throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
|
Chris@0
|
1025 }
|
Chris@0
|
1026 if (isset($this->loadedDynamicParameters[$name])) {
|
Chris@0
|
1027 return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
|
Chris@0
|
1028 }
|
Chris@0
|
1029
|
Chris@0
|
1030 return $this->parameters[$name];
|
Chris@0
|
1031 }
|
Chris@0
|
1032
|
Chris@0
|
1033 /**
|
Chris@0
|
1034 * {@inheritdoc}
|
Chris@0
|
1035 */
|
Chris@0
|
1036 public function hasParameter($name)
|
Chris@0
|
1037 {
|
Chris@0
|
1038 $name = strtolower($name);
|
Chris@0
|
1039
|
Chris@0
|
1040 return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters) || isset($this->loadedDynamicParameters[$name]);
|
Chris@0
|
1041 }
|
Chris@0
|
1042
|
Chris@0
|
1043 /**
|
Chris@0
|
1044 * {@inheritdoc}
|
Chris@0
|
1045 */
|
Chris@0
|
1046 public function setParameter($name, $value)
|
Chris@0
|
1047 {
|
Chris@0
|
1048 throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
|
Chris@0
|
1049 }
|
Chris@0
|
1050
|
Chris@0
|
1051 /**
|
Chris@0
|
1052 * {@inheritdoc}
|
Chris@0
|
1053 */
|
Chris@0
|
1054 public function getParameterBag()
|
Chris@0
|
1055 {
|
Chris@0
|
1056 if (null === $this->parameterBag) {
|
Chris@0
|
1057 $parameters = $this->parameters;
|
Chris@0
|
1058 foreach ($this->loadedDynamicParameters as $name => $loaded) {
|
Chris@0
|
1059 $parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
|
Chris@0
|
1060 }
|
Chris@0
|
1061 $this->parameterBag = new FrozenParameterBag($parameters);
|
Chris@0
|
1062 }
|
Chris@0
|
1063
|
Chris@0
|
1064 return $this->parameterBag;
|
Chris@0
|
1065 }
|
Chris@0
|
1066
|
Chris@0
|
1067 EOF;
|
Chris@0
|
1068 if ('' === $this->docStar) {
|
Chris@0
|
1069 $code = str_replace('/**', '/*', $code);
|
Chris@0
|
1070 }
|
Chris@0
|
1071
|
Chris@0
|
1072 if ($dynamicPhp) {
|
Chris@0
|
1073 $loadedDynamicParameters = $this->exportParameters(array_combine(array_keys($dynamicPhp), array_fill(0, count($dynamicPhp), false)), '', 8);
|
Chris@0
|
1074 $getDynamicParameter = <<<'EOF'
|
Chris@0
|
1075 switch ($name) {
|
Chris@0
|
1076 %s
|
Chris@0
|
1077 default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%%s" must be defined.', $name));
|
Chris@0
|
1078 }
|
Chris@0
|
1079 $this->loadedDynamicParameters[$name] = true;
|
Chris@0
|
1080
|
Chris@0
|
1081 return $this->dynamicParameters[$name] = $value;
|
Chris@0
|
1082 EOF;
|
Chris@0
|
1083 $getDynamicParameter = sprintf($getDynamicParameter, implode("\n", $dynamicPhp));
|
Chris@0
|
1084 } else {
|
Chris@0
|
1085 $loadedDynamicParameters = 'array()';
|
Chris@0
|
1086 $getDynamicParameter = str_repeat(' ', 8).'throw new InvalidArgumentException(sprintf(\'The dynamic parameter "%s" must be defined.\', $name));';
|
Chris@0
|
1087 }
|
Chris@0
|
1088
|
Chris@0
|
1089 $code .= <<<EOF
|
Chris@0
|
1090
|
Chris@0
|
1091 private \$loadedDynamicParameters = {$loadedDynamicParameters};
|
Chris@0
|
1092 private \$dynamicParameters = array();
|
Chris@0
|
1093
|
Chris@0
|
1094 /*{$this->docStar}
|
Chris@0
|
1095 * Computes a dynamic parameter.
|
Chris@0
|
1096 *
|
Chris@0
|
1097 * @param string The name of the dynamic parameter to load
|
Chris@0
|
1098 *
|
Chris@0
|
1099 * @return mixed The value of the dynamic parameter
|
Chris@0
|
1100 *
|
Chris@0
|
1101 * @throws InvalidArgumentException When the dynamic parameter does not exist
|
Chris@0
|
1102 */
|
Chris@0
|
1103 private function getDynamicParameter(\$name)
|
Chris@0
|
1104 {
|
Chris@0
|
1105 {$getDynamicParameter}
|
Chris@0
|
1106 }
|
Chris@0
|
1107
|
Chris@0
|
1108 EOF;
|
Chris@0
|
1109 } elseif ($dynamicPhp) {
|
Chris@0
|
1110 throw new RuntimeException('You cannot dump a not-frozen container with dynamic parameters.');
|
Chris@0
|
1111 }
|
Chris@0
|
1112
|
Chris@0
|
1113 $code .= <<<EOF
|
Chris@0
|
1114
|
Chris@0
|
1115 /*{$this->docStar}
|
Chris@0
|
1116 * Gets the default parameters.
|
Chris@0
|
1117 *
|
Chris@0
|
1118 * @return array An array of the default parameters
|
Chris@0
|
1119 */
|
Chris@0
|
1120 protected function getDefaultParameters()
|
Chris@0
|
1121 {
|
Chris@0
|
1122 return $parameters;
|
Chris@0
|
1123 }
|
Chris@0
|
1124
|
Chris@0
|
1125 EOF;
|
Chris@0
|
1126
|
Chris@0
|
1127 return $code;
|
Chris@0
|
1128 }
|
Chris@0
|
1129
|
Chris@0
|
1130 /**
|
Chris@0
|
1131 * Exports parameters.
|
Chris@0
|
1132 *
|
Chris@0
|
1133 * @param array $parameters
|
Chris@0
|
1134 * @param string $path
|
Chris@0
|
1135 * @param int $indent
|
Chris@0
|
1136 *
|
Chris@0
|
1137 * @return string
|
Chris@0
|
1138 *
|
Chris@0
|
1139 * @throws InvalidArgumentException
|
Chris@0
|
1140 */
|
Chris@0
|
1141 private function exportParameters(array $parameters, $path = '', $indent = 12)
|
Chris@0
|
1142 {
|
Chris@0
|
1143 $php = array();
|
Chris@0
|
1144 foreach ($parameters as $key => $value) {
|
Chris@0
|
1145 if (is_array($value)) {
|
Chris@0
|
1146 $value = $this->exportParameters($value, $path.'/'.$key, $indent + 4);
|
Chris@0
|
1147 } elseif ($value instanceof Variable) {
|
Chris@0
|
1148 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
|
1149 } elseif ($value instanceof Definition) {
|
Chris@0
|
1150 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
|
1151 } elseif ($value instanceof Reference) {
|
Chris@0
|
1152 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
|
1153 } elseif ($value instanceof Expression) {
|
Chris@0
|
1154 throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain expressions. Expression "%s" found in "%s".', $value, $path.'/'.$key));
|
Chris@0
|
1155 } else {
|
Chris@0
|
1156 $value = $this->export($value);
|
Chris@0
|
1157 }
|
Chris@0
|
1158
|
Chris@0
|
1159 $php[] = sprintf('%s%s => %s,', str_repeat(' ', $indent), $this->export($key), $value);
|
Chris@0
|
1160 }
|
Chris@0
|
1161
|
Chris@0
|
1162 return sprintf("array(\n%s\n%s)", implode("\n", $php), str_repeat(' ', $indent - 4));
|
Chris@0
|
1163 }
|
Chris@0
|
1164
|
Chris@0
|
1165 /**
|
Chris@0
|
1166 * Ends the class definition.
|
Chris@0
|
1167 *
|
Chris@0
|
1168 * @return string
|
Chris@0
|
1169 */
|
Chris@0
|
1170 private function endClass()
|
Chris@0
|
1171 {
|
Chris@0
|
1172 return <<<'EOF'
|
Chris@0
|
1173 }
|
Chris@0
|
1174
|
Chris@0
|
1175 EOF;
|
Chris@0
|
1176 }
|
Chris@0
|
1177
|
Chris@0
|
1178 /**
|
Chris@0
|
1179 * Wraps the service conditionals.
|
Chris@0
|
1180 *
|
Chris@0
|
1181 * @param string $value
|
Chris@0
|
1182 * @param string $code
|
Chris@0
|
1183 *
|
Chris@0
|
1184 * @return string
|
Chris@0
|
1185 */
|
Chris@0
|
1186 private function wrapServiceConditionals($value, $code)
|
Chris@0
|
1187 {
|
Chris@0
|
1188 if (!$services = ContainerBuilder::getServiceConditionals($value)) {
|
Chris@0
|
1189 return $code;
|
Chris@0
|
1190 }
|
Chris@0
|
1191
|
Chris@0
|
1192 $conditions = array();
|
Chris@0
|
1193 foreach ($services as $service) {
|
Chris@0
|
1194 $conditions[] = sprintf("\$this->has('%s')", $service);
|
Chris@0
|
1195 }
|
Chris@0
|
1196
|
Chris@0
|
1197 // re-indent the wrapped code
|
Chris@0
|
1198 $code = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code)));
|
Chris@0
|
1199
|
Chris@0
|
1200 return sprintf(" if (%s) {\n%s }\n", implode(' && ', $conditions), $code);
|
Chris@0
|
1201 }
|
Chris@0
|
1202
|
Chris@0
|
1203 /**
|
Chris@0
|
1204 * Builds service calls from arguments.
|
Chris@0
|
1205 *
|
Chris@0
|
1206 * @param array $arguments
|
Chris@0
|
1207 * @param array &$calls By reference
|
Chris@0
|
1208 * @param array &$behavior By reference
|
Chris@0
|
1209 */
|
Chris@0
|
1210 private function getServiceCallsFromArguments(array $arguments, array &$calls, array &$behavior)
|
Chris@0
|
1211 {
|
Chris@0
|
1212 foreach ($arguments as $argument) {
|
Chris@0
|
1213 if (is_array($argument)) {
|
Chris@0
|
1214 $this->getServiceCallsFromArguments($argument, $calls, $behavior);
|
Chris@0
|
1215 } elseif ($argument instanceof Reference) {
|
Chris@0
|
1216 $id = (string) $argument;
|
Chris@0
|
1217
|
Chris@0
|
1218 if (!isset($calls[$id])) {
|
Chris@0
|
1219 $calls[$id] = 0;
|
Chris@0
|
1220 }
|
Chris@0
|
1221 if (!isset($behavior[$id])) {
|
Chris@0
|
1222 $behavior[$id] = $argument->getInvalidBehavior();
|
Chris@0
|
1223 } elseif (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $behavior[$id]) {
|
Chris@0
|
1224 $behavior[$id] = $argument->getInvalidBehavior();
|
Chris@0
|
1225 }
|
Chris@0
|
1226
|
Chris@0
|
1227 ++$calls[$id];
|
Chris@0
|
1228 }
|
Chris@0
|
1229 }
|
Chris@0
|
1230 }
|
Chris@0
|
1231
|
Chris@0
|
1232 /**
|
Chris@0
|
1233 * Returns the inline definition.
|
Chris@0
|
1234 *
|
Chris@0
|
1235 * @param Definition $definition
|
Chris@0
|
1236 *
|
Chris@0
|
1237 * @return array
|
Chris@0
|
1238 */
|
Chris@0
|
1239 private function getInlinedDefinitions(Definition $definition)
|
Chris@0
|
1240 {
|
Chris@0
|
1241 if (false === $this->inlinedDefinitions->contains($definition)) {
|
Chris@0
|
1242 $definitions = array_merge(
|
Chris@0
|
1243 $this->getDefinitionsFromArguments($definition->getArguments()),
|
Chris@0
|
1244 $this->getDefinitionsFromArguments($definition->getMethodCalls()),
|
Chris@0
|
1245 $this->getDefinitionsFromArguments($definition->getProperties()),
|
Chris@0
|
1246 $this->getDefinitionsFromArguments(array($definition->getConfigurator())),
|
Chris@0
|
1247 $this->getDefinitionsFromArguments(array($definition->getFactory()))
|
Chris@0
|
1248 );
|
Chris@0
|
1249
|
Chris@0
|
1250 $this->inlinedDefinitions->offsetSet($definition, $definitions);
|
Chris@0
|
1251
|
Chris@0
|
1252 return $definitions;
|
Chris@0
|
1253 }
|
Chris@0
|
1254
|
Chris@0
|
1255 return $this->inlinedDefinitions->offsetGet($definition);
|
Chris@0
|
1256 }
|
Chris@0
|
1257
|
Chris@0
|
1258 /**
|
Chris@0
|
1259 * Gets the definition from arguments.
|
Chris@0
|
1260 *
|
Chris@0
|
1261 * @param array $arguments
|
Chris@0
|
1262 *
|
Chris@0
|
1263 * @return array
|
Chris@0
|
1264 */
|
Chris@0
|
1265 private function getDefinitionsFromArguments(array $arguments)
|
Chris@0
|
1266 {
|
Chris@0
|
1267 $definitions = array();
|
Chris@0
|
1268 foreach ($arguments as $argument) {
|
Chris@0
|
1269 if (is_array($argument)) {
|
Chris@0
|
1270 $definitions = array_merge($definitions, $this->getDefinitionsFromArguments($argument));
|
Chris@0
|
1271 } elseif ($argument instanceof Definition) {
|
Chris@0
|
1272 $definitions = array_merge(
|
Chris@0
|
1273 $definitions,
|
Chris@0
|
1274 $this->getInlinedDefinitions($argument),
|
Chris@0
|
1275 array($argument)
|
Chris@0
|
1276 );
|
Chris@0
|
1277 }
|
Chris@0
|
1278 }
|
Chris@0
|
1279
|
Chris@0
|
1280 return $definitions;
|
Chris@0
|
1281 }
|
Chris@0
|
1282
|
Chris@0
|
1283 /**
|
Chris@0
|
1284 * Checks if a service id has a reference.
|
Chris@0
|
1285 *
|
Chris@0
|
1286 * @param string $id
|
Chris@0
|
1287 * @param array $arguments
|
Chris@0
|
1288 * @param bool $deep
|
Chris@0
|
1289 * @param array $visited
|
Chris@0
|
1290 *
|
Chris@0
|
1291 * @return bool
|
Chris@0
|
1292 */
|
Chris@0
|
1293 private function hasReference($id, array $arguments, $deep = false, array &$visited = array())
|
Chris@0
|
1294 {
|
Chris@0
|
1295 foreach ($arguments as $argument) {
|
Chris@0
|
1296 if (is_array($argument)) {
|
Chris@0
|
1297 if ($this->hasReference($id, $argument, $deep, $visited)) {
|
Chris@0
|
1298 return true;
|
Chris@0
|
1299 }
|
Chris@0
|
1300 } elseif ($argument instanceof Reference) {
|
Chris@0
|
1301 $argumentId = (string) $argument;
|
Chris@0
|
1302 if ($id === $argumentId) {
|
Chris@0
|
1303 return true;
|
Chris@0
|
1304 }
|
Chris@0
|
1305
|
Chris@0
|
1306 if ($deep && !isset($visited[$argumentId]) && 'service_container' !== $argumentId) {
|
Chris@0
|
1307 $visited[$argumentId] = true;
|
Chris@0
|
1308
|
Chris@0
|
1309 $service = $this->container->getDefinition($argumentId);
|
Chris@0
|
1310
|
Chris@0
|
1311 // if the proxy manager is enabled, disable searching for references in lazy services,
|
Chris@0
|
1312 // as these services will be instantiated lazily and don't have direct related references.
|
Chris@0
|
1313 if ($service->isLazy() && !$this->getProxyDumper() instanceof NullDumper) {
|
Chris@0
|
1314 continue;
|
Chris@0
|
1315 }
|
Chris@0
|
1316
|
Chris@0
|
1317 $arguments = array_merge($service->getMethodCalls(), $service->getArguments(), $service->getProperties());
|
Chris@0
|
1318
|
Chris@0
|
1319 if ($this->hasReference($id, $arguments, $deep, $visited)) {
|
Chris@0
|
1320 return true;
|
Chris@0
|
1321 }
|
Chris@0
|
1322 }
|
Chris@0
|
1323 }
|
Chris@0
|
1324 }
|
Chris@0
|
1325
|
Chris@0
|
1326 return false;
|
Chris@0
|
1327 }
|
Chris@0
|
1328
|
Chris@0
|
1329 /**
|
Chris@0
|
1330 * Dumps values.
|
Chris@0
|
1331 *
|
Chris@0
|
1332 * @param mixed $value
|
Chris@0
|
1333 * @param bool $interpolate
|
Chris@0
|
1334 *
|
Chris@0
|
1335 * @return string
|
Chris@0
|
1336 *
|
Chris@0
|
1337 * @throws RuntimeException
|
Chris@0
|
1338 */
|
Chris@0
|
1339 private function dumpValue($value, $interpolate = true)
|
Chris@0
|
1340 {
|
Chris@0
|
1341 if (is_array($value)) {
|
Chris@0
|
1342 $code = array();
|
Chris@0
|
1343 foreach ($value as $k => $v) {
|
Chris@0
|
1344 $code[] = sprintf('%s => %s', $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate));
|
Chris@0
|
1345 }
|
Chris@0
|
1346
|
Chris@0
|
1347 return sprintf('array(%s)', implode(', ', $code));
|
Chris@0
|
1348 } elseif ($value instanceof Definition) {
|
Chris@0
|
1349 if (null !== $this->definitionVariables && $this->definitionVariables->contains($value)) {
|
Chris@0
|
1350 return $this->dumpValue($this->definitionVariables->offsetGet($value), $interpolate);
|
Chris@0
|
1351 }
|
Chris@0
|
1352 if ($value->getMethodCalls()) {
|
Chris@0
|
1353 throw new RuntimeException('Cannot dump definitions which have method calls.');
|
Chris@0
|
1354 }
|
Chris@0
|
1355 if ($value->getProperties()) {
|
Chris@0
|
1356 throw new RuntimeException('Cannot dump definitions which have properties.');
|
Chris@0
|
1357 }
|
Chris@0
|
1358 if (null !== $value->getConfigurator()) {
|
Chris@0
|
1359 throw new RuntimeException('Cannot dump definitions which have a configurator.');
|
Chris@0
|
1360 }
|
Chris@0
|
1361
|
Chris@0
|
1362 $arguments = array();
|
Chris@0
|
1363 foreach ($value->getArguments() as $argument) {
|
Chris@0
|
1364 $arguments[] = $this->dumpValue($argument);
|
Chris@0
|
1365 }
|
Chris@0
|
1366
|
Chris@0
|
1367 if (null !== $value->getFactory()) {
|
Chris@0
|
1368 $factory = $value->getFactory();
|
Chris@0
|
1369
|
Chris@0
|
1370 if (is_string($factory)) {
|
Chris@0
|
1371 return sprintf('%s(%s)', $this->dumpLiteralClass($this->dumpValue($factory)), implode(', ', $arguments));
|
Chris@0
|
1372 }
|
Chris@0
|
1373
|
Chris@0
|
1374 if (is_array($factory)) {
|
Chris@0
|
1375 if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $factory[1])) {
|
Chris@0
|
1376 throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s)', $factory[1] ?: 'n/a'));
|
Chris@0
|
1377 }
|
Chris@0
|
1378
|
Chris@0
|
1379 if (is_string($factory[0])) {
|
Chris@0
|
1380 return sprintf('%s::%s(%s)', $this->dumpLiteralClass($this->dumpValue($factory[0])), $factory[1], implode(', ', $arguments));
|
Chris@0
|
1381 }
|
Chris@0
|
1382
|
Chris@0
|
1383 if ($factory[0] instanceof Definition) {
|
Chris@0
|
1384 return sprintf("call_user_func(array(%s, '%s')%s)", $this->dumpValue($factory[0]), $factory[1], count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
|
Chris@0
|
1385 }
|
Chris@0
|
1386
|
Chris@0
|
1387 if ($factory[0] instanceof Reference) {
|
Chris@0
|
1388 return sprintf('%s->%s(%s)', $this->dumpValue($factory[0]), $factory[1], implode(', ', $arguments));
|
Chris@0
|
1389 }
|
Chris@0
|
1390 }
|
Chris@0
|
1391
|
Chris@0
|
1392 throw new RuntimeException('Cannot dump definition because of invalid factory');
|
Chris@0
|
1393 }
|
Chris@0
|
1394
|
Chris@0
|
1395 $class = $value->getClass();
|
Chris@0
|
1396 if (null === $class) {
|
Chris@0
|
1397 throw new RuntimeException('Cannot dump definitions which have no class nor factory.');
|
Chris@0
|
1398 }
|
Chris@0
|
1399
|
Chris@0
|
1400 return sprintf('new %s(%s)', $this->dumpLiteralClass($this->dumpValue($class)), implode(', ', $arguments));
|
Chris@0
|
1401 } elseif ($value instanceof Variable) {
|
Chris@0
|
1402 return '$'.$value;
|
Chris@0
|
1403 } elseif ($value instanceof Reference) {
|
Chris@0
|
1404 if (null !== $this->referenceVariables && isset($this->referenceVariables[$id = (string) $value])) {
|
Chris@0
|
1405 return $this->dumpValue($this->referenceVariables[$id], $interpolate);
|
Chris@0
|
1406 }
|
Chris@0
|
1407
|
Chris@0
|
1408 return $this->getServiceCall((string) $value, $value);
|
Chris@0
|
1409 } elseif ($value instanceof Expression) {
|
Chris@0
|
1410 return $this->getExpressionLanguage()->compile((string) $value, array('this' => 'container'));
|
Chris@0
|
1411 } elseif ($value instanceof Parameter) {
|
Chris@0
|
1412 return $this->dumpParameter($value);
|
Chris@0
|
1413 } elseif (true === $interpolate && is_string($value)) {
|
Chris@0
|
1414 if (preg_match('/^%([^%]+)%$/', $value, $match)) {
|
Chris@0
|
1415 // we do this to deal with non string values (Boolean, integer, ...)
|
Chris@0
|
1416 // the preg_replace_callback converts them to strings
|
Chris@0
|
1417 return $this->dumpParameter(strtolower($match[1]));
|
Chris@0
|
1418 } else {
|
Chris@0
|
1419 $replaceParameters = function ($match) {
|
Chris@0
|
1420 return "'.".$this->dumpParameter(strtolower($match[2])).".'";
|
Chris@0
|
1421 };
|
Chris@0
|
1422
|
Chris@0
|
1423 $code = str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\1/', $replaceParameters, $this->export($value)));
|
Chris@0
|
1424
|
Chris@0
|
1425 return $code;
|
Chris@0
|
1426 }
|
Chris@0
|
1427 } elseif (is_object($value) || is_resource($value)) {
|
Chris@0
|
1428 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
|
Chris@0
|
1429 }
|
Chris@0
|
1430
|
Chris@0
|
1431 return $this->export($value);
|
Chris@0
|
1432 }
|
Chris@0
|
1433
|
Chris@0
|
1434 /**
|
Chris@0
|
1435 * Dumps a string to a literal (aka PHP Code) class value.
|
Chris@0
|
1436 *
|
Chris@0
|
1437 * @param string $class
|
Chris@0
|
1438 *
|
Chris@0
|
1439 * @return string
|
Chris@0
|
1440 *
|
Chris@0
|
1441 * @throws RuntimeException
|
Chris@0
|
1442 */
|
Chris@0
|
1443 private function dumpLiteralClass($class)
|
Chris@0
|
1444 {
|
Chris@0
|
1445 if (false !== strpos($class, '$')) {
|
Chris@0
|
1446 return sprintf('${($_ = %s) && false ?: "_"}', $class);
|
Chris@0
|
1447 }
|
Chris@0
|
1448 if (0 !== strpos($class, "'") || !preg_match('/^\'[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
|
1449 throw new RuntimeException(sprintf('Cannot dump definition because of invalid class name (%s)', $class ?: 'n/a'));
|
Chris@0
|
1450 }
|
Chris@0
|
1451
|
Chris@0
|
1452 return '\\'.substr(str_replace('\\\\', '\\', $class), 1, -1);
|
Chris@0
|
1453 }
|
Chris@0
|
1454
|
Chris@0
|
1455 /**
|
Chris@0
|
1456 * Dumps a parameter.
|
Chris@0
|
1457 *
|
Chris@0
|
1458 * @param string $name
|
Chris@0
|
1459 *
|
Chris@0
|
1460 * @return string
|
Chris@0
|
1461 */
|
Chris@0
|
1462 private function dumpParameter($name)
|
Chris@0
|
1463 {
|
Chris@0
|
1464 if ($this->container->isFrozen() && $this->container->hasParameter($name)) {
|
Chris@0
|
1465 return $this->dumpValue($this->container->getParameter($name), false);
|
Chris@0
|
1466 }
|
Chris@0
|
1467
|
Chris@0
|
1468 return sprintf("\$this->getParameter('%s')", strtolower($name));
|
Chris@0
|
1469 }
|
Chris@0
|
1470
|
Chris@0
|
1471 /**
|
Chris@0
|
1472 * Gets a service call.
|
Chris@0
|
1473 *
|
Chris@0
|
1474 * @param string $id
|
Chris@0
|
1475 * @param Reference $reference
|
Chris@0
|
1476 *
|
Chris@0
|
1477 * @return string
|
Chris@0
|
1478 */
|
Chris@0
|
1479 private function getServiceCall($id, Reference $reference = null)
|
Chris@0
|
1480 {
|
Chris@0
|
1481 if ('service_container' === $id) {
|
Chris@0
|
1482 return '$this';
|
Chris@0
|
1483 }
|
Chris@0
|
1484
|
Chris@0
|
1485 if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
|
Chris@0
|
1486 // The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? \$this->{$this->generateMethodName($id)}())" on PHP>=7.0
|
Chris@0
|
1487
|
Chris@0
|
1488 return "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : \$this->{$this->generateMethodName($id)}()) && false ?: '_'}";
|
Chris@0
|
1489 }
|
Chris@0
|
1490 if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
|
Chris@0
|
1491 return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
|
Chris@0
|
1492 }
|
Chris@0
|
1493
|
Chris@0
|
1494 if ($this->container->hasAlias($id)) {
|
Chris@0
|
1495 $id = (string) $this->container->getAlias($id);
|
Chris@0
|
1496 }
|
Chris@0
|
1497
|
Chris@0
|
1498 return sprintf('$this->get(\'%s\')', $id);
|
Chris@0
|
1499 }
|
Chris@0
|
1500
|
Chris@0
|
1501 /**
|
Chris@0
|
1502 * Initializes the method names map to avoid conflicts with the Container methods.
|
Chris@0
|
1503 *
|
Chris@0
|
1504 * @param string $class the container base class
|
Chris@0
|
1505 */
|
Chris@0
|
1506 private function initializeMethodNamesMap($class)
|
Chris@0
|
1507 {
|
Chris@0
|
1508 $this->serviceIdToMethodNameMap = array();
|
Chris@0
|
1509 $this->usedMethodNames = array();
|
Chris@0
|
1510
|
Chris@0
|
1511 try {
|
Chris@0
|
1512 $reflectionClass = new \ReflectionClass($class);
|
Chris@0
|
1513 foreach ($reflectionClass->getMethods() as $method) {
|
Chris@0
|
1514 $this->usedMethodNames[strtolower($method->getName())] = true;
|
Chris@0
|
1515 }
|
Chris@0
|
1516 } catch (\ReflectionException $e) {
|
Chris@0
|
1517 }
|
Chris@0
|
1518 }
|
Chris@0
|
1519
|
Chris@0
|
1520 /**
|
Chris@0
|
1521 * Convert a service id to a valid PHP method name.
|
Chris@0
|
1522 *
|
Chris@0
|
1523 * @param string $id
|
Chris@0
|
1524 *
|
Chris@0
|
1525 * @return string
|
Chris@0
|
1526 *
|
Chris@0
|
1527 * @throws InvalidArgumentException
|
Chris@0
|
1528 */
|
Chris@0
|
1529 private function generateMethodName($id)
|
Chris@0
|
1530 {
|
Chris@0
|
1531 if (isset($this->serviceIdToMethodNameMap[$id])) {
|
Chris@0
|
1532 return $this->serviceIdToMethodNameMap[$id];
|
Chris@0
|
1533 }
|
Chris@0
|
1534
|
Chris@0
|
1535 $name = Container::camelize($id);
|
Chris@0
|
1536 $name = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '', $name);
|
Chris@0
|
1537 $methodName = 'get'.$name.'Service';
|
Chris@0
|
1538 $suffix = 1;
|
Chris@0
|
1539
|
Chris@0
|
1540 while (isset($this->usedMethodNames[strtolower($methodName)])) {
|
Chris@0
|
1541 ++$suffix;
|
Chris@0
|
1542 $methodName = 'get'.$name.$suffix.'Service';
|
Chris@0
|
1543 }
|
Chris@0
|
1544
|
Chris@0
|
1545 $this->serviceIdToMethodNameMap[$id] = $methodName;
|
Chris@0
|
1546 $this->usedMethodNames[strtolower($methodName)] = true;
|
Chris@0
|
1547
|
Chris@0
|
1548 return $methodName;
|
Chris@0
|
1549 }
|
Chris@0
|
1550
|
Chris@0
|
1551 /**
|
Chris@0
|
1552 * Returns the next name to use.
|
Chris@0
|
1553 *
|
Chris@0
|
1554 * @return string
|
Chris@0
|
1555 */
|
Chris@0
|
1556 private function getNextVariableName()
|
Chris@0
|
1557 {
|
Chris@0
|
1558 $firstChars = self::FIRST_CHARS;
|
Chris@0
|
1559 $firstCharsLength = strlen($firstChars);
|
Chris@0
|
1560 $nonFirstChars = self::NON_FIRST_CHARS;
|
Chris@0
|
1561 $nonFirstCharsLength = strlen($nonFirstChars);
|
Chris@0
|
1562
|
Chris@0
|
1563 while (true) {
|
Chris@0
|
1564 $name = '';
|
Chris@0
|
1565 $i = $this->variableCount;
|
Chris@0
|
1566
|
Chris@0
|
1567 if ('' === $name) {
|
Chris@0
|
1568 $name .= $firstChars[$i % $firstCharsLength];
|
Chris@0
|
1569 $i = (int) ($i / $firstCharsLength);
|
Chris@0
|
1570 }
|
Chris@0
|
1571
|
Chris@0
|
1572 while ($i > 0) {
|
Chris@0
|
1573 --$i;
|
Chris@0
|
1574 $name .= $nonFirstChars[$i % $nonFirstCharsLength];
|
Chris@0
|
1575 $i = (int) ($i / $nonFirstCharsLength);
|
Chris@0
|
1576 }
|
Chris@0
|
1577
|
Chris@0
|
1578 ++$this->variableCount;
|
Chris@0
|
1579
|
Chris@0
|
1580 // check that the name is not reserved
|
Chris@0
|
1581 if (in_array($name, $this->reservedVariables, true)) {
|
Chris@0
|
1582 continue;
|
Chris@0
|
1583 }
|
Chris@0
|
1584
|
Chris@0
|
1585 return $name;
|
Chris@0
|
1586 }
|
Chris@0
|
1587 }
|
Chris@0
|
1588
|
Chris@0
|
1589 private function getExpressionLanguage()
|
Chris@0
|
1590 {
|
Chris@0
|
1591 if (null === $this->expressionLanguage) {
|
Chris@0
|
1592 if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
|
Chris@0
|
1593 throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
|
Chris@0
|
1594 }
|
Chris@0
|
1595 $providers = $this->container->getExpressionLanguageProviders();
|
Chris@0
|
1596 $this->expressionLanguage = new ExpressionLanguage(null, $providers);
|
Chris@0
|
1597
|
Chris@0
|
1598 if ($this->container->isTrackingResources()) {
|
Chris@0
|
1599 foreach ($providers as $provider) {
|
Chris@0
|
1600 $this->container->addObjectResource($provider);
|
Chris@0
|
1601 }
|
Chris@0
|
1602 }
|
Chris@0
|
1603 }
|
Chris@0
|
1604
|
Chris@0
|
1605 return $this->expressionLanguage;
|
Chris@0
|
1606 }
|
Chris@0
|
1607
|
Chris@0
|
1608 private function exportTargetDirs()
|
Chris@0
|
1609 {
|
Chris@0
|
1610 return null === $this->targetDirRegex ? '' : <<<EOF
|
Chris@0
|
1611
|
Chris@0
|
1612 \$dir = __DIR__;
|
Chris@0
|
1613 for (\$i = 1; \$i <= {$this->targetDirMaxMatches}; ++\$i) {
|
Chris@0
|
1614 \$this->targetDirs[\$i] = \$dir = dirname(\$dir);
|
Chris@0
|
1615 }
|
Chris@0
|
1616 EOF;
|
Chris@0
|
1617 }
|
Chris@0
|
1618
|
Chris@0
|
1619 private function export($value)
|
Chris@0
|
1620 {
|
Chris@0
|
1621 if (null !== $this->targetDirRegex && is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) {
|
Chris@0
|
1622 $prefix = $matches[0][1] ? $this->doExport(substr($value, 0, $matches[0][1])).'.' : '';
|
Chris@0
|
1623 $suffix = $matches[0][1] + strlen($matches[0][0]);
|
Chris@0
|
1624 $suffix = isset($value[$suffix]) ? '.'.$this->doExport(substr($value, $suffix)) : '';
|
Chris@0
|
1625 $dirname = '__DIR__';
|
Chris@0
|
1626
|
Chris@0
|
1627 if (0 < $offset = 1 + $this->targetDirMaxMatches - count($matches)) {
|
Chris@0
|
1628 $dirname = sprintf('$this->targetDirs[%d]', $offset);
|
Chris@0
|
1629 }
|
Chris@0
|
1630
|
Chris@0
|
1631 if ($prefix || $suffix) {
|
Chris@0
|
1632 return sprintf('(%s%s%s)', $prefix, $dirname, $suffix);
|
Chris@0
|
1633 }
|
Chris@0
|
1634
|
Chris@0
|
1635 return $dirname;
|
Chris@0
|
1636 }
|
Chris@0
|
1637
|
Chris@0
|
1638 return $this->doExport($value);
|
Chris@0
|
1639 }
|
Chris@0
|
1640
|
Chris@0
|
1641 private function doExport($value)
|
Chris@0
|
1642 {
|
Chris@0
|
1643 $export = var_export($value, true);
|
Chris@0
|
1644
|
Chris@0
|
1645 if ("'" === $export[0] && $export !== $resolvedExport = $this->container->resolveEnvPlaceholders($export, "'.\$this->getEnv('%s').'")) {
|
Chris@0
|
1646 $export = $resolvedExport;
|
Chris@0
|
1647 if ("'" === $export[1]) {
|
Chris@0
|
1648 $export = substr($export, 3);
|
Chris@0
|
1649 }
|
Chris@0
|
1650 if (".''" === substr($export, -3)) {
|
Chris@0
|
1651 $export = substr($export, 0, -3);
|
Chris@0
|
1652 }
|
Chris@0
|
1653 }
|
Chris@0
|
1654
|
Chris@0
|
1655 return $export;
|
Chris@0
|
1656 }
|
Chris@0
|
1657 }
|