From bf01818edc7be15b3dc1c405be52f5b1ddb38f15 Mon Sep 17 00:00:00 2001 From: spiralbot Date: Thu, 28 Dec 2023 09:05:58 +0000 Subject: [PATCH] Merge pull request #1043 from andrey-mokhov/fix-311-tokenizer --- .../TokenizerListenerBootloader.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Bootloader/TokenizerListenerBootloader.php b/src/Bootloader/TokenizerListenerBootloader.php index 28f7a3c..b734121 100644 --- a/src/Bootloader/TokenizerListenerBootloader.php +++ b/src/Bootloader/TokenizerListenerBootloader.php @@ -63,10 +63,12 @@ public function init(AbstractKernel $kernel): void $kernel->booting($this->loadClasses(...)); $kernel->booting($this->loadEnums(...)); $kernel->booting($this->loadInterfaces(...)); + $kernel->booting($this->finalizeListeners(...)); $kernel->booted($this->loadClasses(...)); $kernel->booted($this->loadEnums(...)); $kernel->booted($this->loadInterfaces(...)); + $kernel->booted($this->finalizeListeners(...)); } public function initCachedClassesLoader( @@ -164,18 +166,19 @@ private function loadReflections( callable $reflections, callable $loader, ): void { + $listeners = $this->listeners; + // First, we check if the listener has been cached. If it has, we will load the classes/enums/interfaces // from the cache. - foreach ($this->listeners as $i => $listener) { + foreach ($listeners as $i => $listener) { if ($loader($listener)) { - $listener->finalize(); - unset($this->listeners[$i]); + unset($listeners[$i]); } } // If there are no listeners left, we don't need to use static analysis at all and save // valuable time. - if ($this->listeners === []) { + if ($listeners === []) { return; } @@ -183,10 +186,17 @@ private function loadReflections( // Please note that this is a very expensive operation and should be avoided if possible. // Use #[TargetClass] or #[TargetAttribute] attributes in your listeners to cache the classes/enums/interfaces. $classes = $reflections(); - foreach ($this->listeners as $i => $listener) { + foreach ($listeners as $listener) { $invoker->invoke($listener, $classes); + } + } + + private function finalizeListeners(): void + { + foreach ($this->listeners as $listener) { $listener->finalize(); - unset($this->listeners[$i]); } + // We don't need the listeners anymore, so we will clear them from memory. + $this->listeners = []; } }