1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gr8abbasi\Container; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Gr8abbasi\Container\Exception\NotFoundException; |
7
|
|
|
use Gr8abbasi\Container\Exception\ContainerException; |
8
|
|
|
use Gr8abbasi\Container\Repository\InMemoryServiceRepository; |
9
|
|
|
use Gr8abbasi\Container\Factory\ConfigFileServiceFactory; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Container Class |
13
|
|
|
*/ |
14
|
|
|
class Container implements ContainerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $services; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $serviceStore; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ServiceRepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $repository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ServiceFactoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $factory; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor for Container |
38
|
|
|
* |
39
|
|
|
* @param array $services |
40
|
|
|
*/ |
41
|
5 |
|
public function __construct( |
42
|
|
|
array $services = [], |
43
|
|
|
ServiceRepositoryInterface $repository = null, |
44
|
|
|
ServiceFactoryInterface $factory = null |
|
|
|
|
45
|
|
|
) { |
46
|
5 |
|
$this->services = $services; |
47
|
5 |
|
$this->serviceStore = []; |
48
|
5 |
|
$this->repository = $repository ?: new InMemoryServiceRepository(); |
|
|
|
|
49
|
|
|
// $this->factory = $factory ?: new ConfigFileServiceFactory(); |
50
|
5 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
4 |
|
public function get($name) |
56
|
|
|
{ |
57
|
4 |
|
if (!$this->has($name)) { |
58
|
1 |
|
throw new NotFoundException('Service not found: ' . $name); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
if (!isset($this->serviceStore[$name])) { |
62
|
3 |
|
$this->serviceStore[$name] = $this->createService($name); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
return $this->serviceStore[$name]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
4 |
|
public function has($name) |
72
|
|
|
{ |
73
|
4 |
|
return isset($this->services[$name]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create service instance |
78
|
|
|
* |
79
|
|
|
* @param string $name |
80
|
|
|
* |
81
|
|
|
* @return mixed created service |
82
|
|
|
*/ |
83
|
3 |
|
private function createService($name) |
84
|
|
|
{ |
85
|
3 |
|
if (!isset($this->services[$name]) || empty($this->services[$name])) { |
86
|
1 |
|
throw new ContainerException( |
87
|
|
|
'Service should be an array with key value pair: ' . $name |
88
|
1 |
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
if (!class_exists($this->services[$name]['class'])) { |
92
|
1 |
|
throw new ContainerException( |
93
|
1 |
|
'Class does not exists: ' . $this->services[$name]['class'] |
94
|
1 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
$service = new \ReflectionClass($this->services[$name]['class']); |
98
|
1 |
|
return $service->newInstance(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.