removed deprecated token parser broker

This commit is contained in:
Fabien Potencier
2013-08-05 18:40:42 +02:00
parent 13f8ed7e80
commit 2d659ed5a8
8 changed files with 20 additions and 203 deletions
+8 -14
View File
@@ -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
+1 -1
View File
@@ -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()
{
+1 -1
View File
@@ -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()
{
+1 -1
View File
@@ -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()
{
+1 -1
View File
@@ -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();
+8 -4
View File
@@ -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;
-136
View File
@@ -1,136 +0,0 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2010 Fabien Potencier
* (c) 2010 Arnaud Le Blanc
*
* 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.
*
* @author Arnaud Le Blanc <arnaud.lb@gmail.com>
* @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);
}
}
}
-45
View File
@@ -1,45 +0,0 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2010 Fabien Potencier
* (c) 2010 Arnaud Le Blanc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface implemented by token parser brokers.
*
* Token parser brokers allows to implement custom logic in the process of resolving a token parser for a given tag name.
*
* @author Arnaud Le Blanc <arnaud.lb@gmail.com>
* @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();
}