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