RegisterEnvVarProcessorsPass.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\EnvVarProcessor;
  13. use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
  14. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  15. use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. /**
  18. * Creates the container.env_var_processors_locator service.
  19. *
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. */
  22. class RegisterEnvVarProcessorsPass implements CompilerPassInterface
  23. {
  24. private const ALLOWED_TYPES = ['array', 'bool', 'float', 'int', 'string'];
  25. public function process(ContainerBuilder $container)
  26. {
  27. $bag = $container->getParameterBag();
  28. $types = [];
  29. $processors = [];
  30. foreach ($container->findTaggedServiceIds('container.env_var_processor') as $id => $tags) {
  31. if (!$r = $container->getReflectionClass($class = $container->getDefinition($id)->getClass())) {
  32. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  33. } elseif (!$r->isSubclassOf(EnvVarProcessorInterface::class)) {
  34. throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, EnvVarProcessorInterface::class));
  35. }
  36. foreach ($class::getProvidedTypes() as $prefix => $type) {
  37. $processors[$prefix] = new Reference($id);
  38. $types[$prefix] = self::validateProvidedTypes($type, $class);
  39. }
  40. }
  41. if ($bag instanceof EnvPlaceholderParameterBag) {
  42. foreach (EnvVarProcessor::getProvidedTypes() as $prefix => $type) {
  43. if (!isset($types[$prefix])) {
  44. $types[$prefix] = self::validateProvidedTypes($type, EnvVarProcessor::class);
  45. }
  46. }
  47. $bag->setProvidedTypes($types);
  48. }
  49. if ($processors) {
  50. $container->setAlias('container.env_var_processors_locator', (string) ServiceLocatorTagPass::register($container, $processors))
  51. ->setPublic(true)
  52. ;
  53. }
  54. }
  55. private static function validateProvidedTypes(string $types, string $class): array
  56. {
  57. $types = explode('|', $types);
  58. foreach ($types as $type) {
  59. if (!\in_array($type, self::ALLOWED_TYPES)) {
  60. throw new InvalidArgumentException(sprintf('Invalid type "%s" returned by "%s::getProvidedTypes()", expected one of "%s".', $type, $class, implode('", "', self::ALLOWED_TYPES)));
  61. }
  62. }
  63. return $types;
  64. }
  65. }