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