ProfilerExtension.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. 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)
  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)
  35. {
  36. parent::leave($profile);
  37. if ($this->stopwatch && $profile->isTemplate()) {
  38. $this->events[$profile]->stop();
  39. unset($this->events[$profile]);
  40. }
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getName()
  46. {
  47. return 'native_profiler';
  48. }
  49. }