TemplateBoxTokenParser.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\CoreBundle\Twig\TokenParser;
  11. use Sonata\CoreBundle\Twig\Node\TemplateBoxNode;
  12. use Symfony\Component\Translation\TranslatorInterface;
  13. class TemplateBoxTokenParser extends \Twig_TokenParser
  14. {
  15. /**
  16. * @var bool
  17. */
  18. protected $enabled;
  19. /**
  20. * @var TranslatorInterface
  21. */
  22. protected $translator;
  23. /**
  24. * @param bool $enabled Is Symfony debug enabled?
  25. * @param TranslatorInterface $translator Symfony Translator service
  26. */
  27. public function __construct($enabled, TranslatorInterface $translator)
  28. {
  29. $this->enabled = $enabled;
  30. $this->translator = $translator;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function parse(\Twig_Token $token)
  36. {
  37. if ($this->parser->getStream()->test(\Twig_Token::STRING_TYPE)) {
  38. $message = $this->parser->getExpressionParser()->parseExpression();
  39. } else {
  40. $message = new \Twig_Node_Expression_Constant('Template information', $token->getLine());
  41. }
  42. if ($this->parser->getStream()->test(\Twig_Token::STRING_TYPE)) {
  43. $translationBundle = $this->parser->getExpressionParser()->parseExpression();
  44. } else {
  45. $translationBundle = null;
  46. }
  47. $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
  48. return new TemplateBoxNode($message, $translationBundle, $this->enabled, $this->translator, $token->getLine(), $this->getTag());
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getTag()
  54. {
  55. return 'sonata_template_box';
  56. }
  57. }