ProfilerExtension.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 Twig\Extension\ProfilerExtension as BaseProfilerExtension;
  13. use Twig\Profiler\Profile;
  14. /**
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. final class ProfilerExtension extends BaseProfilerExtension
  18. {
  19. private $stopwatch;
  20. private $events;
  21. public function __construct(Profile $profile, Stopwatch $stopwatch = null)
  22. {
  23. parent::__construct($profile);
  24. $this->stopwatch = $stopwatch;
  25. $this->events = new \SplObjectStorage();
  26. }
  27. public function enter(Profile $profile): void
  28. {
  29. if ($this->stopwatch && $profile->isTemplate()) {
  30. $this->events[$profile] = $this->stopwatch->start($profile->getName(), 'template');
  31. }
  32. parent::enter($profile);
  33. }
  34. public function leave(Profile $profile): void
  35. {
  36. parent::leave($profile);
  37. if ($this->stopwatch && $profile->isTemplate()) {
  38. $this->events[$profile]->stop();
  39. unset($this->events[$profile]);
  40. }
  41. }
  42. }