1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-22 12:25:59 +01:00
vichan/inc/lib/Twig/TokenParserBroker.php

121 lines
3.5 KiB
PHP
Raw Normal View History

2011-10-05 06:22:53 +02:00
<?php
/*
* This file is part of Twig.
*
2018-05-10 12:24:53 +02:00
* (c) Fabien Potencier
* (c) Arnaud Le Blanc
2011-10-05 06:22:53 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Default implementation of a token parser broker.
*
2013-08-01 21:20:12 +02:00
* @author Arnaud Le Blanc <arnaud.lb@gmail.com>
2018-05-10 12:24:53 +02:00
*
2013-08-01 21:20:12 +02:00
* @deprecated since 1.12 (to be removed in 2.0)
2011-10-05 06:22:53 +02:00
*/
class Twig_TokenParserBroker implements Twig_TokenParserBrokerInterface
{
protected $parser;
protected $parsers = array();
protected $brokers = array();
/**
2018-05-10 12:24:53 +02:00
* @param array|Traversable $parsers A Traversable of Twig_TokenParserInterface instances
* @param array|Traversable $brokers A Traversable of Twig_TokenParserBrokerInterface instances
* @param bool $triggerDeprecationError
2011-10-05 06:22:53 +02:00
*/
2018-05-10 12:24:53 +02:00
public function __construct($parsers = array(), $brokers = array(), $triggerDeprecationError = true)
2011-10-05 06:22:53 +02:00
{
2018-05-10 12:24:53 +02:00
if ($triggerDeprecationError) {
@trigger_error('The '.__CLASS__.' class is deprecated since version 1.12 and will be removed in 2.0.', E_USER_DEPRECATED);
}
2011-10-05 06:22:53 +02:00
foreach ($parsers as $parser) {
if (!$parser instanceof Twig_TokenParserInterface) {
2018-05-10 12:24:53 +02:00
throw new LogicException('$parsers must a an array of Twig_TokenParserInterface.');
2011-10-05 06:22:53 +02:00
}
$this->parsers[$parser->getTag()] = $parser;
}
foreach ($brokers as $broker) {
if (!$broker instanceof Twig_TokenParserBrokerInterface) {
2018-05-10 12:24:53 +02:00
throw new LogicException('$brokers must a an array of Twig_TokenParserBrokerInterface.');
2011-10-05 06:22:53 +02:00
}
$this->brokers[] = $broker;
}
}
public function addTokenParser(Twig_TokenParserInterface $parser)
{
$this->parsers[$parser->getTag()] = $parser;
}
2013-08-01 21:20:12 +02:00
public function removeTokenParser(Twig_TokenParserInterface $parser)
{
$name = $parser->getTag();
if (isset($this->parsers[$name]) && $parser === $this->parsers[$name]) {
unset($this->parsers[$name]);
}
}
2018-05-10 12:24:53 +02:00
public function addTokenParserBroker(self $broker)
2011-10-05 06:22:53 +02:00
{
$this->brokers[] = $broker;
}
2018-05-10 12:24:53 +02:00
public function removeTokenParserBroker(self $broker)
2013-08-01 21:20:12 +02:00
{
if (false !== $pos = array_search($broker, $this->brokers)) {
unset($this->brokers[$pos]);
}
}
2011-10-05 06:22:53 +02:00
/**
* Gets a suitable TokenParser for a tag.
*
* First looks in parsers, then in brokers.
*
* @param string $tag A tag name
*
* @return null|Twig_TokenParserInterface A Twig_TokenParserInterface or null if no suitable TokenParser was found
*/
public function getTokenParser($tag)
{
if (isset($this->parsers[$tag])) {
return $this->parsers[$tag];
}
$broker = end($this->brokers);
while (false !== $broker) {
$parser = $broker->getTokenParser($tag);
if (null !== $parser) {
return $parser;
}
$broker = prev($this->brokers);
}
2013-08-01 21:20:12 +02:00
}
public function getParsers()
{
return $this->parsers;
2011-10-05 06:22:53 +02:00
}
public function getParser()
{
return $this->parser;
}
public function setParser(Twig_ParserInterface $parser)
{
$this->parser = $parser;
foreach ($this->parsers as $tokenParser) {
$tokenParser->setParser($parser);
}
foreach ($this->brokers as $broker) {
$broker->setParser($parser);
}
}
}