|
14 | 14 | namespace ApiPlatform\Metadata\Extractor; |
15 | 15 |
|
16 | 16 | use Psr\Container\ContainerInterface; |
17 | | -use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface; |
18 | 17 |
|
19 | 18 | /** |
20 | 19 | * Base file extractor. |
|
23 | 22 | */ |
24 | 23 | abstract class AbstractPropertyExtractor implements PropertyExtractorInterface |
25 | 24 | { |
| 25 | + use ResolveValueTrait; |
| 26 | + |
26 | 27 | protected ?array $properties = null; |
27 | 28 | private array $collectedParameters = []; |
28 | 29 |
|
@@ -54,70 +55,4 @@ public function getProperties(): array |
54 | 55 | * Extracts metadata from a given path. |
55 | 56 | */ |
56 | 57 | abstract protected function extractPath(string $path): void; |
57 | | - |
58 | | - /** |
59 | | - * Recursively replaces placeholders with the service container parameters. |
60 | | - * |
61 | | - * @see https://github.com/symfony/symfony/blob/6fec32c/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php |
62 | | - * |
63 | | - * @copyright (c) Fabien Potencier <fabien@symfony.com> |
64 | | - * |
65 | | - * @param mixed $value The source which might contain "%placeholders%" |
66 | | - * |
67 | | - * @throws \RuntimeException When a container value is not a string or a numeric value |
68 | | - * |
69 | | - * @return mixed The source with the placeholders replaced by the container |
70 | | - * parameters. Arrays are resolved recursively. |
71 | | - */ |
72 | | - protected function resolve(mixed $value): mixed |
73 | | - { |
74 | | - if (null === $this->container) { |
75 | | - return $value; |
76 | | - } |
77 | | - |
78 | | - if (\is_array($value)) { |
79 | | - foreach ($value as $key => $val) { |
80 | | - $value[$key] = $this->resolve($val); |
81 | | - } |
82 | | - |
83 | | - return $value; |
84 | | - } |
85 | | - |
86 | | - if (!\is_string($value)) { |
87 | | - return $value; |
88 | | - } |
89 | | - |
90 | | - $escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($value) { |
91 | | - $parameter = $match[1] ?? null; |
92 | | - |
93 | | - // skip %% |
94 | | - if (!isset($parameter)) { |
95 | | - return '%%'; |
96 | | - } |
97 | | - |
98 | | - if (preg_match('/^env\(\w+\)$/', $parameter)) { |
99 | | - throw new \RuntimeException(\sprintf('Using "%%%s%%" is not allowed in routing configuration.', $parameter)); |
100 | | - } |
101 | | - |
102 | | - if (\array_key_exists($parameter, $this->collectedParameters)) { |
103 | | - return $this->collectedParameters[$parameter]; |
104 | | - } |
105 | | - |
106 | | - if ($this->container instanceof SymfonyContainerInterface) { |
107 | | - $resolved = $this->container->getParameter($parameter); |
108 | | - } else { |
109 | | - $resolved = $this->container->get($parameter); |
110 | | - } |
111 | | - |
112 | | - if (\is_string($resolved) || is_numeric($resolved)) { |
113 | | - $this->collectedParameters[$parameter] = $resolved; |
114 | | - |
115 | | - return (string) $resolved; |
116 | | - } |
117 | | - |
118 | | - throw new \RuntimeException(\sprintf('The container parameter "%s", used in the resource configuration value "%s", must be a string or numeric, but it is of type %s.', $parameter, $value, \gettype($resolved))); |
119 | | - }, $value); |
120 | | - |
121 | | - return str_replace('%%', '%', $escapedValue); |
122 | | - } |
123 | 58 | } |
0 commit comments