|
5 | 5 | namespace MaplePHP\Container; |
6 | 6 |
|
7 | 7 | use Closure; |
8 | | -use MaplePHP\Container\Interfaces\ContainerInterface; |
| 8 | +use MaplePHP\Container\Interfaces\AutowireInterface; |
| 9 | +use Psr\Container\ContainerInterface; |
9 | 10 | use MaplePHP\Container\Interfaces\FactoryInterface; |
10 | 11 | use MaplePHP\DTO\Format\Arr; |
11 | 12 | //use MaplePHP\Container\Reflection; |
@@ -34,10 +35,10 @@ public function __call($method, $args) |
34 | 35 | * TestClasses\Test::class, |
35 | 36 | * TestClasses\Test::class."::__construct", |
36 | 37 | * TestClasses\Test::class."::getStaticMethod", |
37 | | - * @param array|null $args Pass argumnets to constructor staticMethod if you choose. |
| 38 | + * @param array|null $args Pass arguments to constructor staticMethod if you choose. |
38 | 39 | * @param bool $overwrite Will throw exception if already been defined if not arg is set to TRUE. |
39 | 40 | */ |
40 | | - public function set(string $identifier, $value, ?array $args = null, bool $overwrite = false): ContainerInterface |
| 41 | + public function set(string $identifier, mixed $value, ?array $args = null, bool $overwrite = false): ContainerInterface |
41 | 42 | { |
42 | 43 | if (!$overwrite && $this->has($identifier)) { |
43 | 44 | $type = ($this->isFactory($identifier)) ? "factory" : "container"; |
@@ -112,27 +113,26 @@ public function isContainer(string $identifier): bool |
112 | 113 |
|
113 | 114 | /** |
114 | 115 | * Get a container or factory |
115 | | - * @param string $identifier [description] |
116 | | - * @param array $args Is possible to overwrite/add __construct or method argumnets |
117 | | - * @return mixed |
| 116 | + * @template T of object |
| 117 | + * @param class-string<T>|string $identifier The class or service identifier |
| 118 | + * @param string $identifier |
| 119 | + * @param array $args Is possible to overwrite/add __construct or method arguments |
| 120 | + * @return T |
118 | 121 | * @throws ReflectionException |
119 | 122 | */ |
120 | 123 | public function get(string $identifier, array $args = []): mixed |
121 | 124 | { |
122 | | - if ($service = $this->getService($identifier)) { |
| 125 | + $service = $this->getService($identifier); |
| 126 | + if (isset($service)) { |
123 | 127 | if (count($args) === 0) { |
124 | 128 | $args = $this->getArgs($identifier); |
125 | 129 | } |
126 | 130 | if ($this->isFactory($identifier)) { |
127 | 131 | $this->getter[$identifier] = $service(...$args); |
128 | 132 | } else { |
129 | | - if (empty($this->getter[$identifier])) { |
130 | | - if (is_string($service) && class_exists($service)) { |
131 | | - $reflect = new Reflection($service); |
132 | | - if (count($args) > 0) { |
133 | | - $reflect->setArgs($args); |
134 | | - } |
135 | | - $this->getter[$identifier] = $reflect->get(); |
| 133 | + if (!isset($this->getter[$identifier])) { |
| 134 | + if ($service instanceof AutowireInterface) { |
| 135 | + $this->getter[$identifier] = $service->run(); |
136 | 136 | } else { |
137 | 137 | $this->getter[$identifier] = $service; |
138 | 138 | } |
|
0 commit comments