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\Factory\ConfigFileServiceFactory; |
9
|
|
|
use Gr8abbasi\Container\Repository\InMemoryServiceRepository; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Container Class |
13
|
|
|
*/ |
14
|
|
|
class Container implements ContainerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $services; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ServiceRepositoryInterface |
23
|
|
|
*/ |
24
|
|
|
private $repository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ServiceFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $factory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor for Container |
33
|
|
|
* |
34
|
|
|
* @param array $services |
35
|
|
|
*/ |
36
|
5 |
|
public function __construct( |
37
|
|
|
array $services = [], |
38
|
|
|
ServiceRepositoryInterface $repository = null, |
39
|
|
|
ServiceFactoryInterface $factory = null |
40
|
|
|
) { |
41
|
5 |
|
$this->services = $services; |
42
|
5 |
|
$this->repository = $repository ?: new InMemoryServiceRepository(); |
|
|
|
|
43
|
5 |
|
$this->factory = $factory ?: new ConfigFileServiceFactory(); |
|
|
|
|
44
|
5 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
4 |
|
public function get($name) |
50
|
|
|
{ |
51
|
4 |
|
if (!$this->has($name)) { |
52
|
1 |
|
throw new NotFoundException('Service not found: ' . $name); |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
if (is_null($this->repository->get($name))) { |
56
|
|
|
// foreach($this->services[$name] as $id => $service){ |
57
|
|
|
|
58
|
3 |
|
if (isset($this->services[$name]['arguments'])) { |
59
|
2 |
|
foreach ($this->services[$name]['arguments'] as $argument) { |
60
|
|
|
// $input = $this->container->get($argument); |
61
|
|
|
/** |
62
|
|
|
* TODO |
63
|
|
|
* Check if dependency already resolved or not |
64
|
|
|
*/ |
65
|
2 |
|
$this->validate($name); |
66
|
1 |
|
$service = $this->factory->create($this->services[$argument]['class']); |
67
|
1 |
|
$this->repository->add($name, $service); |
68
|
1 |
|
} |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
// } |
72
|
2 |
|
$this->validate($name); |
73
|
1 |
|
$service = $this->factory->create($this->services[$name]['class']); |
74
|
1 |
|
$this->repository->add($name, $service); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
return $this->repository->get($name); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
4 |
|
public function has($name) |
84
|
|
|
{ |
85
|
4 |
|
return isset($this->services[$name]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Validate requested service before |
90
|
|
|
* attempt to resolve |
91
|
|
|
* |
92
|
|
|
* @param string $name |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
3 |
|
private function validate($name) |
97
|
|
|
{ |
98
|
3 |
|
if (!isset($this->services[$name]) || empty($this->services[$name])) { |
99
|
1 |
|
throw new ContainerException( |
100
|
|
|
'Service should be an array with key value pair: ' . $name |
101
|
1 |
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
if (!class_exists($this->services[$name]['class'])) { |
105
|
1 |
|
throw new ContainerException( |
106
|
1 |
|
'Class does not exists: ' . $this->services[$name]['class'] |
107
|
1 |
|
); |
108
|
|
|
} |
109
|
1 |
|
} |
110
|
|
|
} |
111
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.