|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hgraca\MicroDI\Adapter\Pimple; |
|
3
|
|
|
|
|
4
|
|
|
use Hgraca\MicroDI\Adapter\Exception\NotAnObjectException; |
|
5
|
|
|
use Hgraca\MicroDI\BuilderInterface; |
|
6
|
|
|
use Hgraca\MicroDI\Port\ContainerInterface; |
|
7
|
|
|
use Pimple\Container; |
|
8
|
|
|
|
|
9
|
|
|
final class PimpleAdapter implements ContainerInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var Container */ |
|
12
|
|
|
private $container; |
|
13
|
|
|
|
|
14
|
|
|
/** @var BuilderInterface */ |
|
15
|
|
|
private $builder; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Container $container, BuilderInterface $builder) |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
$this->container = $container; |
|
21
|
|
|
$this->builder = $builder; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function hasInstance(string $class): bool |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->hasKey($class); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function hasArgument(string $parameter): bool |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->hasKey($parameter); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function hasFactoryContext(string $factoryClass): bool |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->hasKey($this->generateFactoryContextKey($factoryClass)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getInstance(string $class) |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->getKey($class); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getArgument(string $parameter) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->getKey($parameter); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getFactoryContext(string $factoryClass): array |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->getKey($this->generateFactoryContextKey($factoryClass)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function addInstance($instance) |
|
61
|
|
|
{ |
|
62
|
|
|
if (! is_object($instance)) { |
|
63
|
|
|
throw new NotAnObjectException(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->container[get_class($instance)] = $instance; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function addInstanceLazyLoad(string $class, array $arguments = []) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->container[$class] = function () use ($class, $arguments) { |
|
72
|
|
|
return $this->builder->build($class, $arguments); |
|
73
|
|
|
}; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function hasKey(string $key): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return isset($this->container[$key]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
|
|
private function getKey(string $key) |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->container[$key]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function generateFactoryContextKey(string $factoryClass): string |
|
90
|
|
|
{ |
|
91
|
|
|
return $factoryClass . '.context'; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|