You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tag.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Extensions_Grammar_Tag extends Twig_Extensions_Grammar
  11. {
  12. protected $grammar;
  13. public function __construct()
  14. {
  15. $this->grammar = array();
  16. foreach (func_get_args() as $grammar) {
  17. $this->addGrammar($grammar);
  18. }
  19. }
  20. public function __toString()
  21. {
  22. $repr = array();
  23. foreach ($this->grammar as $grammar) {
  24. $repr[] = (string) $grammar;
  25. }
  26. return implode(' ', $repr);
  27. }
  28. public function addGrammar(Twig_Extensions_GrammarInterface $grammar)
  29. {
  30. $this->grammar[] = $grammar;
  31. }
  32. public function parse(Twig_Token $token)
  33. {
  34. $elements = array();
  35. foreach ($this->grammar as $grammar) {
  36. $grammar->setParser($this->parser);
  37. $element = $grammar->parse($token);
  38. if (is_array($element)) {
  39. $elements = array_merge($elements, $element);
  40. } else {
  41. $elements[$grammar->getName()] = $element;
  42. }
  43. }
  44. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  45. return $elements;
  46. }
  47. }