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; |
19
|
|
|
|
20
|
|
|
use Rade\DI\Exceptions\{FrozenServiceException, NotFoundServiceException}; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Dependency injection container. |
24
|
|
|
* |
25
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Container extends AbstractContainer implements \ArrayAccess |
28
|
|
|
{ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
if (!isset($this->types[$cl = static::class])) { |
32
|
|
|
$this->type(self::SERVICE_CONTAINER, \array_keys(\class_implements($c = $this) + \class_parents($c) + [$cl => $cl])); |
33
|
|
|
} |
34
|
|
|
$this->resolver = new Resolver($this->services[self::SERVICE_CONTAINER] = $c ?? $this); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sets a new service to a unique identifier. |
39
|
|
|
* |
40
|
|
|
* @param string $offset The unique identifier for the parameter or object |
41
|
|
|
* @param mixed $value The value of the service assign to the $offset |
42
|
|
|
* |
43
|
|
|
* @throws FrozenServiceException Prevent override of a frozen service |
44
|
|
|
*/ |
45
|
|
|
public function offsetSet($offset, $value): void |
46
|
|
|
{ |
47
|
|
|
$this->autowire($offset, $value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets a registered service definition. |
52
|
|
|
* |
53
|
|
|
* @param string $offset The unique identifier for the service |
54
|
|
|
* |
55
|
|
|
* @throws NotFoundServiceException If the identifier is not defined |
56
|
|
|
* |
57
|
106 |
|
* @return mixed The value of the service |
58
|
|
|
*/ |
59
|
106 |
|
#[\ReturnTypeWillChange] |
60
|
106 |
|
public function offsetGet($offset) |
61
|
106 |
|
{ |
62
|
|
|
return $this->get($offset); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Checks if a service is set. |
67
|
|
|
* |
68
|
|
|
* @param string $offset The unique identifier for the service |
69
|
|
|
*/ |
70
|
|
|
public function offsetExists($offset): bool |
71
|
63 |
|
{ |
72
|
|
|
return $this->has($offset); |
73
|
63 |
|
} |
74
|
63 |
|
|
75
|
|
|
/** |
76
|
|
|
* Unset a service by given offset. |
77
|
|
|
* |
78
|
|
|
* @param string $offset The unique identifier for service definition |
79
|
|
|
*/ |
80
|
|
|
public function offsetUnset($offset): void |
81
|
|
|
{ |
82
|
|
|
$this->removeDefinition($offset); |
83
|
|
|
} |
84
|
|
|
|
85
|
55 |
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
55 |
|
* |
88
|
|
|
* @throws \ReflectionException |
89
|
|
|
*/ |
90
|
|
|
protected function doCreate(string $id, $definition, int $invalidBehavior) |
91
|
|
|
{ |
92
|
|
|
if ($definition instanceof Definitions\DefinitionInterface) { |
93
|
|
|
$service = $definition->build($id, $this->resolver); |
94
|
|
|
|
95
|
7 |
|
if ($definition instanceof Definitions\ShareableDefinitionInterface) { |
96
|
|
|
if (!$definition->isPublic()) { |
97
|
7 |
|
$this->removeDefinition($id); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!$definition->isShared()) { |
101
|
|
|
return $service; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
6 |
|
|
106
|
|
|
return $this->services[$id] = $service ?? $definition; |
107
|
6 |
|
} |
108
|
|
|
} |
109
|
|
|
|