1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2021 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade\DI\Services; |
19
|
|
|
|
20
|
|
|
use DivineNii\Invoker\CallableReflection; |
21
|
|
|
use Psr\Container\ContainerExceptionInterface; |
22
|
|
|
use Rade\DI\Exceptions\CircularReferenceException; |
23
|
|
|
use Symfony\Contracts\Service\ServiceLocatorTrait; |
24
|
|
|
use Symfony\Contracts\Service\ServiceProviderInterface; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Rade PSR-11 service locator. |
28
|
|
|
* |
29
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class ServiceLocator implements ServiceProviderInterface |
32
|
|
|
{ |
33
|
|
|
use ServiceLocatorTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
8 |
|
public function get($id) |
39
|
|
|
{ |
40
|
8 |
|
if (!isset($this->factories[$id])) { |
41
|
3 |
|
throw $this->createNotFoundException($id); |
42
|
|
|
} |
43
|
|
|
|
44
|
6 |
|
if (isset($this->loading[$id])) { |
45
|
1 |
|
$ids = array_values($this->loading); |
46
|
1 |
|
$ids = \array_slice($this->loading, (int) array_search($id, $ids, true)); |
47
|
1 |
|
$ids[] = $id; |
48
|
|
|
|
49
|
1 |
|
throw $this->createCircularReferenceException($id, $ids); |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
$this->loading[$id] = $id; |
53
|
|
|
|
54
|
|
|
try { |
55
|
6 |
|
$service = $this->factories[$id]; |
56
|
|
|
|
57
|
6 |
|
return \is_callable($service) ? $service() : $service; |
58
|
|
|
} finally { |
59
|
6 |
|
unset($this->loading[$id]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
1 |
|
public function getProvidedServices(): array |
67
|
|
|
{ |
68
|
1 |
|
if (null === $this->providedTypes) { |
69
|
1 |
|
$this->providedTypes = []; |
70
|
|
|
|
71
|
1 |
|
foreach ($this->factories as $name => $factory) { |
72
|
1 |
|
if (\is_callable($factory)) { |
73
|
1 |
|
$type = CallableReflection::create($factory)->getReturnType(); |
74
|
|
|
|
75
|
1 |
|
$this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '') . ($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?'; |
76
|
1 |
|
} elseif (\is_object($factory) && !$factory instanceof \stdClass) { |
77
|
1 |
|
$this->providedTypes[$name] = \get_class($factory); |
78
|
|
|
} else { |
79
|
1 |
|
$this->providedTypes[$name] = '?'; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $this->providedTypes; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface |
88
|
|
|
{ |
89
|
1 |
|
return new CircularReferenceException($id, $path); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: