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
Ah, sorry. That's a stupid mistake... Thank you guys!
And, @mathiasnoback thanks for your given solution. I'll do some research on it and tweet you when I need your help.
@lsmith77 thanks for your anwsers!
Very nice!
In my opinion the ternary operator should not be used to execute a command, but only to evaluate the value of an expression. So if ($this->stopwatch instanceof Stopwatch) { would be better.
In production you could use a node visitor to replace StopwatchNodes with their Body node, so you won't get any errors there.
Author
@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)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm it doesnt seem to be executing the code enclosed by the stopwatch/endstopwatch. For your questions IMHO it should be in the WebProfilerBundle and in terms of the extension I guess: