TokenParserBroker.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 Fabien Potencier
  6. * (c) 2010 Arnaud Le Blanc
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Default implementation of a token parser broker.
  13. *
  14. * @package twig
  15. * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
  16. */
  17. class Twig_TokenParserBroker implements Twig_TokenParserBrokerInterface
  18. {
  19. protected $parser;
  20. protected $parsers = array();
  21. protected $brokers = array();
  22. /**
  23. * Constructor.
  24. *
  25. * @param array|Traversable $parsers A Traversable of Twig_TokenParserInterface instances
  26. * @param array|Traversable $brokers A Traversable of Twig_TokenParserBrokerInterface instances
  27. */
  28. public function __construct($parsers = array(), $brokers = array())
  29. {
  30. foreach ($parsers as $parser) {
  31. if (!$parser instanceof Twig_TokenParserInterface) {
  32. throw new Twig_Error('$parsers must a an array of Twig_TokenParserInterface');
  33. }
  34. $this->parsers[$parser->getTag()] = $parser;
  35. }
  36. foreach ($brokers as $broker) {
  37. if (!$broker instanceof Twig_TokenParserBrokerInterface) {
  38. throw new Twig_Error('$brokers must a an array of Twig_TokenParserBrokerInterface');
  39. }
  40. $this->brokers[] = $broker;
  41. }
  42. }
  43. /**
  44. * Adds a TokenParser.
  45. *
  46. * @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
  47. */
  48. public function addTokenParser(Twig_TokenParserInterface $parser)
  49. {
  50. $this->parsers[$parser->getTag()] = $parser;
  51. }
  52. /**
  53. * Adds a TokenParserBroker.
  54. *
  55. * @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
  56. */
  57. public function addTokenParserBroker(Twig_TokenParserBroker $broker)
  58. {
  59. $this->brokers[] = $broker;
  60. }
  61. /**
  62. * Gets a suitable TokenParser for a tag.
  63. *
  64. * First looks in parsers, then in brokers.
  65. *
  66. * @param string $tag A tag name
  67. *
  68. * @return null|Twig_TokenParserInterface A Twig_TokenParserInterface or null if no suitable TokenParser was found
  69. */
  70. public function getTokenParser($tag)
  71. {
  72. if (isset($this->parsers[$tag])) {
  73. return $this->parsers[$tag];
  74. }
  75. $broker = end($this->brokers);
  76. while (false !== $broker) {
  77. $parser = $broker->getTokenParser($tag);
  78. if (null !== $parser) {
  79. return $parser;
  80. }
  81. $broker = prev($this->brokers);
  82. }
  83. return null;
  84. }
  85. public function getParsers()
  86. {
  87. return $this->parsers;
  88. }
  89. public function getParser()
  90. {
  91. return $this->parser;
  92. }
  93. public function setParser(Twig_ParserInterface $parser)
  94. {
  95. $this->parser = $parser;
  96. foreach ($this->parsers as $tokenParser) {
  97. $tokenParser->setParser($parser);
  98. }
  99. foreach ($this->brokers as $broker) {
  100. $broker->setParser($parser);
  101. }
  102. }
  103. }