Skip to content

Commit 6104fa5

Browse files
committed
ServiceContainer:
- reorder methods - discard existing instances hydration for now
1 parent 2c847ff commit 6104fa5

File tree

1 file changed

+70
-91
lines changed

1 file changed

+70
-91
lines changed

src/ServiceContainer.php

Lines changed: 70 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ public function __construct(CacheInterface $cache = null)
4747
$this->singletons = $cache->get('singletons', []);
4848
}
4949

50+
/**
51+
* @throws InvalidArgumentException
52+
*/
53+
public function __destruct()
54+
{
55+
$this->cache->set('aliases', $this->aliases);
56+
$this->cache->set('singletons', $this->singletons);
57+
}
58+
59+
/**
60+
* @param string $alias
61+
* @param string $concrete
62+
*
63+
* @return void
64+
*/
65+
public function addAlias(string $alias, string $concrete): void
66+
{
67+
$this->aliases[$alias] = $concrete;
68+
}
69+
5070
/**
5171
* Finds an entry of the container by its identifier and returns it.
5272
*
@@ -111,88 +131,26 @@ public function has(string $id): bool
111131
/**
112132
* @template T
113133
*
114-
* @param T $classOrObject
134+
* @param T $className
115135
* @param array $data
116136
*
117-
* @return T|T[]
118137
* @throws ContainerException
119138
*
139+
* @return T|T[]
120140
*/
121-
public function hydrate(string|object $classOrObject, array $data = []): mixed
141+
public function hydrate(string $className, array $data = []): mixed
122142
{
123143
try {
124144
if (array_is_list($data)) {
125-
return $this->hydrateArray($classOrObject, $data);
145+
return $this->hydrateArray($className, $data);
126146
}
127147

128-
if (is_string($classOrObject)) {
129-
return $this->hydrateByClassName($classOrObject, $data);
130-
}
131-
132-
return $this->hydrateExistingInstance($classOrObject, $data);
148+
return $this->hydrateByClassName($className, $data);
133149
} catch (Exception $e) {
134150
throw new ContainerException('Could not hydrate object', $e->getCode(), $e);
135151
}
136152
}
137153

138-
/**
139-
* @param ReflectionMethod|ReflectionFunction $reflectionMethod
140-
* @param array $arguments
141-
*
142-
* @throws NotFoundExceptionInterface
143-
* @throws ReflectionException
144-
* @throws ContainerExceptionInterface
145-
*
146-
* @return array
147-
*/
148-
private function createArguments(
149-
ReflectionMethod|ReflectionFunction $reflectionMethod,
150-
array $arguments = []
151-
): array {
152-
foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
153-
$argumentName = $reflectionParameter->getName();
154-
155-
if (isset($arguments[$argumentName])) {
156-
continue;
157-
}
158-
159-
if ($reflectionParameter->hasType()) {
160-
$reflectionType = $reflectionParameter->getType();
161-
$typeAlias = null;
162-
163-
$injectAttrs = $reflectionParameter->getAttributes(Inject::class);
164-
if (!empty($injectAttrs)) {
165-
$injectAttrs[0]->newInstance()->index;
166-
}
167-
168-
if ($typeAlias || !$reflectionType->isBuiltin()) {
169-
$arguments[$argumentName] = $this->get($typeAlias ?? $reflectionType->getName());
170-
continue;
171-
}
172-
173-
if (
174-
!$reflectionParameter->isDefaultValueAvailable()
175-
&& !$reflectionParameter->allowsNull()
176-
) {
177-
throw new Exception("Could not inject parameter: $argumentName");
178-
}
179-
180-
$arguments[$argumentName] = $reflectionParameter->getDefaultValue();
181-
}
182-
}
183-
184-
return $arguments;
185-
}
186-
187-
/**
188-
* @throws InvalidArgumentException
189-
*/
190-
public function __destruct()
191-
{
192-
$this->cache->set('aliases', $this->aliases);
193-
$this->cache->set('singletons', $this->singletons);
194-
}
195-
196154
/**
197155
* @param callable $function
198156
* @param array $arguments
@@ -239,14 +197,53 @@ public function loadDefinitionsFromDirectory(string $directory, string $namespac
239197
}
240198

241199
/**
242-
* @param string $alias
243-
* @param string $concrete
200+
* @param ReflectionMethod|ReflectionFunction $reflectionMethod
201+
* @param array $arguments
244202
*
245-
* @return void
203+
* @throws NotFoundExceptionInterface
204+
* @throws ReflectionException
205+
* @throws ContainerExceptionInterface
206+
*
207+
* @return array
246208
*/
247-
public function addAlias(string $alias, string $concrete): void
209+
private function createArguments(
210+
ReflectionMethod|ReflectionFunction $reflectionMethod,
211+
array $arguments = []
212+
): array
248213
{
249-
$this->aliases[$alias] = $concrete;
214+
foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
215+
$argumentName = $reflectionParameter->getName();
216+
217+
if (isset($arguments[$argumentName])) {
218+
continue;
219+
}
220+
221+
if ($reflectionParameter->hasType()) {
222+
$reflectionType = $reflectionParameter->getType();
223+
$typeAlias = null;
224+
225+
$injectAttrs = $reflectionParameter->getAttributes(Inject::class);
226+
if (!empty($injectAttrs)) {
227+
$injectAttrs[0]->newInstance()->index;
228+
}
229+
230+
if ($typeAlias || !$reflectionType->isBuiltin()) {
231+
$arguments[$argumentName] = $this->get($typeAlias ?? $reflectionType->getName());
232+
continue;
233+
}
234+
235+
if (
236+
!$reflectionParameter->isDefaultValueAvailable()
237+
&& !$reflectionParameter->allowsNull()
238+
) {
239+
throw new Exception("Could not inject parameter: $argumentName");
240+
}
241+
242+
$arguments[$argumentName] = $reflectionParameter->getDefaultValue();
243+
}
244+
}
245+
246+
return $arguments;
250247
}
251248

252249
/**
@@ -257,12 +254,8 @@ public function addAlias(string $alias, string $concrete): void
257254
*
258255
* @return T[]
259256
*/
260-
private function hydrateArray(string|object $classOrObject, array $data): mixed
257+
private function hydrateArray(string $classOrObject, array $data): mixed
261258
{
262-
if (is_object($classOrObject)) {
263-
$classOrObject = get_class($classOrObject);
264-
}
265-
266259
$objects = [];
267260

268261
foreach ($data as $item) {
@@ -331,18 +324,4 @@ private function hydrateByClassName(string $className, array $data): mixed
331324

332325
return $this->get($className, $arguments);
333326
}
334-
335-
/**
336-
* @param object $instance
337-
* @param array $data
338-
*
339-
* @throws Exception
340-
*
341-
* @return mixed
342-
*/
343-
private function hydrateExistingInstance(object $instance, array $data): mixed
344-
{
345-
//TODO implementation
346-
throw new Exception('Not implemented');
347-
}
348327
}

0 commit comments

Comments
 (0)