| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WPMU_DEV\Defender\Vendor\DI\Definition\Helper; |
| 6 |
|
| 7 |
use WPMU_DEV\Defender\Vendor\DI\Definition\DecoratorDefinition; |
| 8 |
use WPMU_DEV\Defender\Vendor\DI\Definition\FactoryDefinition; |
| 9 |
|
| 10 |
/** |
| 11 |
* Helps defining how to create an instance of a class using a factory (callable). |
| 12 |
* |
| 13 |
* @author Matthieu Napoli <[email protected]> |
| 14 |
*/ |
| 15 |
class FactoryDefinitionHelper implements DefinitionHelper |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @var callable |
| 19 |
*/ |
| 20 |
private $factory; |
| 21 |
|
| 22 |
private bool $decorate; |
| 23 |
|
| 24 |
private array $parameters = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* @param bool $decorate Is the factory decorating a previous definition? |
| 28 |
*/ |
| 29 |
public function __construct(callable|array|string $factory, bool $decorate = false) |
| 30 |
{ |
| 31 |
$this->factory = $factory; |
| 32 |
$this->decorate = $decorate; |
| 33 |
} |
| 34 |
|
| 35 |
public function getDefinition(string $entryName) : FactoryDefinition |
| 36 |
{ |
| 37 |
if ($this->decorate) { |
| 38 |
return new DecoratorDefinition($entryName, $this->factory, $this->parameters); |
| 39 |
} |
| 40 |
|
| 41 |
return new FactoryDefinition($entryName, $this->factory, $this->parameters); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Defines arguments to pass to the factory. |
| 46 |
* |
| 47 |
* Because factory methods do not yet support attributes or autowiring, this method |
| 48 |
* should be used to define all parameters except the ContainerInterface and RequestedEntry. |
| 49 |
* |
| 50 |
* Multiple calls can be made to the method to override individual values. |
| 51 |
* |
| 52 |
* @param string $parameter Name or index of the parameter for which the value will be given. |
| 53 |
* @param mixed $value Value to give to this parameter. |
| 54 |
* |
| 55 |
* @return $this |
| 56 |
*/ |
| 57 |
public function parameter(string $parameter, mixed $value) : self |
| 58 |
{ |
| 59 |
$this->parameters[$parameter] = $value; |
| 60 |
|
| 61 |
return $this; |
| 62 |
} |
| 63 |
} |
| 64 |
|