fwk /
Di
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
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-2016, 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-2016 Julien Ballestracci <[email protected]> |
||
| 30 | * @license http://www.opensource.org/licenses/bsd-license.php BSD License |
||
| 31 | * @link http://fwk.io/di |
||
| 32 | */ |
||
| 33 | namespace Fwk\Di\Definitions; |
||
| 34 | |||
| 35 | use Fwk\Di\Container; |
||
| 36 | use Fwk\Di\InvokableInterface; |
||
| 37 | use Fwk\Di\Exceptions; |
||
| 38 | use Fwk\Di\Reference; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Abstract Definition Utility |
||
| 42 | * |
||
| 43 | * @category Utilities |
||
| 44 | * @package Fwk\Di |
||
| 45 | * @author Julien Ballestracci <[email protected]> |
||
| 46 | * @license http://www.opensource.org/licenses/bsd-license.php BSD License |
||
| 47 | * @link http://fwk.io/di |
||
| 48 | */ |
||
| 49 | abstract class AbstractArgsDefinition extends AbstractDefinition |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * List of arguments |
||
| 53 | * @var array<mixed> |
||
| 54 | */ |
||
| 55 | protected $arguments = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor |
||
| 59 | * |
||
| 60 | * @param mixed $arg Mixed argument |
||
| 61 | * @param array<mixed> $arguments List of arguments |
||
| 62 | * |
||
| 63 | * @abstract |
||
| 64 | * @return void |
||
|
0 ignored issues
–
show
|
|||
| 65 | */ |
||
| 66 | abstract public function __construct($arg, array $arguments = array()); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Return the list of arguments |
||
| 70 | * |
||
| 71 | * @return array<mixed> |
||
| 72 | */ |
||
| 73 | public function getArguments() |
||
| 74 | { |
||
| 75 | return $this->arguments; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Adds an argument to the Definition. |
||
| 80 | * |
||
| 81 | * For a ClassDefinition these arguments are passed to the constructor. |
||
| 82 | * |
||
| 83 | * @param string|InvokableInterface $argument The Argument |
||
| 84 | * |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | 9 | public function addArgument($argument) |
|
| 88 | { |
||
| 89 | 9 | $this->arguments[] = $argument; |
|
| 90 | |||
| 91 | 9 | return $this; |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Add multiples arguments (merge) |
||
| 96 | * |
||
| 97 | * @param array<mixed> $arguments List of new arguments |
||
| 98 | * |
||
| 99 | * @return self |
||
| 100 | */ |
||
| 101 | public function addArguments(array $arguments) |
||
| 102 | { |
||
| 103 | $this->arguments += $arguments; |
||
| 104 | |||
| 105 | return $this; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns all arguments (computed) |
||
| 110 | * |
||
| 111 | * @param Container $container The Di Container |
||
| 112 | * @param null|string $definition Name of the current definition (if any) |
||
| 113 | * |
||
| 114 | * @return array<mixed> |
||
| 115 | */ |
||
| 116 | 15 | protected function getConstructorArguments(Container $container, |
|
| 117 | $definition = null |
||
| 118 | ) { |
||
| 119 | 15 | if (!count($this->arguments)) { |
|
| 120 | 8 | return array($container); |
|
| 121 | } |
||
| 122 | |||
| 123 | 9 | return $this->propertizeArguments( |
|
| 124 | 9 | $this->arguments, |
|
| 125 | $container, |
||
| 126 | $definition |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Transform arguments to their real value if they are instance of InvokableInterface |
||
| 132 | * or a @reference. |
||
| 133 | * |
||
| 134 | * @param array<mixed> $args List of arguments |
||
| 135 | * @param Container $container The Di Container |
||
| 136 | * @param null|string $definition Name of the current definition (if any) |
||
| 137 | * |
||
| 138 | * @return array<mixed> |
||
| 139 | * @throws Exceptions\InvalidArgumentException |
||
| 140 | */ |
||
| 141 | 12 | protected function propertizeArguments(array $args, Container $container, |
|
| 142 | $definition = null |
||
| 143 | ) { |
||
| 144 | 12 | $return = array(); |
|
| 145 | 12 | foreach ($args as $idx => $arg) { |
|
| 146 | 12 | $arg = $this->transformValueType($arg); |
|
| 147 | 12 | if (is_string($arg)) { |
|
| 148 | 9 | $arg = $container->propertizeString($arg); |
|
| 149 | } |
||
| 150 | |||
| 151 | 12 | if (is_string($arg) && strpos($arg, '@', 0) === 0) { |
|
| 152 | 3 | $arg = new Reference(substr($arg, 1)); |
|
| 153 | 11 | } elseif (is_array($arg)) { |
|
| 154 | $arg = $this->propertizeArguments($arg, $container, $definition); |
||
| 155 | } |
||
| 156 | |||
| 157 | try { |
||
| 158 | 12 | $return[$idx] = (($arg instanceof InvokableInterface) |
|
| 159 | 6 | ? $arg->invoke($container, $definition) |
|
| 160 | 10 | : $arg |
|
| 161 | ); |
||
| 162 | 3 | } catch(\Fwk\Di\Exception $exp) { |
|
| 163 | 12 | throw new Exceptions\InvalidArgumentException($idx, $definition, $exp); |
|
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | 10 | return $return; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Transforms a string to a type, if known: |
||
| 172 | * |
||
| 173 | * - boolean: true / false |
||
| 174 | * - null: null |
||
| 175 | * |
||
| 176 | * @param string $value The initial string value |
||
| 177 | * |
||
| 178 | * @return mixed |
||
|
0 ignored issues
–
show
|
|||
| 179 | */ |
||
| 180 | 12 | protected function transformValueType($value) |
|
| 181 | { |
||
| 182 | 12 | if (!is_string($value)) { |
|
| 183 | 5 | return $value; |
|
| 184 | } |
||
| 185 | |||
| 186 | 9 | $value = trim($value); |
|
| 187 | 9 | if (strtolower($value) === "true") { |
|
| 188 | $value = true; |
||
| 189 | 9 | } elseif (strtolower($value) === "false") { |
|
| 190 | $value = false; |
||
| 191 | 9 | } elseif (strtolower($value) === "null") { |
|
| 192 | $value = null; |
||
| 193 | } |
||
| 194 | |||
| 195 | 9 | return $value; |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Factory method |
||
| 200 | * |
||
| 201 | * @param mixed $arg Mixed argument |
||
| 202 | * @param array<mixed> $arguments List of arguments |
||
| 203 | * |
||
| 204 | * @return self |
||
| 205 | * @static |
||
| 206 | */ |
||
| 207 | 32 | public static function factory($arg, array $arguments = array()) |
|
| 208 | { |
||
| 209 | 32 | return new static($arg, $arguments); |
|
| 210 | } |
||
| 211 | } |
||
| 212 |
Adding a
@returnannotation 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.