|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Metadata\Extractor; |
| 15 | + |
| 16 | +use Psr\Container\ContainerInterface; |
| 17 | +use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * @internal |
| 21 | + */ |
| 22 | +trait ResolveValueTrait |
| 23 | +{ |
| 24 | + private ?ContainerInterface $container = null; |
| 25 | + |
| 26 | + /** |
| 27 | + * Recursively replaces placeholders with the service container parameters. |
| 28 | + * |
| 29 | + * @see https://github.com/symfony/symfony/blob/6fec32c/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php |
| 30 | + * |
| 31 | + * @copyright (c) Fabien Potencier <fabien@symfony.com> |
| 32 | + * |
| 33 | + * @param mixed $value The source which might contain "%placeholders%" |
| 34 | + * |
| 35 | + * @throws \RuntimeException When a container value is not a string or a numeric value |
| 36 | + * |
| 37 | + * @return mixed The source with the placeholders replaced by the container |
| 38 | + * parameters. Arrays are resolved recursively. |
| 39 | + */ |
| 40 | + protected function resolve(mixed $value): mixed |
| 41 | + { |
| 42 | + if (null === $this->container) { |
| 43 | + return $value; |
| 44 | + } |
| 45 | + |
| 46 | + if (\is_array($value)) { |
| 47 | + foreach ($value as $key => $val) { |
| 48 | + $value[$key] = $this->resolve($val); |
| 49 | + } |
| 50 | + |
| 51 | + return $value; |
| 52 | + } |
| 53 | + |
| 54 | + if (!\is_string($value)) { |
| 55 | + return $value; |
| 56 | + } |
| 57 | + |
| 58 | + $escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($value) { |
| 59 | + $parameter = $match[1] ?? null; |
| 60 | + |
| 61 | + // skip %% |
| 62 | + if (!isset($parameter)) { |
| 63 | + return '%%'; |
| 64 | + } |
| 65 | + |
| 66 | + if (preg_match('/^env\(\w+\)$/', $parameter)) { |
| 67 | + throw new \RuntimeException(\sprintf('Using "%%%s%%" is not allowed in routing configuration.', $parameter)); |
| 68 | + } |
| 69 | + |
| 70 | + if (\array_key_exists($parameter, $this->collectedParameters)) { |
| 71 | + return $this->collectedParameters[$parameter]; |
| 72 | + } |
| 73 | + |
| 74 | + if ($this->container instanceof SymfonyContainerInterface) { |
| 75 | + $resolved = $this->container->getParameter($parameter); |
| 76 | + } else { |
| 77 | + $resolved = $this->container->get($parameter); |
| 78 | + } |
| 79 | + |
| 80 | + if (\is_string($resolved) || is_numeric($resolved)) { |
| 81 | + $this->collectedParameters[$parameter] = $resolved; |
| 82 | + |
| 83 | + return (string) $resolved; |
| 84 | + } |
| 85 | + |
| 86 | + 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))); |
| 87 | + }, $value); |
| 88 | + |
| 89 | + return str_replace('%%', '%', $escapedValue); |
| 90 | + } |
| 91 | +} |
0 commit comments