Skip to content

Commit 14645a8

Browse files
authored
Added PriorityDefinition to sort dependencies. (#5646)
1 parent ca59b06 commit 14645a8

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

src/Definition/DefinitionSource.php

+6
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,21 @@ private function normalizeSource(array $source): array
102102
*/
103103
private function normalizeDefinition(string $identifier, $definition): ?DefinitionInterface
104104
{
105+
if ($definition instanceof PriorityDefinition) {
106+
$definition = $definition->getDefinition();
107+
}
108+
105109
if (is_string($definition) && class_exists($definition)) {
106110
if (method_exists($definition, '__invoke')) {
107111
return new FactoryDefinition($identifier, $definition, []);
108112
}
109113
return $this->autowire($identifier, new ObjectDefinition($identifier, $definition));
110114
}
115+
111116
if (is_callable($definition)) {
112117
return new FactoryDefinition($identifier, $definition, []);
113118
}
119+
114120
return null;
115121
}
116122

src/Definition/PriorityDefinition.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Di\Definition;
13+
14+
class PriorityDefinition
15+
{
16+
/**
17+
* @var array<string, int>
18+
*/
19+
protected array $dependencies = [];
20+
21+
public function __construct(string $class, int $priority = 0)
22+
{
23+
$this->dependencies[$class] = $priority;
24+
}
25+
26+
public function getDependencies(): array
27+
{
28+
return $this->dependencies;
29+
}
30+
31+
public function merge(PriorityDefinition $factory): static
32+
{
33+
foreach ($factory->getDependencies() as $class => $priority) {
34+
if (isset($this->dependencies[$class]) && $priority <= $this->dependencies[$class]) {
35+
continue;
36+
}
37+
38+
$this->dependencies[$class] = $priority;
39+
}
40+
41+
return $this;
42+
}
43+
44+
public function getDefinition(): string
45+
{
46+
$dependencies = array_flip($this->dependencies);
47+
ksort($dependencies);
48+
return array_pop($dependencies);
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Di\Definition;
13+
14+
use Hyperf\Di\Definition\PriorityDefinition;
15+
use HyperfTest\Config\Stub\ProviderConfig;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @internal
20+
* @coversNothing
21+
*/
22+
class PriorityDefinitionTest extends TestCase
23+
{
24+
public function testProviderConfigLoad()
25+
{
26+
$result = ProviderConfig::merge(
27+
[
28+
'dependencies' => [
29+
'foo' => 'foo1',
30+
],
31+
],
32+
[
33+
'dependencies' => [
34+
'foo' => 'foo2',
35+
],
36+
]
37+
);
38+
39+
$this->assertSame('foo2', $result['dependencies']['foo']);
40+
41+
$result = ProviderConfig::merge(
42+
[
43+
'dependencies' => [
44+
'foo' => new PriorityDefinition('foo', 1),
45+
],
46+
],
47+
[
48+
'dependencies' => [
49+
'foo' => 'foo2',
50+
],
51+
]
52+
);
53+
54+
$this->assertSame('foo', $result['dependencies']['foo']->getDefinition());
55+
56+
$result = ProviderConfig::merge(
57+
[
58+
'dependencies' => [
59+
'foo' => new PriorityDefinition('foo', 2),
60+
'bar' => 'bar1',
61+
],
62+
],
63+
[
64+
'dependencies' => [
65+
'foo' => new PriorityDefinition('foo3', 1),
66+
'bar' => 'bar2',
67+
],
68+
]
69+
);
70+
71+
$this->assertSame('foo', $result['dependencies']['foo']->getDefinition());
72+
$this->assertSame('bar2', $result['dependencies']['bar']);
73+
}
74+
}

0 commit comments

Comments
 (0)