| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP-DI |
| 4 |
* |
| 5 |
* @link http://php-di.org/ |
| 6 |
* @copyright Matthieu Napoli (http://mnapoli.fr/) |
| 7 |
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file) |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Cybot\Dependencies\DI\Definition\Helper; |
| 11 |
|
| 12 |
use Cybot\Dependencies\DI\Definition\ObjectDefinition; |
| 13 |
use Cybot\Dependencies\DI\Definition\ObjectDefinition\MethodInjection; |
| 14 |
use Cybot\Dependencies\DI\Definition\ObjectDefinition\PropertyInjection; |
| 15 |
|
| 16 |
/** |
| 17 |
* Helps defining how to create an instance of a class. |
| 18 |
* |
| 19 |
* @author Matthieu Napoli <[email protected]> |
| 20 |
*/ |
| 21 |
class ObjectDefinitionHelper implements DefinitionHelper |
| 22 |
{ |
| 23 |
/** |
| 24 |
* @var string|null |
| 25 |
*/ |
| 26 |
private $className; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var boolean|null |
| 30 |
*/ |
| 31 |
private $lazy; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var string|null |
| 35 |
*/ |
| 36 |
private $scope; |
| 37 |
|
| 38 |
/** |
| 39 |
* Array of constructor parameters. |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
private $constructor = []; |
| 43 |
|
| 44 |
/** |
| 45 |
* Array of properties and their value. |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
private $properties = []; |
| 49 |
|
| 50 |
/** |
| 51 |
* Array of methods and their parameters. |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
private $methods = []; |
| 55 |
|
| 56 |
/** |
| 57 |
* Helper for defining an object. |
| 58 |
* |
| 59 |
* @param string|null $className Class name of the object. |
| 60 |
* If null, the name of the entry (in the container) will be used as class name. |
| 61 |
*/ |
| 62 |
public function __construct($className = null) |
| 63 |
{ |
| 64 |
$this->className = $className; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Define the entry as lazy. |
| 69 |
* |
| 70 |
* A lazy entry is created only when it is used, a proxy is injected instead. |
| 71 |
* |
| 72 |
* @return ObjectDefinitionHelper |
| 73 |
*/ |
| 74 |
public function lazy() |
| 75 |
{ |
| 76 |
$this->lazy = true; |
| 77 |
return $this; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Defines the scope of the entry. |
| 82 |
* |
| 83 |
* @param string $scope |
| 84 |
* |
| 85 |
* @return ObjectDefinitionHelper |
| 86 |
*/ |
| 87 |
public function scope($scope) |
| 88 |
{ |
| 89 |
$this->scope = $scope; |
| 90 |
return $this; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Defines the arguments to use to call the constructor. |
| 95 |
* |
| 96 |
* This method takes a variable number of arguments, example: |
| 97 |
* ->constructor($param1, $param2, $param3) |
| 98 |
* |
| 99 |
* @param mixed ... Parameters to use for calling the constructor of the class. |
| 100 |
* |
| 101 |
* @return ObjectDefinitionHelper |
| 102 |
*/ |
| 103 |
public function constructor() |
| 104 |
{ |
| 105 |
$this->constructor = func_get_args(); |
| 106 |
return $this; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Defines a value for a specific argument of the constructor. |
| 111 |
* |
| 112 |
* This method is usually used together with annotations or autowiring, when a parameter |
| 113 |
* is not (or cannot be) type-hinted. Using this method instead of constructor() allows to |
| 114 |
* avoid defining all the parameters (letting them being resolved using annotations or autowiring) |
| 115 |
* and only define one. |
| 116 |
* |
| 117 |
* @param string $parameter Parameter for which the value will be given. |
| 118 |
* @param mixed $value Value to give to this parameter. |
| 119 |
* |
| 120 |
* @return ObjectDefinitionHelper |
| 121 |
*/ |
| 122 |
public function constructorParameter($parameter, $value) |
| 123 |
{ |
| 124 |
$this->constructor[$parameter] = $value; |
| 125 |
return $this; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Defines a value to inject in a property of the object. |
| 130 |
* |
| 131 |
* @param string $property Entry in which to inject the value. |
| 132 |
* @param mixed $value Value to inject in the property. |
| 133 |
* |
| 134 |
* @return ObjectDefinitionHelper |
| 135 |
*/ |
| 136 |
public function property($property, $value) |
| 137 |
{ |
| 138 |
$this->properties[$property] = $value; |
| 139 |
return $this; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Defines a method to call and the arguments to use. |
| 144 |
* |
| 145 |
* This method takes a variable number of arguments after the method name, example: |
| 146 |
* |
| 147 |
* ->method('myMethod', $param1, $param2) |
| 148 |
* |
| 149 |
* Can be used multiple times to declare multiple calls. |
| 150 |
* |
| 151 |
* @param string $method Name of the method to call. |
| 152 |
* @param mixed ... Parameters to use for calling the method. |
| 153 |
* |
| 154 |
* @return ObjectDefinitionHelper |
| 155 |
*/ |
| 156 |
public function method($method) |
| 157 |
{ |
| 158 |
$args = func_get_args(); |
| 159 |
array_shift($args); |
| 160 |
|
| 161 |
if (! isset($this->methods[$method])) { |
| 162 |
$this->methods[$method] = []; |
| 163 |
} |
| 164 |
|
| 165 |
$this->methods[$method][] = $args; |
| 166 |
|
| 167 |
return $this; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Defines a method to call and a value for a specific argument. |
| 172 |
* |
| 173 |
* This method is usually used together with annotations or autowiring, when a parameter |
| 174 |
* is not (or cannot be) type-hinted. Using this method instead of method() allows to |
| 175 |
* avoid defining all the parameters (letting them being resolved using annotations or |
| 176 |
* autowiring) and only define one. |
| 177 |
* |
| 178 |
* If multiple calls to the method have been configured already (e.g. in a previous definition) |
| 179 |
* then this method only overrides the parameter for the *first* call. |
| 180 |
* |
| 181 |
* @param string $method Name of the method to call. |
| 182 |
* @param string $parameter Name or index of the parameter for which the value will be given. |
| 183 |
* @param mixed $value Value to give to this parameter. |
| 184 |
* |
| 185 |
* @return ObjectDefinitionHelper |
| 186 |
*/ |
| 187 |
public function methodParameter($method, $parameter, $value) |
| 188 |
{ |
| 189 |
// Special case for the constructor |
| 190 |
if ($method === '__construct') { |
| 191 |
$this->constructor[$parameter] = $value; |
| 192 |
return $this; |
| 193 |
} |
| 194 |
|
| 195 |
if (! isset($this->methods[$method])) { |
| 196 |
$this->methods[$method] = [0 => []]; |
| 197 |
} |
| 198 |
|
| 199 |
$this->methods[$method][0][$parameter] = $value; |
| 200 |
|
| 201 |
return $this; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* {@inheritdoc} |
| 206 |
*/ |
| 207 |
public function getDefinition($entryName) |
| 208 |
{ |
| 209 |
$definition = new ObjectDefinition($entryName, $this->className); |
| 210 |
|
| 211 |
if ($this->lazy !== null) { |
| 212 |
$definition->setLazy($this->lazy); |
| 213 |
} |
| 214 |
if ($this->scope !== null) { |
| 215 |
$definition->setScope($this->scope); |
| 216 |
} |
| 217 |
|
| 218 |
if (! empty($this->constructor)) { |
| 219 |
$parameters = $this->fixParameters($definition, '__construct', $this->constructor); |
| 220 |
$constructorInjection = MethodInjection::constructor($parameters); |
| 221 |
$definition->setConstructorInjection($constructorInjection); |
| 222 |
} |
| 223 |
|
| 224 |
if (! empty($this->properties)) { |
| 225 |
foreach ($this->properties as $property => $value) { |
| 226 |
$definition->addPropertyInjection( |
| 227 |
new PropertyInjection($property, $value) |
| 228 |
); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
if (! empty($this->methods)) { |
| 233 |
foreach ($this->methods as $method => $calls) { |
| 234 |
foreach ($calls as $parameters) { |
| 235 |
$parameters = $this->fixParameters($definition, $method, $parameters); |
| 236 |
$methodInjection = new MethodInjection($method, $parameters); |
| 237 |
$definition->addMethodInjection($methodInjection); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return $definition; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Fixes parameters indexed by the parameter name -> reindex by position. |
| 247 |
* |
| 248 |
* This is necessary so that merging definitions between sources is possible. |
| 249 |
* |
| 250 |
* @param ObjectDefinition $definition |
| 251 |
* @param string $method |
| 252 |
* @param array $parameters |
| 253 |
* @return array |
| 254 |
*/ |
| 255 |
private function fixParameters(ObjectDefinition $definition, $method, $parameters) |
| 256 |
{ |
| 257 |
$fixedParameters = []; |
| 258 |
|
| 259 |
foreach ($parameters as $index => $parameter) { |
| 260 |
// Parameter indexed by the parameter name, we reindex it with its position |
| 261 |
if (is_string($index)) { |
| 262 |
$callable = [$definition->getClassName(), $method]; |
| 263 |
$reflectionParameter = new \ReflectionParameter($callable, $index); |
| 264 |
|
| 265 |
$index = $reflectionParameter->getPosition(); |
| 266 |
} |
| 267 |
|
| 268 |
$fixedParameters[$index] = $parameter; |
| 269 |
} |
| 270 |
|
| 271 |
return $fixedParameters; |
| 272 |
} |
| 273 |
} |
| 274 |
|