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
|
* @author Arnaud Le Blanc <arnaud.lb@gmail.com>
|
17
|
* @deprecated since 1.12 (to be removed in 2.0)
|
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 LogicException('$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 LogicException('$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
|
* 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
|
/**
|
71
|
* Adds a TokenParserBroker.
|
72
|
*
|
73
|
* @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
|
74
|
*/
|
75
|
public function addTokenParserBroker(Twig_TokenParserBroker $broker)
|
76
|
{
|
77
|
$this->brokers[] = $broker;
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Removes a TokenParserBroker.
|
82
|
*
|
83
|
* @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
|
84
|
*/
|
85
|
public function removeTokenParserBroker(Twig_TokenParserBroker $broker)
|
86
|
{
|
87
|
if (false !== $pos = array_search($broker, $this->brokers)) {
|
88
|
unset($this->brokers[$pos]);
|
89
|
}
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Gets a suitable TokenParser for a tag.
|
94
|
*
|
95
|
* First looks in parsers, then in brokers.
|
96
|
*
|
97
|
* @param string $tag A tag name
|
98
|
*
|
99
|
* @return null|Twig_TokenParserInterface A Twig_TokenParserInterface or null if no suitable TokenParser was found
|
100
|
*/
|
101
|
public function getTokenParser($tag)
|
102
|
{
|
103
|
if (isset($this->parsers[$tag])) {
|
104
|
return $this->parsers[$tag];
|
105
|
}
|
106
|
$broker = end($this->brokers);
|
107
|
while (false !== $broker) {
|
108
|
$parser = $broker->getTokenParser($tag);
|
109
|
if (null !== $parser) {
|
110
|
return $parser;
|
111
|
}
|
112
|
$broker = prev($this->brokers);
|
113
|
}
|
114
|
}
|
115
|
|
116
|
public function getParsers()
|
117
|
{
|
118
|
return $this->parsers;
|
119
|
}
|
120
|
|
121
|
public function getParser()
|
122
|
{
|
123
|
return $this->parser;
|
124
|
}
|
125
|
|
126
|
public function setParser(Twig_ParserInterface $parser)
|
127
|
{
|
128
|
$this->parser = $parser;
|
129
|
foreach ($this->parsers as $tokenParser) {
|
130
|
$tokenParser->setParser($parser);
|
131
|
}
|
132
|
foreach ($this->brokers as $broker) {
|
133
|
$broker->setParser($parser);
|
134
|
}
|
135
|
}
|
136
|
}
|