EnvironmentConfigurator.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Bundle\TwigBundle\DependencyInjection\Configurator;
  11. use Symfony\Bridge\Twig\UndefinedCallableHandler;
  12. use Twig\Environment;
  13. // BC/FC with namespaced Twig
  14. class_exists(Environment::class);
  15. /**
  16. * Twig environment configurator.
  17. *
  18. * @author Christian Flothmann <christian.flothmann@xabbuh.de>
  19. */
  20. class EnvironmentConfigurator
  21. {
  22. private $dateFormat;
  23. private $intervalFormat;
  24. private $timezone;
  25. private $decimals;
  26. private $decimalPoint;
  27. private $thousandsSeparator;
  28. public function __construct(string $dateFormat, string $intervalFormat, ?string $timezone, int $decimals, string $decimalPoint, string $thousandsSeparator)
  29. {
  30. $this->dateFormat = $dateFormat;
  31. $this->intervalFormat = $intervalFormat;
  32. $this->timezone = $timezone;
  33. $this->decimals = $decimals;
  34. $this->decimalPoint = $decimalPoint;
  35. $this->thousandsSeparator = $thousandsSeparator;
  36. }
  37. public function configure(Environment $environment)
  38. {
  39. $environment->getExtension('Twig\Extension\CoreExtension')->setDateFormat($this->dateFormat, $this->intervalFormat);
  40. if (null !== $this->timezone) {
  41. $environment->getExtension('Twig\Extension\CoreExtension')->setTimezone($this->timezone);
  42. }
  43. $environment->getExtension('Twig\Extension\CoreExtension')->setNumberFormat($this->decimals, $this->decimalPoint, $this->thousandsSeparator);
  44. // wrap UndefinedCallableHandler in closures for lazy-autoloading
  45. $environment->registerUndefinedFilterCallback(function ($name) { return UndefinedCallableHandler::onUndefinedFilter($name); });
  46. $environment->registerUndefinedFunctionCallback(function ($name) { return UndefinedCallableHandler::onUndefinedFunction($name); });
  47. }
  48. }