ExpressionLanguageProvider.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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;
  11. use Symfony\Component\ExpressionLanguage\ExpressionFunction;
  12. use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
  13. /**
  14. * Define some ExpressionLanguage functions.
  15. *
  16. * To get a service, use service('request').
  17. * To get a parameter, use parameter('kernel.debug').
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
  22. {
  23. private $serviceCompiler;
  24. public function __construct(callable $serviceCompiler = null)
  25. {
  26. $this->serviceCompiler = $serviceCompiler;
  27. }
  28. public function getFunctions()
  29. {
  30. return [
  31. new ExpressionFunction('service', $this->serviceCompiler ?: function ($arg) {
  32. return sprintf('$this->get(%s)', $arg);
  33. }, function (array $variables, $value) {
  34. return $variables['container']->get($value);
  35. }),
  36. new ExpressionFunction('parameter', function ($arg) {
  37. return sprintf('$this->getParameter(%s)', $arg);
  38. }, function (array $variables, $value) {
  39. return $variables['container']->getParameter($value);
  40. }),
  41. ];
  42. }
  43. }