Skip to content

Commit

Permalink
Merge pull request #1033 from spiral/feature/listeners-in-booting
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Dec 21, 2023
1 parent bf47a52 commit f51b847
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
33 changes: 13 additions & 20 deletions src/Bootloader/TokenizerListenerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Spiral\Boot\AbstractKernel;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Boot\DirectoriesInterface;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Boot\Memory;
use Spiral\Bootloader\Attributes\AttributesBootloader;
use Spiral\Core\Container\SingletonInterface;
Expand Down Expand Up @@ -56,12 +54,15 @@ public function addListener(TokenizationListenerInterface $listener): void
$this->listeners[] = $listener;
}

public function boot(AbstractKernel $kernel): void
public function init(AbstractKernel $kernel): void
{
$kernel->booting($this->loadClasses(...));
$kernel->booting($this->loadEnums(...));
$kernel->booting($this->loadInterfaces(...));

$kernel->booted($this->loadClasses(...));
$kernel->booted($this->loadEnums(...));
$kernel->booted($this->loadInterfaces(...));
$kernel->booted($this->finalizeListeners(...));
}

public function initCachedClassesLoader(
Expand Down Expand Up @@ -151,37 +152,29 @@ 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 ($listeners as $i => $listener) {
if (call_user_func($loader, $listener)) {
unset($listeners[$i]);
foreach ($this->listeners as $i => $listener) {
if ($loader($listener)) {
$listener->finalize();
unset($this->listeners[$i]);
}
}

// If there are no listeners left, we don't need to use static analysis at all and save
// valuable time.
if ($listeners === []) {
if ($this->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 = call_user_func($reflections);
foreach ($listeners as $listener) {
$classes = $reflections();
foreach ($this->listeners as $i => $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 = [];
}
}
2 changes: 1 addition & 1 deletion tests/Listener/ListenerInvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function testInvokerViaKernel(): void
$bootloader->addListener($listener);

$container->invoke(
[$bootloader, 'boot'],
[$bootloader, 'init'],
['kernel' => $kernel, 'config' => $container->get(TokenizerConfig::class)]
);

Expand Down

0 comments on commit f51b847

Please sign in to comment.