Default implementation of a token parser broker.
@package twig @author Arnaud Le Blanc <arnaud.lb@gmail.com>
Expanded class hierarchy of Twig_TokenParserBroker
class Twig_TokenParserBroker implements Twig_TokenParserBrokerInterface {
protected $parser;
protected $parsers = array();
protected $brokers = array();
/**
* Constructor.
*
* @param array|Traversable $parsers A Traversable of Twig_TokenParserInterface instances
* @param array|Traversable $brokers A Traversable of Twig_TokenParserBrokerInterface instances
*/
public function __construct($parsers = array(), $brokers = array()) {
foreach ($parsers as $parser) {
if (!$parser instanceof Twig_TokenParserInterface) {
throw new Twig_Error('$parsers must a an array of Twig_TokenParserInterface');
}
$this->parsers[$parser
->getTag()] = $parser;
}
foreach ($brokers as $broker) {
if (!$broker instanceof Twig_TokenParserBrokerInterface) {
throw new Twig_Error('$brokers must a an array of Twig_TokenParserBrokerInterface');
}
$this->brokers[] = $broker;
}
}
/**
* Adds a TokenParser.
*
* @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
*/
public function addTokenParser(Twig_TokenParserInterface $parser) {
$this->parsers[$parser
->getTag()] = $parser;
}
/**
* Adds a TokenParserBroker.
*
* @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
*/
public function addTokenParserBroker(Twig_TokenParserBroker $broker) {
$this->brokers[] = $broker;
}
/**
* 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);
}
return null;
}
public function getParsers() {
return $this->parsers;
}
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);
}
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Twig_TokenParserBroker:: |
protected | property | ||
Twig_TokenParserBroker:: |
protected | property | ||
Twig_TokenParserBroker:: |
protected | property | ||
Twig_TokenParserBroker:: |
public | function | Adds a TokenParser. | |
Twig_TokenParserBroker:: |
public | function | Adds a TokenParserBroker. | |
Twig_TokenParserBroker:: |
public | function |
Gets the Twig_ParserInterface. Overrides Twig_TokenParserBrokerInterface:: |
|
Twig_TokenParserBroker:: |
public | function | ||
Twig_TokenParserBroker:: |
public | function |
Gets a suitable TokenParser for a tag. Overrides Twig_TokenParserBrokerInterface:: |
|
Twig_TokenParserBroker:: |
public | function |
Calls Twig_TokenParserInterface::setParser on all parsers the implementation knows of. Overrides Twig_TokenParserBrokerInterface:: |
|
Twig_TokenParserBroker:: |
public | function | Constructor. |