ParametersConfigurator.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Loader\Configurator;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  13. use Symfony\Component\ExpressionLanguage\Expression;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class ParametersConfigurator extends AbstractConfigurator
  18. {
  19. public const FACTORY = 'parameters';
  20. private $container;
  21. public function __construct(ContainerBuilder $container)
  22. {
  23. $this->container = $container;
  24. }
  25. /**
  26. * Creates a parameter.
  27. *
  28. * @return $this
  29. */
  30. final public function set(string $name, $value): self
  31. {
  32. if ($value instanceof Expression) {
  33. throw new InvalidArgumentException(sprintf('Using an expression in parameter "%s" is not allowed.', $name));
  34. }
  35. $this->container->setParameter($name, static::processValue($value, true));
  36. return $this;
  37. }
  38. /**
  39. * Creates a parameter.
  40. *
  41. * @return $this
  42. */
  43. final public function __invoke(string $name, $value): self
  44. {
  45. return $this->set($name, $value);
  46. }
  47. }