composer package updates
[openemr.git] / vendor / twig / twig / lib / Twig / TokenParserBroker.php
blob0d7d6e52a84400918b4c33942ff1aa6016d158ce
1 <?php
3 /*
4 * This file is part of Twig.
6 * (c) Fabien Potencier
7 * (c) Arnaud Le Blanc
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
13 /**
14 * Default implementation of a token parser broker.
16 * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
18 * @deprecated since 1.12 (to be removed in 2.0)
20 class Twig_TokenParserBroker implements Twig_TokenParserBrokerInterface
22 protected $parser;
23 protected $parsers = array();
24 protected $brokers = array();
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
31 public function __construct($parsers = array(), $brokers = array(), $triggerDeprecationError = true)
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);
37 foreach ($parsers as $parser) {
38 if (!$parser instanceof Twig_TokenParserInterface) {
39 throw new LogicException('$parsers must a an array of Twig_TokenParserInterface.');
41 $this->parsers[$parser->getTag()] = $parser;
43 foreach ($brokers as $broker) {
44 if (!$broker instanceof Twig_TokenParserBrokerInterface) {
45 throw new LogicException('$brokers must a an array of Twig_TokenParserBrokerInterface.');
47 $this->brokers[] = $broker;
51 public function addTokenParser(Twig_TokenParserInterface $parser)
53 $this->parsers[$parser->getTag()] = $parser;
56 public function removeTokenParser(Twig_TokenParserInterface $parser)
58 $name = $parser->getTag();
59 if (isset($this->parsers[$name]) && $parser === $this->parsers[$name]) {
60 unset($this->parsers[$name]);
64 public function addTokenParserBroker(self $broker)
66 $this->brokers[] = $broker;
69 public function removeTokenParserBroker(self $broker)
71 if (false !== $pos = array_search($broker, $this->brokers)) {
72 unset($this->brokers[$pos]);
76 /**
77 * Gets a suitable TokenParser for a tag.
79 * First looks in parsers, then in brokers.
81 * @param string $tag A tag name
83 * @return null|Twig_TokenParserInterface A Twig_TokenParserInterface or null if no suitable TokenParser was found
85 public function getTokenParser($tag)
87 if (isset($this->parsers[$tag])) {
88 return $this->parsers[$tag];
90 $broker = end($this->brokers);
91 while (false !== $broker) {
92 $parser = $broker->getTokenParser($tag);
93 if (null !== $parser) {
94 return $parser;
96 $broker = prev($this->brokers);
100 public function getParsers()
102 return $this->parsers;
105 public function getParser()
107 return $this->parser;
110 public function setParser(Twig_ParserInterface $parser)
112 $this->parser = $parser;
113 foreach ($this->parsers as $tokenParser) {
114 $tokenParser->setParser($parser);
116 foreach ($this->brokers as $broker) {
117 $broker->setParser($parser);