PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.7.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.7.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Dependencies / DI / Definition / Helper / FactoryDefinitionHelper.php

FactoryDefinitionHelper.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.7.0, at includes/Dependencies/DI/Definition/Helper/FactoryDefinitionHelper.php

76 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\DecoratorDefinition;
8 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition;
9 use WPDeveloper\BetterDocs\Dependencies\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