4 * This file is part of Twig.
6 * (c) 2010 Fabien Potencier
7 * (c) 2010 Arnaud Le Blanc
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
14 * Default implementation of a token parser broker.
16 * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
17 * @deprecated since 1.12 (to be removed in 2.0)
19 class Twig_TokenParserBroker
implements Twig_TokenParserBrokerInterface
22 protected $parsers = array();
23 protected $brokers = array();
28 * @param array|Traversable $parsers A Traversable of Twig_TokenParserInterface instances
29 * @param array|Traversable $brokers A Traversable of Twig_TokenParserBrokerInterface instances
31 public function __construct($parsers = array(), $brokers = array())
33 foreach ($parsers as $parser) {
34 if (!$parser instanceof Twig_TokenParserInterface
) {
35 throw new LogicException('$parsers must a an array of Twig_TokenParserInterface');
37 $this->parsers
[$parser->getTag()] = $parser;
39 foreach ($brokers as $broker) {
40 if (!$broker instanceof Twig_TokenParserBrokerInterface
) {
41 throw new LogicException('$brokers must a an array of Twig_TokenParserBrokerInterface');
43 $this->brokers
[] = $broker;
50 * @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
52 public function addTokenParser(Twig_TokenParserInterface
$parser)
54 $this->parsers
[$parser->getTag()] = $parser;
58 * Removes a TokenParser.
60 * @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
62 public function removeTokenParser(Twig_TokenParserInterface
$parser)
64 $name = $parser->getTag();
65 if (isset($this->parsers
[$name]) && $parser === $this->parsers
[$name]) {
66 unset($this->parsers
[$name]);
71 * Adds a TokenParserBroker.
73 * @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
75 public function addTokenParserBroker(Twig_TokenParserBroker
$broker)
77 $this->brokers
[] = $broker;
81 * Removes a TokenParserBroker.
83 * @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
85 public function removeTokenParserBroker(Twig_TokenParserBroker
$broker)
87 if (false !== $pos = array_search($broker, $this->brokers
)) {
88 unset($this->brokers
[$pos]);
93 * Gets a suitable TokenParser for a tag.
95 * First looks in parsers, then in brokers.
97 * @param string $tag A tag name
99 * @return null|Twig_TokenParserInterface A Twig_TokenParserInterface or null if no suitable TokenParser was found
101 public function getTokenParser($tag)
103 if (isset($this->parsers
[$tag])) {
104 return $this->parsers
[$tag];
106 $broker = end($this->brokers
);
107 while (false !== $broker) {
108 $parser = $broker->getTokenParser($tag);
109 if (null !== $parser) {
112 $broker = prev($this->brokers
);
116 public function getParsers()
118 return $this->parsers
;
121 public function getParser()
123 return $this->parser
;
126 public function setParser(Twig_ParserInterface
$parser)
128 $this->parser
= $parser;
129 foreach ($this->parsers
as $tokenParser) {
130 $tokenParser->setParser($parser);
132 foreach ($this->brokers
as $broker) {
133 $broker->setParser($parser);