Last active
December 17, 2015 00:38
-
-
Save wouterj/5521978 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Symfony\Component\Stopwatch\Stopwatch; | |
| class StopwatchExtension extends Twig_Extension | |
| { | |
| private $stopwatch; | |
| public function __construct(Stopwatch $stopwatch = null) | |
| { | |
| $this->stopwatch = $stopwatch; | |
| } | |
| public function getTokenParsers() | |
| { | |
| return array( | |
| /* | |
| * {% stopwatch foo %} | |
| * Some stuff which will be recorded on the timeline | |
| * {% endstopwatch %} | |
| */ | |
| new StopwatchTokenParser(), | |
| ); | |
| } | |
| public function getName() | |
| { | |
| return 'stopwatch'; | |
| } | |
| public function getStopwatch() | |
| { | |
| return $this->stopwatch; | |
| } | |
| public function startEvent($name) | |
| { | |
| null === $this->stopwatch ?: $this->stopwatch->start($name); | |
| } | |
| public function stopEvent($name) | |
| { | |
| null === $this->stopwatch ?: $this->stopwatch->stop($name); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class StopwatchNode extends Twig_Node | |
| { | |
| public function __construct($name, $body, $lineno = 0, $tag = null) | |
| { | |
| 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')) | |
| ->write('$this->env->getExtension(\'stopwatch\')->stopEvent(\''.$name.'\');') | |
| ; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class StopwatchTokenParser extends Twig_TokenParser | |
| { | |
| public function parse(Twig_Token $token) | |
| { | |
| $lineno = $token->getLine(); | |
| $stream = $this->parser->getStream(); | |
| // {% stopwatch bar %} | |
| $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); | |
| $stream->expect(Twig_Token::BLOCK_END_TYPE); | |
| // {% endstopwatch %} | |
| $body = $this->parser->subparse(array($this, 'decideStopwatchEnd'), true); | |
| $stream->expect(Twig_Token::BLOCK_END_TYPE); | |
| return new StopwatchNode($name, $body, $lineno, $this->getTag()); | |
| } | |
| public function decideStopwatchEnd(Twig_Token $token) | |
| { | |
| return $token->test('endstopwatch'); | |
| } | |
| public function getTag() | |
| { | |
| return 'stopwatch'; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@matthiasnoback, thank you! I've created a PR ( symfony/symfony#7953 ), I'll see what the stoffs say about the ternary operator (I like it to use it this way)