Skip to content

Commit

Permalink
Merge pull request #1043 from andrey-mokhov/fix-311-tokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Dec 28, 2023
1 parent 4eebd04 commit bf01818
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Bootloader/TokenizerListenerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -164,29 +166,37 @@ 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;
}

// If there are listeners left, we will use static analysis to find the classes/enums/interfaces.
// 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 = [];
}
}

0 comments on commit bf01818

Please sign in to comment.