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
12namespace Symfony\Component\DependencyInjection\Compiler;
13
14use Symfony\Component\DependencyInjection\ContainerBuilder;
15use Symfony\Component\DependencyInjection\EnvVarProcessor;
16use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
17use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
19use Symfony\Component\DependencyInjection\Reference;
20
21/**
22 * Creates the container.env_var_processors_locator service.
23 *
24 * @author Nicolas Grekas <p@tchwork.com>
25 */
26class RegisterEnvVarProcessorsPass implements CompilerPassInterface
27{
28    private static $allowedTypes = ['array', 'bool', 'float', 'int', 'string'];
29
30    public function process(ContainerBuilder $container)
31    {
32        $bag = $container->getParameterBag();
33        $types = [];
34        $processors = [];
35        foreach ($container->findTaggedServiceIds('container.env_var_processor') as $id => $tags) {
36            if (!$r = $container->getReflectionClass($class = $container->getDefinition($id)->getClass())) {
37                throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
38            } elseif (!$r->isSubclassOf(EnvVarProcessorInterface::class)) {
39                throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, EnvVarProcessorInterface::class));
40            }
41            foreach ($class::getProvidedTypes() as $prefix => $type) {
42                $processors[$prefix] = new Reference($id);
43                $types[$prefix] = self::validateProvidedTypes($type, $class);
44            }
45        }
46
47        if ($bag instanceof EnvPlaceholderParameterBag) {
48            foreach (EnvVarProcessor::getProvidedTypes() as $prefix => $type) {
49                if (!isset($types[$prefix])) {
50                    $types[$prefix] = self::validateProvidedTypes($type, EnvVarProcessor::class);
51                }
52            }
53            $bag->setProvidedTypes($types);
54        }
55
56        if ($processors) {
57            $container->setAlias('container.env_var_processors_locator', (string) ServiceLocatorTagPass::register($container, $processors))
58                ->setPublic(true)
59            ;
60        }
61    }
62
63    private static function validateProvidedTypes(string $types, string $class): array
64    {
65        $types = explode('|', $types);
66
67        foreach ($types as $type) {
68            if (!\in_array($type, self::$allowedTypes)) {
69                throw new InvalidArgumentException(sprintf('Invalid type "%s" returned by "%s::getProvidedTypes()", expected one of "%s".', $type, $class, implode('", "', self::$allowedTypes)));
70            }
71        }
72
73        return $types;
74    }
75}
76