|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\DependencyInjection\Compiler; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
15 |
use Symfony\Component\DependencyInjection\Reference; |
|
|
16 |
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
|
17 |
|
|
|
18 |
/** |
|
|
19 |
* Emulates the invalid behavior if the reference is not found within the |
|
|
20 |
* container. |
|
|
21 |
* |
|
|
22 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
23 |
*/ |
|
|
24 |
class ResolveInvalidReferencesPass implements CompilerPassInterface |
|
|
25 |
{ |
|
|
26 |
private $container; |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* Process the ContainerBuilder to resolve invalid references. |
|
|
30 |
* |
|
|
31 |
* @param ContainerBuilder $container |
|
|
32 |
*/ |
|
|
33 |
public function process(ContainerBuilder $container) |
|
|
34 |
{ |
|
|
35 |
$this->container = $container; |
|
|
36 |
foreach ($container->getDefinitions() as $definition) { |
|
|
37 |
if ($definition->isSynthetic() || $definition->isAbstract()) { |
|
|
38 |
continue; |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
$definition->setArguments( |
|
|
42 |
$this->processArguments($definition->getArguments()) |
|
|
43 |
); |
|
|
44 |
|
|
|
45 |
$calls = array(); |
|
|
46 |
foreach ($definition->getMethodCalls() as $call) { |
|
|
47 |
try { |
|
|
48 |
$calls[] = array($call[0], $this->processArguments($call[1], true)); |
|
|
49 |
} catch (\RuntimeException $ignore) { |
|
|
50 |
// this call is simply removed |
|
|
51 |
} |
|
|
52 |
} |
|
|
53 |
$definition->setMethodCalls($calls); |
|
|
54 |
|
|
|
55 |
$properties = array(); |
|
|
56 |
foreach ($definition->getProperties() as $name => $value) { |
|
|
57 |
try { |
|
|
58 |
$value = $this->processArguments(array($value), true); |
|
|
59 |
$properties[$name] = reset($value); |
|
|
60 |
} catch (\RuntimeException $ignore) { |
|
|
61 |
// ignore property |
|
|
62 |
} |
|
|
63 |
} |
|
|
64 |
$definition->setProperties($properties); |
|
|
65 |
} |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
/** |
|
|
69 |
* Processes arguments to determine invalid references. |
|
|
70 |
* |
|
|
71 |
* @param array $arguments An array of Reference objects |
|
|
72 |
* @param Boolean $inMethodCall |
|
|
73 |
*/ |
|
|
74 |
private function processArguments(array $arguments, $inMethodCall = false) |
|
|
75 |
{ |
|
|
76 |
foreach ($arguments as $k => $argument) { |
|
|
77 |
if (is_array($argument)) { |
|
|
78 |
$arguments[$k] = $this->processArguments($argument, $inMethodCall); |
|
|
79 |
} else if ($argument instanceof Reference) { |
|
|
80 |
$id = (string) $argument; |
|
|
81 |
|
|
|
82 |
$invalidBehavior = $argument->getInvalidBehavior(); |
|
|
83 |
$exists = $this->container->has($id); |
|
|
84 |
|
|
|
85 |
// resolve invalid behavior |
|
|
86 |
if ($exists && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) { |
|
|
87 |
$arguments[$k] = new Reference($id); |
|
|
88 |
} else if (!$exists && ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) { |
|
|
89 |
$arguments[$k] = null; |
|
|
90 |
} else if (!$exists && ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) { |
|
|
91 |
if ($inMethodCall) { |
|
|
92 |
throw new \RuntimeException('Method shouldn\'t be called.'); |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
$arguments[$k] = null; |
|
|
96 |
} |
|
|
97 |
} |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
return $arguments; |
|
|
101 |
} |
|
|
102 |
} |