| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition\Helper; |
| 6 |
|
| 7 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\AutowireDefinition; |
| 8 |
|
| 9 |
/** |
| 10 |
* Helps defining how to create an instance of a class using autowiring. |
| 11 |
* |
| 12 |
* @author Matthieu Napoli <matthieu@mnapoli.fr> |
| 13 |
*/ |
| 14 |
class AutowireDefinitionHelper extends CreateDefinitionHelper |
| 15 |
{ |
| 16 |
const DEFINITION_CLASS = AutowireDefinition::class; |
| 17 |
|
| 18 |
/** |
| 19 |
* Defines a value for a specific argument of the constructor. |
| 20 |
* |
| 21 |
* This method is usually used together with annotations or autowiring, when a parameter |
| 22 |
* is not (or cannot be) type-hinted. Using this method instead of constructor() allows to |
| 23 |
* avoid defining all the parameters (letting them being resolved using annotations or autowiring) |
| 24 |
* and only define one. |
| 25 |
* |
| 26 |
* @param string|int $parameter Parameter name of position for which the value will be given. |
| 27 |
* @param mixed $value Value to give to this parameter. |
| 28 |
* |
| 29 |
* @return $this |
| 30 |
*/ |
| 31 |
public function constructorParameter($parameter, $value) |
| 32 |
{ |
| 33 |
$this->constructor[$parameter] = $value; |
| 34 |
|
| 35 |
return $this; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Defines a method to call and a value for a specific argument. |
| 40 |
* |
| 41 |
* This method is usually used together with annotations or autowiring, when a parameter |
| 42 |
* is not (or cannot be) type-hinted. Using this method instead of method() allows to |
| 43 |
* avoid defining all the parameters (letting them being resolved using annotations or |
| 44 |
* autowiring) and only define one. |
| 45 |
* |
| 46 |
* If multiple calls to the method have been configured already (e.g. in a previous definition) |
| 47 |
* then this method only overrides the parameter for the *first* call. |
| 48 |
* |
| 49 |
* @param string $method Name of the method to call. |
| 50 |
* @param string|int $parameter Parameter name of position for which the value will be given. |
| 51 |
* @param mixed $value Value to give to this parameter. |
| 52 |
* |
| 53 |
* @return $this |
| 54 |
*/ |
| 55 |
public function methodParameter(string $method, $parameter, $value) |
| 56 |
{ |
| 57 |
// Special case for the constructor |
| 58 |
if ($method === '__construct') { |
| 59 |
$this->constructor[$parameter] = $value; |
| 60 |
|
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
if (! isset($this->methods[$method])) { |
| 65 |
$this->methods[$method] = [0 => []]; |
| 66 |
} |
| 67 |
|
| 68 |
$this->methods[$method][0][$parameter] = $value; |
| 69 |
|
| 70 |
return $this; |
| 71 |
} |
| 72 |
} |
| 73 |
|