RegisterEnvVarProcessorsPass.php 3.1 KB

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