1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/boot project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Boot\Middleware; |
10
|
|
|
|
11
|
|
|
use Auryn\Injector; |
12
|
|
|
use Daikon\Interop\Assertion; |
13
|
|
|
use Daikon\Interop\RuntimeException; |
14
|
|
|
use Psr\Container\ContainerInterface; |
15
|
|
|
|
16
|
|
|
trait ResolvesDependency |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param mixed $dependency |
20
|
|
|
* @return callable|object |
21
|
|
|
*/ |
22
|
|
|
private function resolve(ContainerInterface $container, $dependency, string $stereoType) |
23
|
|
|
{ |
24
|
|
|
if (is_string($dependency)) { |
25
|
|
|
Assertion::classExists($dependency, "Given dependency '$dependency' not found."); |
26
|
|
|
$dependency = $container->get($dependency); |
27
|
|
|
} elseif (is_array($dependency) && count($dependency) === 2) { |
28
|
|
|
$fqcn = $dependency[0]; |
29
|
|
|
$params = $dependency[1]; |
30
|
|
|
Assertion::classExists($fqcn, "Given dependency '$fqcn' not found."); |
31
|
|
|
Assertion::isArray($params, 'Dependency parameters must be an array.'); |
32
|
|
|
$dependency = $container->get(Injector::class)->make($fqcn, $params); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (is_object($dependency)) { |
36
|
|
|
Assertion::isInstanceOf( |
37
|
|
|
$dependency, |
38
|
|
|
$stereoType, |
39
|
|
|
sprintf("Given dependency '%s' is not a '$stereoType'.", get_class($dependency)) |
40
|
|
|
); |
41
|
|
|
return $dependency; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (is_callable($dependency)) { |
45
|
|
|
return $dependency; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
//@better debug input |
49
|
|
|
throw new RuntimeException( |
50
|
|
|
sprintf("Given dependency '%s' is not a '$stereoType'.", gettype($dependency)) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|