Skip to content

Commit

Permalink
feat(core): allow defer callbacks to receive parameters from contai…
Browse files Browse the repository at this point in the history
…ner (#798)
  • Loading branch information
innocenzi authored Dec 2, 2024
1 parent d58d028 commit 42262fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Tempest/Core/src/Kernel/FinishDeferredTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@

namespace Tempest\Core\Kernel;

use Tempest\Container\Container;
use Tempest\Core\DeferredTasks;

final readonly class FinishDeferredTasks
{
public function __construct(
private DeferredTasks $deferredTasks,
private Container $container,
) {
}

public function __invoke(): void
{
foreach ($this->deferredTasks->getTasks() as $task) {
$task();
$this->container->invoke($task);
}
}
}
19 changes: 18 additions & 1 deletion tests/Integration/Core/DeferredTasksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Tests\Tempest\Integration\Core;

use Tempest\Container\Container;
use Tempest\Core\Kernel\FinishDeferredTasks;
use function Tempest\defer;
use function Tempest\uri;
use Tests\Tempest\Fixtures\Controllers\DeferController;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
Expand All @@ -22,8 +24,23 @@ public function test_deferred_tasks_are_executed(): void
->get(uri(DeferController::class))
->assertOk();

$this->container->get(FinishDeferredTasks::class)();
$this->container->invoke(FinishDeferredTasks::class);

$this->assertTrue(DeferController::$executed);
}

public function test_deferred_tasks_are_executed_with_container_parameters(): void
{
$executed = false;

defer(function (Container $container) use (&$executed): void {
$container->invoke(function () use (&$executed): void {
$executed = true;
});
});

$this->container->invoke(FinishDeferredTasks::class);

$this->assertTrue($executed);
}
}

0 comments on commit 42262fa

Please sign in to comment.