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