Security update to Drupal 8.4.6
[yaffs-website] / vendor / twig / twig / lib / Twig / TokenParserBroker.php
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
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      * @param array|Traversable $parsers                 A Traversable of Twig_TokenParserInterface instances
28      * @param array|Traversable $brokers                 A Traversable of Twig_TokenParserBrokerInterface instances
29      * @param bool              $triggerDeprecationError
30      */
31     public function __construct($parsers = array(), $brokers = array(), $triggerDeprecationError = true)
32     {
33         if ($triggerDeprecationError) {
34             @trigger_error('The '.__CLASS__.' class is deprecated since version 1.12 and will be removed in 2.0.', E_USER_DEPRECATED);
35         }
36
37         foreach ($parsers as $parser) {
38             if (!$parser instanceof Twig_TokenParserInterface) {
39                 throw new LogicException('$parsers must a an array of Twig_TokenParserInterface.');
40             }
41             $this->parsers[$parser->getTag()] = $parser;
42         }
43         foreach ($brokers as $broker) {
44             if (!$broker instanceof Twig_TokenParserBrokerInterface) {
45                 throw new LogicException('$brokers must a an array of Twig_TokenParserBrokerInterface.');
46             }
47             $this->brokers[] = $broker;
48         }
49     }
50
51     public function addTokenParser(Twig_TokenParserInterface $parser)
52     {
53         $this->parsers[$parser->getTag()] = $parser;
54     }
55
56     public function removeTokenParser(Twig_TokenParserInterface $parser)
57     {
58         $name = $parser->getTag();
59         if (isset($this->parsers[$name]) && $parser === $this->parsers[$name]) {
60             unset($this->parsers[$name]);
61         }
62     }
63
64     public function addTokenParserBroker(self $broker)
65     {
66         $this->brokers[] = $broker;
67     }
68
69     public function removeTokenParserBroker(self $broker)
70     {
71         if (false !== $pos = array_search($broker, $this->brokers)) {
72             unset($this->brokers[$pos]);
73         }
74     }
75
76     /**
77      * Gets a suitable TokenParser for a tag.
78      *
79      * First looks in parsers, then in brokers.
80      *
81      * @param string $tag A tag name
82      *
83      * @return null|Twig_TokenParserInterface A Twig_TokenParserInterface or null if no suitable TokenParser was found
84      */
85     public function getTokenParser($tag)
86     {
87         if (isset($this->parsers[$tag])) {
88             return $this->parsers[$tag];
89         }
90         $broker = end($this->brokers);
91         while (false !== $broker) {
92             $parser = $broker->getTokenParser($tag);
93             if (null !== $parser) {
94                 return $parser;
95             }
96             $broker = prev($this->brokers);
97         }
98     }
99
100     public function getParsers()
101     {
102         return $this->parsers;
103     }
104
105     public function getParser()
106     {
107         return $this->parser;
108     }
109
110     public function setParser(Twig_ParserInterface $parser)
111     {
112         $this->parser = $parser;
113         foreach ($this->parsers as $tokenParser) {
114             $tokenParser->setParser($parser);
115         }
116         foreach ($this->brokers as $broker) {
117             $broker->setParser($parser);
118         }
119     }
120 }