StopwatchExtension.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Extension;
  11. use Symfony\Component\Stopwatch\Stopwatch;
  12. use Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser;
  13. use Twig\Extension\AbstractExtension;
  14. /**
  15. * Twig extension for the stopwatch helper.
  16. *
  17. * @author Wouter J <wouter@wouterj.nl>
  18. */
  19. class StopwatchExtension extends AbstractExtension
  20. {
  21. private $stopwatch;
  22. /**
  23. * @var bool
  24. */
  25. private $enabled;
  26. public function __construct(Stopwatch $stopwatch = null, $enabled = true)
  27. {
  28. $this->stopwatch = $stopwatch;
  29. $this->enabled = $enabled;
  30. }
  31. public function getStopwatch()
  32. {
  33. return $this->stopwatch;
  34. }
  35. public function getTokenParsers()
  36. {
  37. return array(
  38. /*
  39. * {% stopwatch foo %}
  40. * Some stuff which will be recorded on the timeline
  41. * {% endstopwatch %}
  42. */
  43. new StopwatchTokenParser($this->stopwatch !== null && $this->enabled),
  44. );
  45. }
  46. public function getName()
  47. {
  48. return 'stopwatch';
  49. }
  50. }