21
21
* merged Definition instance.
22
22
*
23
23
* @author Johannes M. Schmitt <[email protected] >
24
+ * @author Nicolas Grekas <[email protected] >
24
25
*/
25
26
class ResolveDefinitionTemplatesPass implements CompilerPassInterface
26
27
{
27
- private $ container ;
28
28
private $ compiler ;
29
29
private $ formatter ;
30
+ private $ currentId ;
30
31
31
32
/**
32
33
* Process the ContainerBuilder to replace DefinitionDecorator instances with their real Definition instances.
@@ -35,44 +36,84 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
35
36
*/
36
37
public function process (ContainerBuilder $ container )
37
38
{
38
- $ this ->container = $ container ;
39
39
$ this ->compiler = $ container ->getCompiler ();
40
40
$ this ->formatter = $ this ->compiler ->getLoggingFormatter ();
41
41
42
42
foreach ($ container ->getDefinitions () as $ id => $ definition ) {
43
+ $ this ->currentId = $ id ;
44
+
43
45
// yes, we are specifically fetching the definition from the
44
46
// container to ensure we are not operating on stale data
45
47
$ definition = $ container ->getDefinition ($ id );
46
- if (!$ definition instanceof DefinitionDecorator || $ definition ->isAbstract ()) {
47
- continue ;
48
+ if ($ definition instanceof DefinitionDecorator) {
49
+ $ definition = $ this ->resolveDefinition ($ container , $ definition );
50
+ $ container ->setDefinition ($ id , $ definition );
48
51
}
49
52
50
- $ this ->resolveDefinition ($ id , $ definition );
53
+ $ definition ->setArguments ($ this ->resolveArguments ($ container , $ definition ->getArguments ()));
54
+ $ definition ->setMethodCalls ($ this ->resolveArguments ($ container , $ definition ->getMethodCalls ()));
55
+ $ definition ->setProperties ($ this ->resolveArguments ($ container , $ definition ->getProperties ()));
56
+
57
+ $ configurator = $ this ->resolveArguments ($ container , array ($ definition ->getConfigurator ()));
58
+ $ definition ->setConfigurator ($ configurator [0 ]);
59
+
60
+ $ factory = $ this ->resolveArguments ($ container , array ($ definition ->getFactory ()));
61
+ $ definition ->setFactory ($ factory [0 ]);
51
62
}
52
63
}
53
64
65
+ /**
66
+ * Resolves definition decorator arguments.
67
+ *
68
+ * @param ContainerBuilder $container The ContainerBuilder
69
+ * @param array $arguments An array of arguments
70
+ *
71
+ * @return array
72
+ */
73
+ private function resolveArguments (ContainerBuilder $ container , array $ arguments )
74
+ {
75
+ foreach ($ arguments as $ k => $ argument ) {
76
+ if (is_array ($ argument )) {
77
+ $ arguments [$ k ] = $ this ->resolveArguments ($ container , $ argument );
78
+ } elseif ($ argument instanceof Definition) {
79
+ if ($ argument instanceof DefinitionDecorator) {
80
+ $ arguments [$ k ] = $ argument = $ this ->resolveDefinition ($ container , $ argument );
81
+ }
82
+ $ argument ->setArguments ($ this ->resolveArguments ($ container , $ argument ->getArguments ()));
83
+ $ argument ->setMethodCalls ($ this ->resolveArguments ($ container , $ argument ->getMethodCalls ()));
84
+ $ argument ->setProperties ($ this ->resolveArguments ($ container , $ argument ->getProperties ()));
85
+ }
86
+ }
87
+
88
+ return $ arguments ;
89
+ }
90
+
54
91
/**
55
92
* Resolves the definition.
56
93
*
57
- * @param string $id The definition identifier
94
+ * @param ContainerBuilder $container The ContainerBuilder
58
95
* @param DefinitionDecorator $definition
59
96
*
60
97
* @return Definition
61
98
*
62
99
* @throws \RuntimeException When the definition is invalid
63
100
*/
64
- private function resolveDefinition ($ id , DefinitionDecorator $ definition )
101
+ private function resolveDefinition (ContainerBuilder $ container , DefinitionDecorator $ definition )
65
102
{
66
- if (!$ this -> container ->hasDefinition ($ parent = $ definition ->getParent ())) {
67
- throw new RuntimeException (sprintf ('The parent definition "%s" defined for definition "%s" does not exist. ' , $ parent , $ id ));
103
+ if (!$ container ->hasDefinition ($ parent = $ definition ->getParent ())) {
104
+ throw new RuntimeException (sprintf ('The parent definition "%s" defined for definition "%s" does not exist. ' , $ parent , $ this -> currentId ));
68
105
}
69
106
70
- $ parentDef = $ this -> container ->getDefinition ($ parent );
107
+ $ parentDef = $ container ->getDefinition ($ parent );
71
108
if ($ parentDef instanceof DefinitionDecorator) {
72
- $ parentDef = $ this ->resolveDefinition ($ parent , $ parentDef );
109
+ $ id = $ this ->currentId ;
110
+ $ this ->currentId = $ parent ;
111
+ $ parentDef = $ this ->resolveDefinition ($ container , $ parentDef );
112
+ $ container ->setDefinition ($ parent , $ parentDef );
113
+ $ this ->currentId = $ id ;
73
114
}
74
115
75
- $ this ->compiler ->addLogMessage ($ this ->formatter ->formatResolveInheritance ($ this , $ id , $ parent ));
116
+ $ this ->compiler ->addLogMessage ($ this ->formatter ->formatResolveInheritance ($ this , $ this -> currentId , $ parent ));
76
117
$ def = new Definition ();
77
118
78
119
// merge in parent definition
@@ -156,9 +197,6 @@ private function resolveDefinition($id, DefinitionDecorator $definition)
156
197
$ def ->setScope ($ definition ->getScope (false ), false );
157
198
$ def ->setTags ($ definition ->getTags ());
158
199
159
- // set new definition on container
160
- $ this ->container ->setDefinition ($ id , $ def );
161
-
162
200
return $ def ;
163
201
}
164
202
}
0 commit comments