|
12 | 12 | namespace Symfony\UX\TwigComponent\EventListener;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
15 |
| -use Symfony\Component\Stopwatch\Stopwatch; |
16 | 15 | use Symfony\Contracts\Service\ResetInterface;
|
17 |
| -use Symfony\UX\TwigComponent\Event\PostMountEvent; |
18 | 16 | use Symfony\UX\TwigComponent\Event\PostRenderEvent;
|
19 |
| -use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent; |
20 |
| -use Symfony\UX\TwigComponent\Event\PreMountEvent; |
21 | 17 | use Symfony\UX\TwigComponent\Event\PreRenderEvent;
|
22 | 18 |
|
23 | 19 | /**
|
24 | 20 | * @author Simon André <[email protected]>
|
| 21 | + * |
| 22 | + * @internal |
25 | 23 | */
|
26 |
| -class TwigComponentLoggerListener implements EventSubscriberInterface, ResetInterface |
| 24 | +final class TwigComponentLoggerListener implements EventSubscriberInterface, ResetInterface |
27 | 25 | {
|
28 | 26 | private array $events = [];
|
29 | 27 |
|
30 |
| - public function __construct(private ?Stopwatch $stopwatch = null) |
31 |
| - { |
32 |
| - } |
33 |
| - |
34 | 28 | public static function getSubscribedEvents(): array
|
35 | 29 | {
|
36 | 30 | return [
|
37 |
| - PreCreateForRenderEvent::class => [ |
38 |
| - // High priority: start the stopwatch as soon as possible |
39 |
| - ['onPreCreateForRender', 255], |
40 |
| - // Low priority: check `event::getRenderedString()` as late as possible |
41 |
| - ['onPostCreateForRender', -255], |
42 |
| - ], |
43 |
| - PreMountEvent::class => ['onPreMount', 255], |
44 |
| - PostMountEvent::class => ['onPostMount', -255], |
45 | 31 | PreRenderEvent::class => ['onPreRender', 255],
|
46 | 32 | PostRenderEvent::class => ['onPostRender', -255],
|
47 | 33 | ];
|
48 | 34 | }
|
49 | 35 |
|
| 36 | + /** |
| 37 | + * @return list<array{ |
| 38 | + * PreRenderEvent|PostRenderEvent, |
| 39 | + * array{float, int}, |
| 40 | + * }> |
| 41 | + */ |
50 | 42 | public function getEvents(): array
|
51 | 43 | {
|
52 | 44 | return $this->events;
|
53 | 45 | }
|
54 | 46 |
|
55 |
| - public function onPreCreateForRender(PreCreateForRenderEvent $event): void |
56 |
| - { |
57 |
| - $this->stopwatch?->start($event->getName(), 'twig_component'); |
58 |
| - $this->logEvent($event); |
59 |
| - } |
60 |
| - |
61 |
| - private function logEvent(object $event): void |
62 |
| - { |
63 |
| - $this->events[] = [$event, [microtime(true), memory_get_usage(true)]]; |
64 |
| - } |
65 |
| - |
66 |
| - public function onPostCreateForRender(PreCreateForRenderEvent $event): void |
67 |
| - { |
68 |
| - if (\is_string($event->getRenderedString())) { |
69 |
| - $this->stopwatch?->stop($event->getName()); |
70 |
| - $this->logEvent($event); |
71 |
| - } |
72 |
| - } |
73 |
| - |
74 |
| - public function onPreMount(PreMountEvent $event): void |
75 |
| - { |
76 |
| - $this->logEvent($event); |
77 |
| - } |
78 |
| - |
79 |
| - public function onPostMount(PostMountEvent $event): void |
80 |
| - { |
81 |
| - $this->logEvent($event); |
82 |
| - } |
83 |
| - |
84 | 47 | public function onPreRender(PreRenderEvent $event): void
|
85 | 48 | {
|
86 | 49 | $this->logEvent($event);
|
87 | 50 | }
|
88 | 51 |
|
89 | 52 | public function onPostRender(PostRenderEvent $event): void
|
90 | 53 | {
|
91 |
| - if ($this->stopwatch?->isStarted($name = $event->getMountedComponent()->getName())) { |
92 |
| - $this->stopwatch->stop($name); |
93 |
| - } |
94 | 54 | $this->logEvent($event);
|
95 | 55 | }
|
96 | 56 |
|
97 | 57 | public function reset(): void
|
98 | 58 | {
|
99 | 59 | $this->events = [];
|
100 | 60 | }
|
| 61 | + |
| 62 | + private function logEvent(object $event): void |
| 63 | + { |
| 64 | + $this->events[] = [$event, [microtime(true), memory_get_usage(true)]]; |
| 65 | + } |
101 | 66 | }
|
0 commit comments