|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.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 | +namespace Symfony\Component\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use Symfony\Component\Config\Definition\BaseNode; |
| 15 | +use Symfony\Component\Config\Definition\Processor; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\Exception\LogicException; |
| 18 | +use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface; |
| 19 | +use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; |
| 20 | +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
| 21 | + |
| 22 | +/** |
| 23 | + * Validates environment variable placeholders used in extension configuration with dummy values. |
| 24 | + * |
| 25 | + * @author Roland Franssen <franssen.roland@gmail.com> |
| 26 | + */ |
| 27 | +class ValidateEnvPlaceholdersPass implements CompilerPassInterface |
| 28 | +{ |
| 29 | + private static $typeFixtures = array('array' => array(), 'bool' => false, 'float' => 0.0, 'int' => 0, 'string' => ''); |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + public function process(ContainerBuilder $container) |
| 35 | + { |
| 36 | + if (!class_exists(BaseNode::class) || !$extensions = $container->getExtensions()) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + $resolvingBag = $container->getParameterBag(); |
| 41 | + if (!$resolvingBag instanceof EnvPlaceholderParameterBag) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + $defaultBag = new ParameterBag($container->getParameterBag()->all()); |
| 46 | + $envTypes = $resolvingBag->getProvidedTypes(); |
| 47 | + try { |
| 48 | + foreach ($resolvingBag->getEnvPlaceholders() + $resolvingBag->getUnusedEnvPlaceholders() as $env => $placeholders) { |
| 49 | + $prefix = (false === $i = strpos($env, ':')) ? 'string' : substr($env, 0, $i); |
| 50 | + $types = $envTypes[$prefix] ?? array('string'); |
| 51 | + $default = ($hasEnv = (false === $i && $defaultBag->has("env($env)"))) ? $defaultBag->get("env($env)") : null; |
| 52 | + |
| 53 | + if (null !== $default && !in_array($type = self::getType($default), $types, true)) { |
| 54 | + throw new LogicException(sprintf('Invalid type for env parameter "env(%s)". Expected "%s", but got "%s".', $env, implode('", "', $types), $type)); |
| 55 | + } |
| 56 | + |
| 57 | + $values = array(); |
| 58 | + foreach ($types as $type) { |
| 59 | + $values[$type] = $hasEnv ? $default : self::$typeFixtures[$type] ?? null; |
| 60 | + } |
| 61 | + foreach ($placeholders as $placeholder) { |
| 62 | + BaseNode::setPlaceholder($placeholder, $values); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + $processor = new Processor(); |
| 67 | + |
| 68 | + foreach ($extensions as $name => $extension) { |
| 69 | + if (!$extension instanceof ConfigurationExtensionInterface || !$config = $container->getExtensionConfig($name)) { |
| 70 | + // this extension has no semantic configuration or was not called |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + $config = $resolvingBag->resolveValue($config); |
| 75 | + |
| 76 | + if (null === $configuration = $extension->getConfiguration($config, $container)) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + $processor->processConfiguration($configuration, $config); |
| 81 | + } |
| 82 | + } finally { |
| 83 | + BaseNode::resetPlaceholders(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static function getType($value): string |
| 88 | + { |
| 89 | + switch ($type = \gettype($value)) { |
| 90 | + case 'boolean': |
| 91 | + return 'bool'; |
| 92 | + case 'double': |
| 93 | + return 'float'; |
| 94 | + case 'integer': |
| 95 | + return 'int'; |
| 96 | + } |
| 97 | + |
| 98 | + return $type; |
| 99 | + } |
| 100 | +} |
0 commit comments