TokenParserBroker.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
  15. *
  16. * @deprecated since 1.12 (to be removed in 2.0)
  17. */
  18. class Twig_TokenParserBroker implements Twig_TokenParserBrokerInterface
  19. {
  20. protected $parser;
  21. protected $parsers = array();
  22. protected $brokers = array();
  23. /**
  24. * Constructor.
  25. *
  26. * @param array|Traversable $parsers A Traversable of Twig_TokenParserInterface instances
  27. * @param array|Traversable $brokers A Traversable of Twig_TokenParserBrokerInterface instances
  28. * @param bool $triggerDeprecationError
  29. */
  30. public function __construct($parsers = array(), $brokers = array(), $triggerDeprecationError = true)
  31. {
  32. if ($triggerDeprecationError) {
  33. @trigger_error('The '.__CLASS__.' class is deprecated since version 1.12 and will be removed in 2.0.', E_USER_DEPRECATED);
  34. }
  35. foreach ($parsers as $parser) {
  36. if (!$parser instanceof Twig_TokenParserInterface) {
  37. throw new LogicException('$parsers must a an array of Twig_TokenParserInterface.');
  38. }
  39. $this->parsers[$parser->getTag()] = $parser;
  40. }
  41. foreach ($brokers as $broker) {
  42. if (!$broker instanceof Twig_TokenParserBrokerInterface) {
  43. throw new LogicException('$brokers must a an array of Twig_TokenParserBrokerInterface.');
  44. }
  45. $this->brokers[] = $broker;
  46. }
  47. }
  48. /**
  49. * Adds a TokenParser.
  50. *
  51. * @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
  52. */
  53. public function addTokenParser(Twig_TokenParserInterface $parser)
  54. {
  55. $this->parsers[$parser->getTag()] = $parser;
  56. }
  57. /**
  58. * Removes a TokenParser.
  59. *
  60. * @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
  61. */
  62. public function removeTokenParser(Twig_TokenParserInterface $parser)
  63. {
  64. $name = $parser->getTag();
  65. if (isset($this->parsers[$name]) && $parser === $this->parsers[$name]) {
  66. unset($this->parsers[$name]);
  67. }
  68. }
  69. /**
  70. * Adds a TokenParserBroker.
  71. *
  72. * @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
  73. */
  74. public function addTokenParserBroker(Twig_TokenParserBroker $broker)
  75. {
  76. $this->brokers[] = $broker;
  77. }
  78. /**
  79. * Removes a TokenParserBroker.
  80. *
  81. * @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
  82. */
  83. public function removeTokenParserBroker(Twig_TokenParserBroker $broker)
  84. {
  85. if (false !== $pos = array_search($broker, $this->brokers)) {
  86. unset($this->brokers[$pos]);
  87. }
  88. }
  89. /**
  90. * Gets a suitable TokenParser for a tag.
  91. *
  92. * First looks in parsers, then in brokers.
  93. *
  94. * @param string $tag A tag name
  95. *
  96. * @return null|Twig_TokenParserInterface A Twig_TokenParserInterface or null if no suitable TokenParser was found
  97. */
  98. public function getTokenParser($tag)
  99. {
  100. if (isset($this->parsers[$tag])) {
  101. return $this->parsers[$tag];
  102. }
  103. $broker = end($this->brokers);
  104. while (false !== $broker) {
  105. $parser = $broker->getTokenParser($tag);
  106. if (null !== $parser) {
  107. return $parser;
  108. }
  109. $broker = prev($this->brokers);
  110. }
  111. }
  112. public function getParsers()
  113. {
  114. return $this->parsers;
  115. }
  116. public function getParser()
  117. {
  118. return $this->parser;
  119. }
  120. public function setParser(Twig_ParserInterface $parser)
  121. {
  122. $this->parser = $parser;
  123. foreach ($this->parsers as $tokenParser) {
  124. $tokenParser->setParser($parser);
  125. }
  126. foreach ($this->brokers as $broker) {
  127. $broker->setParser($parser);
  128. }
  129. }
  130. }