1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/di package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Di; |
11
|
|
|
|
12
|
|
|
use Interop\Container\ContainerInterface as InteropContainer; |
13
|
|
|
use Slick\Di\Exception\InvalidDefinitionsPathException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Container Builder |
17
|
|
|
* |
18
|
|
|
* @package Slick\Di |
19
|
|
|
* @author Filipe Silva <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class ContainerBuilder implements ContainerAwareInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ContainerInterface |
26
|
|
|
*/ |
27
|
|
|
private $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array|string |
31
|
|
|
*/ |
32
|
|
|
private $definitions; |
33
|
|
|
|
34
|
|
|
public function __construct($definitions = []) |
35
|
|
|
{ |
36
|
|
|
$this->definitions = $definitions; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get current container |
41
|
|
|
* |
42
|
|
|
* If no container was created a new, empty container will be created. |
43
|
|
|
* |
44
|
|
|
* @return ContainerInterface|Container |
45
|
|
|
*/ |
46
|
|
|
public function getContainer() |
47
|
|
|
{ |
48
|
|
|
if (!$this->container) { |
49
|
|
|
$this->setContainer(new Container()); |
50
|
|
|
} |
51
|
|
|
$this->hydrateContainer($this->definitions); |
52
|
|
|
return $this->container; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the dependency container |
58
|
8 |
|
* |
59
|
|
|
* @param InteropContainer $container |
60
|
8 |
|
* |
61
|
8 |
|
* @return ContainerBuilder |
62
|
8 |
|
*/ |
63
|
|
|
public function setContainer(InteropContainer $container) |
64
|
|
|
{ |
65
|
|
|
$this->container = $container; |
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
/** |
70
|
|
|
* Hydrates the container with provided definitions |
71
|
6 |
|
* |
72
|
6 |
|
* @param string|array $definitions |
73
|
6 |
|
*/ |
74
|
6 |
|
protected function hydrateContainer($definitions) |
75
|
6 |
|
{ |
76
|
|
|
if (! is_array($definitions)) { |
77
|
|
|
$this->hydrateFromFile($definitions); |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
foreach ($definitions as $name => $definition) { |
82
|
|
|
$this->container->register($name, $definition); |
83
|
6 |
|
} |
84
|
|
|
} |
85
|
6 |
|
|
86
|
6 |
|
/** |
87
|
6 |
|
* Hydrate the container with definitions from provided file |
88
|
6 |
|
* |
89
|
6 |
|
* @param $definitions |
90
|
|
|
*/ |
91
|
6 |
|
protected function hydrateFromFile($definitions) |
92
|
2 |
|
{ |
93
|
2 |
|
if (! is_file($definitions)) { |
94
|
6 |
|
$this->hydrateFromDirectory($definitions); |
95
|
6 |
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$services = require $definitions; |
99
|
|
|
$this->hydrateContainer($services); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function hydrateFromDirectory($definitions) |
103
|
8 |
|
{ |
104
|
|
|
try { |
105
|
8 |
|
$directory = new \RecursiveDirectoryIterator($definitions); |
106
|
8 |
|
} catch (\Exception $caught) { |
107
|
|
|
throw new InvalidDefinitionsPathException( |
108
|
|
|
'Provided definitions path is not valid or is not found. ' . |
109
|
8 |
|
'Could not create container. Please check '.$definitions |
110
|
8 |
|
); |
111
|
8 |
|
} |
112
|
|
|
|
113
|
|
|
$iterator = new \RecursiveIteratorIterator($directory); |
114
|
2 |
|
$phpFiles = new \RegexIterator($iterator, '/.*\.php$/i'); |
115
|
|
|
foreach ($phpFiles as $phpFile) { |
116
|
|
|
$this->hydrateFromFile($phpFile); |
117
|
2 |
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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 given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.