diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 6d9076094..cafc725b3 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -756,7 +756,7 @@ class Twig_Environment /** * Gets the registered Token Parsers. * - * @return Twig_TokenParserBrokerInterface A broker containing token parsers + * @return Twig_TokenParserInterface[] An array of Twig_TokenParserInterface */ public function getTokenParsers() { @@ -770,17 +770,13 @@ class Twig_Environment /** * Gets registered tags. * - * Be warned that this method cannot return tags defined by Twig_TokenParserBrokerInterface classes. - * * @return Twig_TokenParserInterface[] An array of Twig_TokenParserInterface instances */ public function getTags() { $tags = array(); - foreach ($this->getTokenParsers()->getParsers() as $parser) { - if ($parser instanceof Twig_TokenParserInterface) { - $tags[$parser->getTag()] = $parser; - } + foreach ($this->getTokenParsers() as $parser) { + $tags[$parser->getTag()] = $parser; } return $tags; @@ -1185,7 +1181,7 @@ class Twig_Environment } $this->extensionInitialized = true; - $this->parsers = new Twig_TokenParserBroker(); + $this->parsers = array(); $this->filters = array(); $this->functions = array(); $this->tests = array(); @@ -1239,13 +1235,11 @@ class Twig_Environment // token parsers foreach ($extension->getTokenParsers() as $parser) { - if ($parser instanceof Twig_TokenParserInterface) { - $this->parsers->addTokenParser($parser); - } elseif ($parser instanceof Twig_TokenParserBrokerInterface) { - $this->parsers->addTokenParserBroker($parser); - } else { - throw new LogicException('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances'); + if (!$parser instanceof Twig_TokenParserInterface) { + throw new LogicException('getTokenParsers() must return an array of Twig_TokenParserInterface'); } + + $this->parsers[] = $parser; } // node visitors diff --git a/lib/Twig/Extension.php b/lib/Twig/Extension.php index 5c8ad5c96..5d3f0bcf7 100644 --- a/lib/Twig/Extension.php +++ b/lib/Twig/Extension.php @@ -24,7 +24,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface /** * Returns the token parser instances to add to the existing list. * - * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances + * @return array An array of Twig_TokenParserInterface instances */ public function getTokenParsers() { diff --git a/lib/Twig/Extension/Escaper.php b/lib/Twig/Extension/Escaper.php index 0edf563ab..46bc55120 100644 --- a/lib/Twig/Extension/Escaper.php +++ b/lib/Twig/Extension/Escaper.php @@ -20,7 +20,7 @@ class Twig_Extension_Escaper extends Twig_Extension /** * Returns the token parser instances to add to the existing list. * - * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances + * @return array An array of Twig_TokenParserInterface instances */ public function getTokenParsers() { diff --git a/lib/Twig/Extension/Sandbox.php b/lib/Twig/Extension/Sandbox.php index c58259c6a..c9e214d30 100644 --- a/lib/Twig/Extension/Sandbox.php +++ b/lib/Twig/Extension/Sandbox.php @@ -23,7 +23,7 @@ class Twig_Extension_Sandbox extends Twig_Extension /** * Returns the token parser instances to add to the existing list. * - * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances + * @return array An array of Twig_TokenParserInterface instances */ public function getTokenParsers() { diff --git a/lib/Twig/ExtensionInterface.php b/lib/Twig/ExtensionInterface.php index 49541b02e..2d5a52b05 100644 --- a/lib/Twig/ExtensionInterface.php +++ b/lib/Twig/ExtensionInterface.php @@ -28,7 +28,7 @@ interface Twig_ExtensionInterface /** * Returns the token parser instances to add to the existing list. * - * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances + * @return array An array of Twig_TokenParserInterface instances */ public function getTokenParsers(); diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 549ce2bdf..332e34b26 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -69,8 +69,12 @@ class Twig_Parser implements Twig_ParserInterface // tag handlers if (null === $this->handlers) { - $this->handlers = $this->env->getTokenParsers(); - $this->handlers->setParser($this); + $this->handlers = array(); + foreach ($this->env->getTokenParsers() as $handler) { + $handler->setParser($this); + + $this->handlers[$handler->getTag()] = $handler; + } } // node visitors @@ -163,8 +167,7 @@ class Twig_Parser implements Twig_ParserInterface return new Twig_Node($rv, array(), $lineno); } - $subparser = $this->handlers->getTokenParser($token->getValue()); - if (null === $subparser) { + if (!isset($this->handlers[$token->getValue()])) { if (null !== $test) { $error = sprintf('Unexpected tag name "%s"', $token->getValue()); if (is_array($test) && isset($test[0]) && $test[0] instanceof Twig_TokenParserInterface) { @@ -184,6 +187,7 @@ class Twig_Parser implements Twig_ParserInterface $this->stream->next(); + $subparser = $this->handlers[$token->getValue()]; $node = $subparser->parse($token); if (null !== $node) { $rv[] = $node; diff --git a/lib/Twig/TokenParserBroker.php b/lib/Twig/TokenParserBroker.php deleted file mode 100644 index ec3fba674..000000000 --- a/lib/Twig/TokenParserBroker.php +++ /dev/null @@ -1,136 +0,0 @@ - - * @deprecated since 1.12 (to be removed in 2.0) - */ -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 LogicException('$parsers must a an array of Twig_TokenParserInterface'); - } - $this->parsers[$parser->getTag()] = $parser; - } - foreach ($brokers as $broker) { - if (!$broker instanceof Twig_TokenParserBrokerInterface) { - throw new LogicException('$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; - } - - /** - * Removes a TokenParser. - * - * @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance - */ - public function removeTokenParser(Twig_TokenParserInterface $parser) - { - $name = $parser->getTag(); - if (isset($this->parsers[$name]) && $parser === $this->parsers[$name]) { - unset($this->parsers[$name]); - } - } - - /** - * Adds a TokenParserBroker. - * - * @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance - */ - public function addTokenParserBroker(Twig_TokenParserBroker $broker) - { - $this->brokers[] = $broker; - } - - /** - * Removes a TokenParserBroker. - * - * @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance - */ - public function removeTokenParserBroker(Twig_TokenParserBroker $broker) - { - if (false !== $pos = array_search($broker, $this->brokers)) { - unset($this->brokers[$pos]); - } - } - - /** - * 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); - } - } - - 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); - } - } -} diff --git a/lib/Twig/TokenParserBrokerInterface.php b/lib/Twig/TokenParserBrokerInterface.php deleted file mode 100644 index 3f006e339..000000000 --- a/lib/Twig/TokenParserBrokerInterface.php +++ /dev/null @@ -1,45 +0,0 @@ - - * @deprecated since 1.12 (to be removed in 2.0) - */ -interface Twig_TokenParserBrokerInterface -{ - /** - * Gets a TokenParser suitable for a tag. - * - * @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); - - /** - * Calls Twig_TokenParserInterface::setParser on all parsers the implementation knows of. - * - * @param Twig_ParserInterface $parser A Twig_ParserInterface interface - */ - public function setParser(Twig_ParserInterface $parser); - - /** - * Gets the Twig_ParserInterface. - * - * @return null|Twig_ParserInterface A Twig_ParserInterface instance or null - */ - public function getParser(); -}