StopwatchExtension.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\Bridge\Twig\TokenParser\StopwatchTokenParser;
  12. use Symfony\Component\Stopwatch\Stopwatch;
  13. use Twig\Extension\AbstractExtension;
  14. use Twig\TokenParser\TokenParserInterface;
  15. /**
  16. * Twig extension for the stopwatch helper.
  17. *
  18. * @author Wouter J <wouter@wouterj.nl>
  19. */
  20. final class StopwatchExtension extends AbstractExtension
  21. {
  22. private $stopwatch;
  23. private $enabled;
  24. public function __construct(Stopwatch $stopwatch = null, bool $enabled = true)
  25. {
  26. $this->stopwatch = $stopwatch;
  27. $this->enabled = $enabled;
  28. }
  29. public function getStopwatch(): Stopwatch
  30. {
  31. return $this->stopwatch;
  32. }
  33. /**
  34. * @return TokenParserInterface[]
  35. */
  36. public function getTokenParsers(): array
  37. {
  38. return [
  39. /*
  40. * {% stopwatch foo %}
  41. * Some stuff which will be recorded on the timeline
  42. * {% endstopwatch %}
  43. */
  44. new StopwatchTokenParser(null !== $this->stopwatch && $this->enabled),
  45. ];
  46. }
  47. }