Repository navigation
Expand file tree
/
Copy pathContainer.php
More file actions
161 lines (137 loc) · 4.29 KB
/
Copy pathContainer.php
File metadata and controls
161 lines (137 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Di;
use Hyperf\Contract\ContainerInterface as HyperfContainerInterface;
use Hyperf\Di\Definition\DefinitionInterface;
use Hyperf\Di\Definition\ObjectDefinition;
use Hyperf\Di\Exception\InvalidArgumentException;
use Hyperf\Di\Exception\NotFoundException;
use Hyperf\Di\Resolver\ResolverDispatcher;
use Psr\Container\ContainerInterface as PsrContainerInterface;
class Container implements HyperfContainerInterface
{
/**
* Map of entries that are already resolved.
*/
protected array $resolvedEntries;
/**
* Map of definitions that are already fetched (local cache).
*/
protected array $fetchedDefinitions = [];
protected Resolver\ResolverInterface $definitionResolver;
/**
* Container constructor.
*/
public function __construct(protected Definition\DefinitionSourceInterface $definitionSource)
{
$this->definitionResolver = new ResolverDispatcher($this);
// Auto-register the container.
$this->resolvedEntries = [
self::class => $this,
PsrContainerInterface::class => $this,
HyperfContainerInterface::class => $this,
];
}
/**
* @internal
*/
public function make(string $name, array $parameters = [])
{
$definition = $this->getDefinition($name);
if (! $definition) {
throw new NotFoundException("No entry or class found for '{$name}'");
}
return $this->resolveDefinition($definition, $parameters);
}
/**
* @internal
* @param mixed $entry
*/
public function set(string $name, $entry): void
{
$this->resolvedEntries[$name] = $entry;
}
/**
* @internal
*/
public function unbind(string $name): void
{
if ($this->has($name)) {
unset($this->resolvedEntries[$name]);
}
}
/**
* @internal
* @param mixed $definition
*/
public function define(string $name, $definition): void
{
$this->setDefinition($name, $definition);
}
/**
* @param mixed $id
*/
public function get($id)
{
// If the entry is already resolved we return it
if (isset($this->resolvedEntries[$id]) || array_key_exists($id, $this->resolvedEntries)) {
return $this->resolvedEntries[$id];
}
return $this->resolvedEntries[$id] = $this->make($id);
}
/**
* @param mixed $id
*/
public function has($id): bool
{
if (! is_string($id)) {
throw new InvalidArgumentException(sprintf('The name parameter must be of type string, %s given', is_object($id) ? get_class($id) : gettype($id)));
}
if (array_key_exists($id, $this->resolvedEntries)) {
return true;
}
$definition = $this->getDefinition($id);
if ($definition === null) {
return false;
}
if ($definition instanceof ObjectDefinition) {
return $definition->isInstantiable();
}
return true;
}
/**
* @param array|callable|string $definition
*/
protected function setDefinition(string $name, $definition): void
{
// Clear existing entry if it exists
if (array_key_exists($name, $this->resolvedEntries)) {
unset($this->resolvedEntries[$name]);
}
$this->fetchedDefinitions = []; // Completely clear this local cache
$this->definitionSource->addDefinition($name, $definition);
}
protected function getDefinition(string $name): ?DefinitionInterface
{
// Local cache that avoids fetching the same definition twice
if (! array_key_exists($name, $this->fetchedDefinitions)) {
$this->fetchedDefinitions[$name] = $this->definitionSource->getDefinition($name);
}
return $this->fetchedDefinitions[$name];
}
/**
* Resolves a definition.
*/
protected function resolveDefinition(DefinitionInterface $definition, array $parameters = [])
{
return $this->definitionResolver->resolve($definition, $parameters);
}
}