PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.1
Defender Security – Malware Scanner, Login Security & Firewall v5.6.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / lib / packages / DI / Definition / StringDefinition.php

StringDefinition.php in Defender Security – Malware Scanner, Login Security & Firewall 5.6.1, at lib/packages/DI/Definition/StringDefinition.php

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