@@ -16,21 +16,37 @@ class Index implements ReadableIndex, \Serializable
16
16
17
17
/**
18
18
* An associative array that maps splitted fully qualified symbol names
19
- * to definitions, eg :
19
+ * to non-member definitions, eg :
20
20
* [
21
21
* 'Psr' => [
22
22
* '\Log' => [
23
23
* '\LoggerInterface' => [
24
- * '' => $def1, // definition for 'Psr\Log\LoggerInterface' which is non-member
25
- * '->log()' => $def2, // definition for 'Psr\Log\LoggerInterface->log()' which is a member definition
24
+ * '' => $definition,
26
25
* ],
27
26
* ],
28
27
* ],
29
28
* ]
30
29
*
31
30
* @var array
32
31
*/
33
- private $ definitions = [];
32
+ private $ nonMemberDefinitions = [];
33
+
34
+ /**
35
+ * An associative array that maps splitted fully qualified symbol names
36
+ * to member definitions, eg :
37
+ * [
38
+ * 'Psr' => [
39
+ * '\Log' => [
40
+ * '\LoggerInterface' => [
41
+ * '->log()' => $definition,
42
+ * ],
43
+ * ],
44
+ * ],
45
+ * ]
46
+ *
47
+ * @var array
48
+ */
49
+ private $ memberDefinitions = [];
34
50
35
51
/**
36
52
* An associative array that maps fully qualified symbol names
@@ -99,20 +115,29 @@ public function isStaticComplete(): bool
99
115
* Returns a Generator providing an associative array [string => Definition]
100
116
* that maps fully qualified symbol names to Definitions (global or not)
101
117
*
118
+ * @param boolean|null $member Indicates if we want member or non-member definitions (null for both, default null)
102
119
* @return \Generator yields Definition
103
120
*/
104
- public function getDefinitions (): \Generator
121
+ public function getDefinitions (bool $ member = null ): \Generator
105
122
{
106
- yield from $ this ->yieldDefinitionsRecursively ($ this ->definitions );
123
+ if (true === $ member ) {
124
+ yield from $ this ->yieldDefinitionsRecursively ($ this ->memberDefinitions );
125
+ } elseif (false === $ member ) {
126
+ yield from $ this ->yieldDefinitionsRecursively ($ this ->nonMemberDefinitions );
127
+ } else {
128
+ yield from $ this ->yieldDefinitionsRecursively ($ this ->memberDefinitions );
129
+ yield from $ this ->yieldDefinitionsRecursively ($ this ->nonMemberDefinitions );
130
+ }
107
131
}
108
132
109
133
/**
110
134
* Returns a Generator that yields all the descendant Definitions of a given FQN
111
135
*
112
136
* @param string $fqn
137
+ * @param boolean|null $member Indicates if we want member or non-member definitions (null for both, default null)
113
138
* @return \Generator yields Definition
114
139
*/
115
- public function getDescendantDefinitionsForFqn (string $ fqn ): \Generator
140
+ public function getDescendantDefinitionsForFqn (string $ fqn, bool $ member = null ): \Generator
116
141
{
117
142
$ parts = $ this ->splitFqn ($ fqn );
118
143
if ('' === end ($ parts )) {
@@ -121,12 +146,13 @@ public function getDescendantDefinitionsForFqn(string $fqn): \Generator
121
146
array_pop ($ parts );
122
147
}
123
148
124
- $ result = $ this ->getIndexValue ($ parts , $ this ->definitions );
125
-
126
- if ($ result instanceof Definition) {
127
- yield $ fqn => $ result ;
128
- } elseif (is_array ($ result )) {
129
- yield from $ this ->yieldDefinitionsRecursively ($ result , $ fqn );
149
+ if (true === $ member ) {
150
+ yield from $ this ->doGetDescendantDefinitionsForFqn ($ fqn , $ parts , $ this ->memberDefinitions );
151
+ } elseif (false === $ member ) {
152
+ yield from $ this ->doGetDescendantDefinitionsForFqn ($ fqn , $ parts , $ this ->nonMemberDefinitions );
153
+ } else {
154
+ yield from $ this ->doGetDescendantDefinitionsForFqn ($ fqn , $ parts , $ this ->memberDefinitions );
155
+ yield from $ this ->doGetDescendantDefinitionsForFqn ($ fqn , $ parts , $ this ->nonMemberDefinitions );
130
156
}
131
157
}
132
158
@@ -140,8 +166,13 @@ public function getDescendantDefinitionsForFqn(string $fqn): \Generator
140
166
public function getDefinition (string $ fqn , bool $ globalFallback = false )
141
167
{
142
168
$ parts = $ this ->splitFqn ($ fqn );
143
- $ result = $ this ->getIndexValue ($ parts , $ this ->definitions );
144
169
170
+ $ result = $ this ->getIndexValue ($ parts , $ this ->memberDefinitions );
171
+ if ($ result instanceof Definition) {
172
+ return $ result ;
173
+ }
174
+
175
+ $ result = $ this ->getIndexValue ($ parts , $ this ->nonMemberDefinitions );
145
176
if ($ result instanceof Definition) {
146
177
return $ result ;
147
178
}
@@ -164,7 +195,12 @@ public function getDefinition(string $fqn, bool $globalFallback = false)
164
195
public function setDefinition (string $ fqn , Definition $ definition )
165
196
{
166
197
$ parts = $ this ->splitFqn ($ fqn );
167
- $ this ->indexDefinition (0 , $ parts , $ this ->definitions , $ definition );
198
+
199
+ if ($ definition ->isMember ) {
200
+ $ this ->indexDefinition (0 , $ parts , $ this ->memberDefinitions , $ definition );
201
+ } else {
202
+ $ this ->indexDefinition (0 , $ parts , $ this ->nonMemberDefinitions , $ definition );
203
+ }
168
204
169
205
$ this ->emit ('definition-added ' );
170
206
}
@@ -179,7 +215,8 @@ public function setDefinition(string $fqn, Definition $definition)
179
215
public function removeDefinition (string $ fqn )
180
216
{
181
217
$ parts = $ this ->splitFqn ($ fqn );
182
- $ this ->removeIndexedDefinition (0 , $ parts , $ this ->definitions , $ this ->definitions );
218
+ $ this ->removeIndexedDefinition (0 , $ parts , $ this ->memberDefinitions , $ this ->memberDefinitions );
219
+ $ this ->removeIndexedDefinition (0 , $ parts , $ this ->nonMemberDefinitions , $ this ->nonMemberDefinitions );
183
220
184
221
unset($ this ->references [$ fqn ]);
185
222
}
@@ -279,6 +316,26 @@ public function serialize()
279
316
]);
280
317
}
281
318
319
+ /**
320
+ * Returns a Generator that yields all the descendant Definitions of a given FQN
321
+ * in the given definition index.
322
+ *
323
+ * @param string $fqn
324
+ * @param string[] $parts The splitted FQN
325
+ * @param array &$storage The definitions index to look into
326
+ * @return \Generator yields Definition
327
+ */
328
+ private function doGetDescendantDefinitionsForFqn (string $ fqn , array $ parts , array &$ storage ): \Generator
329
+ {
330
+ $ result = $ this ->getIndexValue ($ parts , $ storage );
331
+
332
+ if ($ result instanceof Definition) {
333
+ yield $ fqn => $ result ;
334
+ } elseif (is_array ($ result )) {
335
+ yield from $ this ->yieldDefinitionsRecursively ($ result , $ fqn );
336
+ }
337
+ }
338
+
282
339
/**
283
340
* Returns a Generator that yields all the Definitions in the given $storage recursively.
284
341
* The generator yields key => value pairs, e.g.
@@ -431,7 +488,7 @@ private function removeIndexedDefinition(int $level, array $parts, array &$stora
431
488
$ this ->removeIndexedDefinition (0 , array_slice ($ parts , 0 , $ level ), $ rootStorage , $ rootStorage );
432
489
}
433
490
}
434
- } else {
491
+ } elseif ( isset ( $ storage [ $ part ])) {
435
492
$ this ->removeIndexedDefinition ($ level + 1 , $ parts , $ storage [$ part ], $ rootStorage );
436
493
}
437
494
}
0 commit comments