Skip to content

Commit 58e508d

Browse files
committed
InjectExtension::getInjectMethods() correct order of methods in traits
1 parent 348a6fa commit 58e508d

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

src/DI/Extensions/InjectExtension.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,18 @@ private function updateDefinition($def)
6565
*/
6666
public static function getInjectMethods($class)
6767
{
68-
return array_reverse(array_values(array_filter(get_class_methods($class), function ($name) {
69-
return substr($name, 0, 6) === 'inject';
70-
})));
68+
$res = [];
69+
foreach (get_class_methods($class) as $name) {
70+
if (substr($name, 0, 6) === 'inject') {
71+
$res[$name] = (new \ReflectionMethod($class, $name))->getDeclaringClass()->getName();
72+
}
73+
}
74+
uksort($res, function ($a, $b) use ($res) {
75+
return $res[$a] === $res[$b]
76+
? strcmp($a, $b)
77+
: (is_a($res[$a], $res[$b], TRUE) ? 1 : -1);
78+
});
79+
return array_keys($res);
7180
}
7281

7382

tests/DI/Container.inject.methods.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ $container = createContainer($builder);
5757

5858
$test = new Test2;
5959
$container->callInjects($test);
60-
Assert::same(['Test1::injectOptional', 'Test1::injectA', 'Test1::inject', 'Test2::injectC'], $test->injects);
60+
Assert::same(['Test1::inject', 'Test1::injectA', 'Test1::injectOptional', 'Test2::injectC'], $test->injects);

tests/DI/InjectExtension.basic.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,28 @@ services:
7373
$builder = $compiler->getContainerBuilder();
7474

7575
Assert::equal([
76-
new Statement(['@last.one', 'injectB']),
7776
new Statement(['@last.one', 'injectA']),
78-
new Statement(['@last.one', 'injectD']),
77+
new Statement(['@last.one', 'injectB']),
7978
new Statement(['@last.one', 'injectC']),
79+
new Statement(['@last.one', 'injectD']),
8080
new Statement(['@last.one', '$c'], ['@1_stdClass']),
8181
new Statement(['@last.one', '$a'], ['@1_stdClass']),
8282
], $builder->getDefinition('last.one')->getSetup());
8383

8484
Assert::equal([
85-
new Statement(['@ext.one', 'injectB']),
8685
new Statement(['@ext.one', 'injectA']),
87-
new Statement(['@ext.one', 'injectD']),
86+
new Statement(['@ext.one', 'injectB']),
8887
new Statement(['@ext.one', 'injectC']),
88+
new Statement(['@ext.one', 'injectD']),
8989
new Statement(['@ext.one', '$c'], ['@1_stdClass']),
9090
new Statement(['@ext.one', '$a'], ['@1_stdClass']),
9191
], $builder->getDefinition('ext.one')->getSetup());
9292

9393
Assert::equal([
94-
new Statement(['@two', 'injectB'], [1]),
9594
new Statement(['@two', 'injectA']),
96-
new Statement(['@two', 'injectD']),
95+
new Statement(['@two', 'injectB'], [1]),
9796
new Statement(['@two', 'injectC']),
97+
new Statement(['@two', 'injectD']),
9898
new Statement(['@two', '$c'], ['@1_stdClass']),
9999
new Statement(['@two', '$a'], ['@1_stdClass']),
100100
], $builder->getDefinition('two')->getSetup());

tests/DI/InjectExtension.getInjectMethods().phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ Assert::same([
4949
], InjectExtension::getInjectMethods('Class1'));
5050

5151
Assert::same([
52-
'injectT1',
5352
'inject1',
5453
'inject2',
54+
'injectT1',
5555
], InjectExtension::getInjectMethods('Class2'));
5656

5757
Assert::same([
58-
'injectT2',
59-
'injectT1',
6058
'inject1',
6159
'inject2',
60+
'injectT1',
6261
'inject3',
62+
'injectT2',
6363
], InjectExtension::getInjectMethods('Class3'));

0 commit comments

Comments
 (0)