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 / StringDefinition.php

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

99 lines 2.5 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;
7
8 use WPDeveloper\BetterDocs\Dependencies\DI\DependencyException;
9 use WPDeveloper\BetterDocs\Dependencies\Psr\Container\ContainerInterface;
10 use WPDeveloper\BetterDocs\Dependencies\Psr\Container\NotFoundExceptionInterface;
11
12 /**
13 * Definition of a string composed of other strings.
14 *
15 * @since 5.0
16 * @author Matthieu Napoli <matthieu@mnapoli.fr>
17 */
18 class StringDefinition implements Definition, SelfResolvingDefinition
19 {
20 /**
21 * Entry name.
22 * @var string
23 */
24 private $name = '';
25
26 /**
27 * @var string
28 */
29 private $expression;
30
31 public function __construct(string $expression)
32 {
33 $this->expression = $expression;
34 }
35
36 public function getName() : string
37 {
38 return $this->name;
39 }
40
41 public function setName(string $name)
42 {
43 $this->name = $name;
44 }
45
46 public function getExpression() : string
47 {
48 return $this->expression;
49 }
50
51 public function resolve(ContainerInterface $container) : string
52 {
53 return self::resolveExpression($this->name, $this->expression, $container);
54 }
55
56 public function isResolvable(ContainerInterface $container) : bool
57 {
58 return true;
59 }
60
61 public function replaceNestedDefinitions(callable $replacer)
62 {
63 // no nested definitions
64 }
65
66 public function __toString()
67 {
68 return $this->expression;
69 }
70
71 /**
72 * Resolve a string expression.
73 */
74 public static function resolveExpression(
75 string $entryName,
76 string $expression,
77 ContainerInterface $container
78 ) : string {
79 $callback = function (array $matches) use ($entryName, $container) {
80 try {
81 return $container->get($matches[1]);
82 } catch (NotFoundExceptionInterface $e) {
83 throw new DependencyException(sprintf(
84 "Error while parsing string expression for entry '%s': %s",
85 $entryName,
86 $e->getMessage()
87 ), 0, $e);
88 }
89 };
90
91 $result = preg_replace_callback('#\{([^\{\}]+)\}#', $callback, $expression);
92 if ($result === null) {
93 throw new \RuntimeException(sprintf('An unknown error occurred while parsing the string definition: \'%s\'', $expression));
94 }
95
96 return $result;
97 }
98 }
99