PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.3
4.9.3 4.9.2 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 All 201 releases
betterdocs / includes / Dependencies / DI / Definition / Resolver / DecoratorResolver.php

DecoratorResolver.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.3, at includes/Dependencies/DI/Definition/Resolver/DecoratorResolver.php

86 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:ignoreFile -- Bundled third-party (Mozart) dependency; exempt from plugin coding standards.
3
4 declare(strict_types=1);
5
6 namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition\Resolver;
7
8 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\DecoratorDefinition;
9 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition;
10 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Exception\InvalidDefinition;
11 use WPDeveloper\BetterDocs\Dependencies\Psr\Container\ContainerInterface;
12
13 /**
14 * Resolves a decorator definition to a value.
15 *
16 * @since 5.0
17 * @author Matthieu Napoli <[email protected]>
18 */
19 class DecoratorResolver implements DefinitionResolver
20 {
21 /**
22 * @var ContainerInterface
23 */
24 private $container;
25
26 /**
27 * @var DefinitionResolver
28 */
29 private $definitionResolver;
30
31 /**
32 * The resolver needs a container. This container will be passed to the factory as a parameter
33 * so that the factory can access other entries of the container.
34 *
35 * @param DefinitionResolver $definitionResolver Used to resolve nested definitions.
36 */
37 public function __construct(ContainerInterface $container, DefinitionResolver $definitionResolver)
38 {
39 $this->container = $container;
40 $this->definitionResolver = $definitionResolver;
41 }
42
43 /**
44 * Resolve a decorator definition to a value.
45 *
46 * This will call the callable of the definition and pass it the decorated entry.
47 *
48 * @param DecoratorDefinition $definition
49 *
50 * {@inheritdoc}
51 */
52 public function resolve(Definition $definition, array $parameters = [])
53 {
54 $callable = $definition->getCallable();
55
56 if (! is_callable($callable)) {
57 throw new InvalidDefinition(sprintf(
58 'The decorator "%s" is not callable',
59 $definition->getName()
60 ));
61 }
62
63 $decoratedDefinition = $definition->getDecoratedDefinition();
64
65 if (! $decoratedDefinition instanceof Definition) {
66 if (! $definition->getName()) {
67 throw new InvalidDefinition('Decorators cannot be nested in another definition');
68 }
69
70 throw new InvalidDefinition(sprintf(
71 'Entry "%s" decorates nothing: no previous definition with the same name was found',
72 $definition->getName()
73 ));
74 }
75
76 $decorated = $this->definitionResolver->resolve($decoratedDefinition, $parameters);
77
78 return call_user_func($callable, $decorated, $this->container);
79 }
80
81 public function isResolvable(Definition $definition, array $parameters = []) : bool
82 {
83 return true;
84 }
85 }
86