1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 * (c) 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
13use Twig\TokenParser\TokenParserInterface;
14
15/**
16 * Default implementation of a token parser broker.
17 *
18 * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
19 *
20 * @deprecated since 1.12 (to be removed in 2.0)
21 */
22class Twig_TokenParserBroker implements Twig_TokenParserBrokerInterface
23{
24    protected $parser;
25    protected $parsers = [];
26    protected $brokers = [];
27
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 = [], $brokers = [], $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 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    public function addTokenParser(TokenParserInterface $parser)
54    {
55        $this->parsers[$parser->getTag()] = $parser;
56    }
57
58    public function removeTokenParser(TokenParserInterface $parser)
59    {
60        $name = $parser->getTag();
61        if (isset($this->parsers[$name]) && $parser === $this->parsers[$name]) {
62            unset($this->parsers[$name]);
63        }
64    }
65
66    public function addTokenParserBroker(self $broker)
67    {
68        $this->brokers[] = $broker;
69    }
70
71    public function removeTokenParserBroker(self $broker)
72    {
73        if (false !== $pos = array_search($broker, $this->brokers)) {
74            unset($this->brokers[$pos]);
75        }
76    }
77
78    /**
79     * Gets a suitable TokenParser for a tag.
80     *
81     * First looks in parsers, then in brokers.
82     *
83     * @param string $tag A tag name
84     *
85     * @return TokenParserInterface|null A Twig_TokenParserInterface or null if no suitable TokenParser was found
86     */
87    public function getTokenParser($tag)
88    {
89        if (isset($this->parsers[$tag])) {
90            return $this->parsers[$tag];
91        }
92        $broker = end($this->brokers);
93        while (false !== $broker) {
94            $parser = $broker->getTokenParser($tag);
95            if (null !== $parser) {
96                return $parser;
97            }
98            $broker = prev($this->brokers);
99        }
100    }
101
102    public function getParsers()
103    {
104        return $this->parsers;
105    }
106
107    public function getParser()
108    {
109        return $this->parser;
110    }
111
112    public function setParser(Twig_ParserInterface $parser)
113    {
114        $this->parser = $parser;
115        foreach ($this->parsers as $tokenParser) {
116            $tokenParser->setParser($parser);
117        }
118        foreach ($this->brokers as $broker) {
119            $broker->setParser($parser);
120        }
121    }
122}
123