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 class StopwatchExtension extends \Twig_Extension
{ {
private $stopwatch; private $stopwatch;
public function __construct(Stopwatch $stopwatch = null) public function __construct(Stopwatch $stopwatch = null)
{ {
$this->stopwatch = $stopwatch; $this->stopwatch = $stopwatch;
} }
public function getTokenParsers() public function getTokenParsers()
{ {
return array( return array(
@ -39,19 +39,19 @@ class StopwatchExtension extends \Twig_Extension
new StopwatchTokenParser($this->stopwatch !== null), new StopwatchTokenParser($this->stopwatch !== null),
); );
} }
public function getName() public function getName()
{ {
return 'stopwatch'; return 'stopwatch';
} }
public function startEvent($name) public function startEvent($name)
{ {
if ($this->stopwatch instanceof Stopwatch) { if ($this->stopwatch instanceof Stopwatch) {
$this->stopwatch->start($name, 'template'); $this->stopwatch->start($name, 'template');
} }
} }
public function stopEvent($name) public function stopEvent($name)
{ {
if ($this->stopwatch instanceof Stopwatch) { 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); parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag);
} }
public function compile(\Twig_Compiler $compiler) public function compile(\Twig_Compiler $compiler)
{ {
$name = $this->getAttribute('name'); $name = $this->getAttribute('name');
$compiler $compiler
->write('$this->env->getExtension(\'stopwatch\')->startEvent(\''.$name.'\');') ->write('$this->env->getExtension(\'stopwatch\')->startEvent(\''.$name.'\');')
->subcompile($this->getNode('body')) ->subcompile($this->getNode('body'))

View File

@ -62,10 +62,8 @@ class StopwatchExtensionTest extends TestCase
return array( return array(
array('{% stopwatch foo %}something{% endstopwatch %}', 'foo'), array('{% stopwatch foo %}something{% endstopwatch %}', 'foo'),
array('{% stopwatch foo %}symfony2 is fun{% endstopwatch %}{% stopwatch bar %}something{% endstopwatch %}', array('foo', 'bar')), array('{% stopwatch foo %}symfony2 is fun{% endstopwatch %}{% stopwatch bar %}something{% endstopwatch %}', array('foo', 'bar')),
array('{% stopwatch foo %}something{% endstopwatch foo %}', 'foo'), 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(); $lineno = $token->getLine();
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
// {% stopwatch bar %} // {% stopwatch bar %}
if ($stream->test(\Twig_Token::NAME_TYPE)) { if ($stream->test(\Twig_Token::NAME_TYPE)) {
$name = $stream->expect(\Twig_Token::NAME_TYPE)->getValue(); $name = $stream->expect(\Twig_Token::NAME_TYPE)->getValue();
@ -41,35 +41,35 @@ class StopwatchTokenParser extends \Twig_TokenParser
} }
if (in_array($name, $this->_events)) { 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; $this->_events[] = $name;
$stream->expect(\Twig_Token::BLOCK_END_TYPE); $stream->expect(\Twig_Token::BLOCK_END_TYPE);
// {% endstopwatch %} or {% endstopwatch bar %} // {% endstopwatch %} or {% endstopwatch bar %}
$body = $this->parser->subparse(array($this, 'decideStopwatchEnd'), true); $body = $this->parser->subparse(array($this, 'decideStopwatchEnd'), true);
if ($stream->test(\Twig_Token::NAME_TYPE) || $stream->test(\Twig_Token::STRING_TYPE)) { if ($stream->test(\Twig_Token::NAME_TYPE) || $stream->test(\Twig_Token::STRING_TYPE)) {
$value = $stream->next()->getValue(); $value = $stream->next()->getValue();
if ($name != $value) { 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); $stream->expect(\Twig_Token::BLOCK_END_TYPE);
if ($this->stopwatchIsAvailable) { if ($this->stopwatchIsAvailable) {
return new StopwatchNode($name, $body, $lineno, $this->getTag()); return new StopwatchNode($name, $body, $lineno, $this->getTag());
} }
return $body; return $body;
} }
public function decideStopwatchEnd(\Twig_Token $token) public function decideStopwatchEnd(\Twig_Token $token)
{ {
return $token->test('endstopwatch'); return $token->test('endstopwatch');
} }
public function getTag() public function getTag()
{ {
return 'stopwatch'; return 'stopwatch';