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 Slick\Common\BaseMethods; |
13
|
|
|
use Slick\Di\Definition\Alias; |
14
|
|
|
use Slick\Di\Exception\InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ContainerBuilder for dependency container creation |
18
|
|
|
* |
19
|
|
|
* @package Slick\Di |
20
|
|
|
* @author Filipe Silva <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @method array getDefinitions() Returns the source definitions |
23
|
|
|
*/ |
24
|
|
|
final class ContainerBuilder |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* For easy getters and setters |
29
|
|
|
*/ |
30
|
|
|
use BaseMethods; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @read |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $definitions = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @read |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $override = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @read |
46
|
|
|
* @var Container |
47
|
|
|
*/ |
48
|
|
|
protected $container; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Creates a container builder with provided definitions and override mode |
52
|
|
|
* |
53
|
|
|
* @param array|string $definitions |
54
|
|
|
* The definitions list for container creation |
55
|
|
|
* @param bool $override |
56
|
|
|
* Set to true to override existing definitions |
57
|
|
|
*/ |
58
|
8 |
|
public function __construct($definitions, $override = false) |
59
|
|
|
{ |
60
|
8 |
|
$this->definitions = $this->checkDefinitions($definitions); |
61
|
8 |
|
$this->override = $override; |
62
|
8 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates a container with existing definitions |
66
|
|
|
* |
67
|
|
|
* @return Container |
68
|
|
|
*/ |
69
|
6 |
|
public function getContainer() |
70
|
|
|
{ |
71
|
6 |
|
if (is_null($this->container)) { |
72
|
6 |
|
$this->container = new Container(); |
73
|
6 |
|
$this->applyDefinitions(); |
74
|
6 |
|
} |
75
|
6 |
|
return $this->container; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Apply definitions to content |
80
|
|
|
*/ |
81
|
6 |
|
private function applyDefinitions() |
82
|
|
|
{ |
83
|
6 |
|
foreach($this->definitions as $name => $entry) { |
84
|
|
|
if ( |
85
|
6 |
|
is_string($entry) && |
86
|
6 |
|
preg_match('/^@(?P<key>.*)$/i', $entry, $result) |
87
|
6 |
|
) { |
88
|
6 |
|
$entry = new Alias(['target' => $result['key']]); |
89
|
6 |
|
} |
90
|
|
|
|
91
|
6 |
|
if (!$this->container->has($name) || $this->override) { |
92
|
2 |
|
$this->container->register($name, $entry); |
93
|
2 |
|
} |
94
|
6 |
|
} |
95
|
6 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Checks the data to be returned |
99
|
|
|
* |
100
|
|
|
* @param string|array $definitions |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
8 |
|
private function checkDefinitions($definitions) |
104
|
|
|
{ |
105
|
8 |
|
if (is_array($definitions)) { |
106
|
8 |
|
return $definitions; |
107
|
|
|
} |
108
|
|
|
|
109
|
8 |
|
if (is_string($definitions) && file_exists($definitions)) { |
110
|
8 |
|
$data = include($definitions); |
111
|
8 |
|
return $this->checkDefinitions($data); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
throw new InvalidArgumentException( |
115
|
|
|
"Definitions file not found or invalid. Cannot create ". |
116
|
|
|
"container builder." |
117
|
2 |
|
); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|