This commit is contained in:
Fabien Potencier 2013-08-11 20:06:26 +02:00
parent f39ed5706d
commit bbad387e4b
4 changed files with 15 additions and 17 deletions

View File

@ -22,12 +22,12 @@ use Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser;
class StopwatchExtension extends \Twig_Extension
{
private $stopwatch;
public function __construct(Stopwatch $stopwatch = null)
{
$this->stopwatch = $stopwatch;
}
public function getTokenParsers()
{
return array(
@ -39,19 +39,19 @@ class StopwatchExtension extends \Twig_Extension
new StopwatchTokenParser($this->stopwatch !== null),
);
}
public function getName()
{
return 'stopwatch';
}
public function startEvent($name)
{
if ($this->stopwatch instanceof Stopwatch) {
$this->stopwatch->start($name, 'template');
}
}
public function stopEvent($name)
{
if ($this->stopwatch instanceof Stopwatch) {

View File

@ -22,11 +22,11 @@ class StopwatchNode extends \Twig_Node
{
parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag);
}
public function compile(\Twig_Compiler $compiler)
{
$name = $this->getAttribute('name');
$compiler
->write('$this->env->getExtension(\'stopwatch\')->startEvent(\''.$name.'\');')
->subcompile($this->getNode('body'))

View File

@ -62,10 +62,8 @@ class StopwatchExtensionTest extends TestCase
return array(
array('{% stopwatch foo %}something{% endstopwatch %}', 'foo'),
array('{% stopwatch foo %}symfony2 is fun{% endstopwatch %}{% stopwatch bar %}something{% endstopwatch %}', array('foo', 'bar')),
array('{% stopwatch foo %}something{% endstopwatch foo %}', 'foo'),
array("{% stopwatch 'foo.bar' %}something{% endstopwatch %}", 'foo.bar'),
array('{% stopwatch "foo.bar" %}something{% endstopwatch %}', 'foo.bar'),
);
}

View File

@ -32,7 +32,7 @@ class StopwatchTokenParser extends \Twig_TokenParser
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
// {% stopwatch bar %}
if ($stream->test(\Twig_Token::NAME_TYPE)) {
$name = $stream->expect(\Twig_Token::NAME_TYPE)->getValue();
@ -41,35 +41,35 @@ class StopwatchTokenParser extends \Twig_TokenParser
}
if (in_array($name, $this->_events)) {
throw new \Twig_Error_Syntax(sprintf("The stopwatch event '%s' has already been defined", $name));
throw new \Twig_Error_Syntax(sprintf('The stopwatch event "%s" has already been defined.', $name));
}
$this->_events[] = $name;
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
// {% endstopwatch %} or {% endstopwatch bar %}
$body = $this->parser->subparse(array($this, 'decideStopwatchEnd'), true);
if ($stream->test(\Twig_Token::NAME_TYPE) || $stream->test(\Twig_Token::STRING_TYPE)) {
$value = $stream->next()->getValue();
if ($name != $value) {
throw new \Twig_Error_Syntax(sprintf("Expected endstopwatch for event '%s' (but %s given)", $name, $value));
throw new \Twig_Error_Syntax(sprintf('Expected endstopwatch for event "%s" (but "%s" given).', $name, $value));
}
}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
if ($this->stopwatchIsAvailable) {
return new StopwatchNode($name, $body, $lineno, $this->getTag());
}
return $body;
}
public function decideStopwatchEnd(\Twig_Token $token)
{
return $token->test('endstopwatch');
}
public function getTag()
{
return 'stopwatch';