Project

General

Profile

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
 *
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
     * @param bool              $triggerDeprecationError
32
     */
33
    public function __construct($parsers = array(), $brokers = array(), $triggerDeprecationError = true)
34
    {
35
        if ($triggerDeprecationError) {
36
            @trigger_error('The '.__CLASS__.' class is deprecated since version 1.12 and will be removed in 2.0.', E_USER_DEPRECATED);
37
        }
38

    
39
        foreach ($parsers as $parser) {
40
            if (!$parser instanceof Twig_TokenParserInterface) {
41
                throw new LogicException('$parsers must a an array of Twig_TokenParserInterface.');
42
            }
43
            $this->parsers[$parser->getTag()] = $parser;
44
        }
45
        foreach ($brokers as $broker) {
46
            if (!$broker instanceof Twig_TokenParserBrokerInterface) {
47
                throw new LogicException('$brokers must a an array of Twig_TokenParserBrokerInterface.');
48
            }
49
            $this->brokers[] = $broker;
50
        }
51
    }
52

    
53
    /**
54
     * Adds a TokenParser.
55
     *
56
     * @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
57
     */
58
    public function addTokenParser(Twig_TokenParserInterface $parser)
59
    {
60
        $this->parsers[$parser->getTag()] = $parser;
61
    }
62

    
63
    /**
64
     * Removes a TokenParser.
65
     *
66
     * @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
67
     */
68
    public function removeTokenParser(Twig_TokenParserInterface $parser)
69
    {
70
        $name = $parser->getTag();
71
        if (isset($this->parsers[$name]) && $parser === $this->parsers[$name]) {
72
            unset($this->parsers[$name]);
73
        }
74
    }
75

    
76
    /**
77
     * Adds a TokenParserBroker.
78
     *
79
     * @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
80
     */
81
    public function addTokenParserBroker(Twig_TokenParserBroker $broker)
82
    {
83
        $this->brokers[] = $broker;
84
    }
85

    
86
    /**
87
     * Removes a TokenParserBroker.
88
     *
89
     * @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
90
     */
91
    public function removeTokenParserBroker(Twig_TokenParserBroker $broker)
92
    {
93
        if (false !== $pos = array_search($broker, $this->brokers)) {
94
            unset($this->brokers[$pos]);
95
        }
96
    }
97

    
98
    /**
99
     * Gets a suitable TokenParser for a tag.
100
     *
101
     * First looks in parsers, then in brokers.
102
     *
103
     * @param string $tag A tag name
104
     *
105
     * @return null|Twig_TokenParserInterface A Twig_TokenParserInterface or null if no suitable TokenParser was found
106
     */
107
    public function getTokenParser($tag)
108
    {
109
        if (isset($this->parsers[$tag])) {
110
            return $this->parsers[$tag];
111
        }
112
        $broker = end($this->brokers);
113
        while (false !== $broker) {
114
            $parser = $broker->getTokenParser($tag);
115
            if (null !== $parser) {
116
                return $parser;
117
            }
118
            $broker = prev($this->brokers);
119
        }
120
    }
121

    
122
    public function getParsers()
123
    {
124
        return $this->parsers;
125
    }
126

    
127
    public function getParser()
128
    {
129
        return $this->parser;
130
    }
131

    
132
    public function setParser(Twig_ParserInterface $parser)
133
    {
134
        $this->parser = $parser;
135
        foreach ($this->parsers as $tokenParser) {
136
            $tokenParser->setParser($parser);
137
        }
138
        foreach ($this->brokers as $broker) {
139
            $broker->setParser($parser);
140
        }
141
    }
142
}
(40-40/43)