These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 |
||
0 ignored issues
–
show
|
|||
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 | public function addArgument($argument) |
||
83 | { |
||
84 | $this->arguments[] = $argument; |
||
85 | |||
86 | 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 | protected function getConstructorArguments(Container $container, |
||
112 | $definition = null |
||
113 | ) { |
||
114 | return $this->propertizeArguments( |
||
115 | $this->arguments, |
||
116 | $container, |
||
117 | $definition |
||
118 | ); |
||
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 | protected function propertizeArguments(array $args, Container $container, |
||
133 | $definition = null |
||
134 | ) { |
||
135 | $return = array(); |
||
136 | foreach ($args as $idx => $arg) { |
||
137 | $arg = $this->transformValueType($arg); |
||
138 | if (is_string($arg)) { |
||
139 | $arg = $container->propertizeString($arg); |
||
140 | } |
||
141 | |||
142 | if (is_string($arg) && strpos($arg, '@', 0) === 0) { |
||
143 | $arg = new Reference(substr($arg, 1)); |
||
144 | } elseif (is_array($arg)) { |
||
145 | $arg = $this->propertizeArguments($arg, $container, $definition); |
||
146 | } |
||
147 | |||
148 | try { |
||
149 | $return[$idx] = (($arg instanceof Invokable) |
||
150 | ? $arg->invoke($container, $definition) |
||
151 | : $arg |
||
152 | ); |
||
153 | } catch(\Fwk\Di\Exception $exp) { |
||
154 | throw new Exceptions\InvalidArgument($idx, $definition, $exp); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | 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 |
||
0 ignored issues
–
show
|
|||
170 | */ |
||
171 | protected function transformValueType($value) |
||
172 | { |
||
173 | if (!is_string($value)) { |
||
174 | return $value; |
||
175 | } |
||
176 | |||
177 | $value = trim($value); |
||
178 | if (strtolower($value) === "true") { |
||
179 | $value = true; |
||
180 | } elseif (strtolower($value) === "false") { |
||
181 | $value = false; |
||
182 | } elseif (strtolower($value) === "null") { |
||
183 | $value = null; |
||
184 | } |
||
185 | |||
186 | 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 | public static function factory($arg, array $arguments = array()) |
||
199 | { |
||
200 | 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.