1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Fwk |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2011-2012, Julien Ballestracci <[email protected]>. |
6
|
|
|
* All rights reserved. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
12
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
13
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
14
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
15
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
16
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
17
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
18
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
19
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
20
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
21
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
22
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
23
|
|
|
* |
24
|
|
|
* PHP Version 5.3 |
25
|
|
|
* |
26
|
|
|
* @category DependencyInjection |
27
|
|
|
* @package Fwk\Di |
28
|
|
|
* @author Julien Ballestracci <[email protected]> |
29
|
|
|
* @copyright 2011-2014 Julien Ballestracci <[email protected]> |
30
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
31
|
|
|
* @link http://www.nitronet.org/fwk |
32
|
|
|
*/ |
33
|
|
|
namespace Fwk\Di; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Abstract Definition Utility |
37
|
|
|
* |
38
|
|
|
* @category Utilities |
39
|
|
|
* @package Fwk\Di |
40
|
|
|
* @author Julien Ballestracci <[email protected]> |
41
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
42
|
|
|
* @link http://www.nitronet.org/fwk |
43
|
|
|
*/ |
44
|
|
|
abstract class AbstractDefinition |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* List of arguments |
48
|
|
|
* @var array<mixed> |
49
|
|
|
*/ |
50
|
|
|
protected $arguments = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor |
54
|
|
|
* |
55
|
|
|
* @param mixed $arg Mixed argument |
56
|
|
|
* @param array<mixed> $arguments List of arguments |
57
|
|
|
* |
58
|
|
|
* @abstract |
59
|
|
|
* @return void |
|
|
|
|
60
|
|
|
*/ |
61
|
|
|
abstract public function __construct($arg, array $arguments = array()); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the list of arguments |
65
|
|
|
* |
66
|
|
|
* @return array<mixed> |
67
|
|
|
*/ |
68
|
|
|
public function getArguments() |
69
|
|
|
{ |
70
|
|
|
return $this->arguments; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Adds an argument to the Definition. |
75
|
|
|
* |
76
|
|
|
* For a ClassDefinition these arguments are passed to the constructor. |
77
|
|
|
* |
78
|
|
|
* @param string|Invokable $argument The Argument |
79
|
|
|
* |
80
|
|
|
* @return Definition |
81
|
|
|
*/ |
82
|
9 |
|
public function addArgument($argument) |
83
|
|
|
{ |
84
|
9 |
|
$this->arguments[] = $argument; |
85
|
|
|
|
86
|
9 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add multiples arguments (merge) |
91
|
|
|
* |
92
|
|
|
* @param array<mixed> $arguments List of new arguments |
93
|
|
|
* |
94
|
|
|
* @return Definition |
95
|
|
|
*/ |
96
|
|
|
public function addArguments(array $arguments) |
97
|
|
|
{ |
98
|
|
|
$this->arguments += $arguments; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns all arguments (computed) |
105
|
|
|
* |
106
|
|
|
* @param Container $container The Di Container |
107
|
|
|
* @param null|string $definition Name of the current definition (if any) |
108
|
|
|
* |
109
|
|
|
* @return array<mixed> |
110
|
|
|
*/ |
111
|
10 |
|
protected function getConstructorArguments(Container $container, |
112
|
|
|
$definition = null |
113
|
|
|
) { |
114
|
10 |
|
return $this->propertizeArguments( |
115
|
10 |
|
$this->arguments, |
116
|
10 |
|
$container, |
117
|
|
|
$definition |
118
|
10 |
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Transform arguments to their real value if they are instance of Invokable |
123
|
|
|
* or a @reference. |
124
|
|
|
* |
125
|
|
|
* @param array<mixed> $args List of arguments |
126
|
|
|
* @param Container $container The Di Container |
127
|
|
|
* @param null|string $definition Name of the current definition (if any) |
128
|
|
|
* |
129
|
|
|
* @return array<mixed> |
130
|
|
|
* @throws Exceptions\InvalidArgument |
131
|
|
|
*/ |
132
|
12 |
|
protected function propertizeArguments(array $args, Container $container, |
133
|
|
|
$definition = null |
134
|
|
|
) { |
135
|
12 |
|
$return = array(); |
136
|
12 |
|
foreach ($args as $idx => $arg) { |
137
|
11 |
|
$arg = $this->transformValueType($arg); |
138
|
11 |
|
if (is_string($arg)) { |
139
|
8 |
|
$arg = $container->propertizeString($arg); |
140
|
8 |
|
} |
141
|
|
|
|
142
|
11 |
|
if (is_string($arg) && strpos($arg, '@', 0) === 0) { |
143
|
3 |
|
$arg = new Reference(substr($arg, 1)); |
144
|
11 |
|
} elseif (is_array($arg)) { |
145
|
|
|
$arg = $this->propertizeArguments($arg, $container, $definition); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
try { |
149
|
11 |
|
$return[$idx] = (($arg instanceof Invokable) |
150
|
11 |
|
? $arg->invoke($container, $definition) |
151
|
3 |
|
: $arg |
152
|
7 |
|
); |
153
|
11 |
|
} catch(\Fwk\Di\Exception $exp) { |
154
|
3 |
|
throw new Exceptions\InvalidArgument($idx, $definition, $exp); |
155
|
|
|
} |
156
|
10 |
|
} |
157
|
|
|
|
158
|
10 |
|
return $return; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Transforms a string to a type, if known: |
163
|
|
|
* |
164
|
|
|
* - boolean: true / false |
165
|
|
|
* - null: null |
166
|
|
|
* |
167
|
|
|
* @param string $value The initial string value |
168
|
|
|
* |
169
|
|
|
* @return mixed |
|
|
|
|
170
|
|
|
*/ |
171
|
11 |
|
protected function transformValueType($value) |
172
|
|
|
{ |
173
|
11 |
|
if (!is_string($value)) { |
174
|
4 |
|
return $value; |
175
|
|
|
} |
176
|
|
|
|
177
|
8 |
|
$value = trim($value); |
178
|
8 |
|
if (strtolower($value) === "true") { |
179
|
|
|
$value = true; |
180
|
8 |
|
} elseif (strtolower($value) === "false") { |
181
|
|
|
$value = false; |
182
|
8 |
|
} elseif (strtolower($value) === "null") { |
183
|
|
|
$value = null; |
184
|
|
|
} |
185
|
|
|
|
186
|
8 |
|
return $value; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Factory method |
191
|
|
|
* |
192
|
|
|
* @param mixed $arg Mixed argument |
193
|
|
|
* @param array<mixed> $arguments List of arguments |
194
|
|
|
* |
195
|
|
|
* @return Definition |
196
|
|
|
* @static |
197
|
|
|
*/ |
198
|
2 |
|
public static function factory($arg, array $arguments = array()) |
199
|
|
|
{ |
200
|
2 |
|
return new static($arg, $arguments); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.