diff --git a/AsyncHttpKernel.php b/AsyncHttpKernel.php index e4ddf6e..3fccdd4 100644 --- a/AsyncHttpKernel.php +++ b/AsyncHttpKernel.php @@ -16,6 +16,7 @@ namespace Drift\HttpKernel; use Drift\HttpKernel\Event\PreloadEvent; +use Drift\HttpKernel\Event\ShutdownEvent; use Drift\HttpKernel\Exception\AsyncEventDispatcherNeededException; use Exception; use React\Promise\PromiseInterface; @@ -101,6 +102,16 @@ public function preload(): PromiseInterface ->asyncDispatch(new PreloadEvent(), AsyncKernelEvents::PRELOAD); } + /** + * Shutdown kernel. + */ + public function shutdown(): PromiseInterface + { + return $this + ->dispatcher + ->asyncDispatch(new ShutdownEvent(), AsyncKernelEvents::SHUTDOWN); + } + /** * Handles a Request to convert it to a Response. * diff --git a/AsyncKernel.php b/AsyncKernel.php index e590160..f183e4b 100644 --- a/AsyncKernel.php +++ b/AsyncKernel.php @@ -63,6 +63,16 @@ public function preload(): PromiseInterface ->preload(); } + /** + * Shutdown kernel. + */ + public function shutdown(): PromiseInterface + { + return $this + ->getHttpKernel() + ->shutdown(); + } + /** * Handles a Request to convert it to a Response. * diff --git a/AsyncKernelEvents.php b/AsyncKernelEvents.php index ff0c55f..b09f018 100644 --- a/AsyncKernelEvents.php +++ b/AsyncKernelEvents.php @@ -22,9 +22,20 @@ final class AsyncKernelEvents * the first request is handled. * * This event allows you to load services in the dependency injection, to - * preload clients... + * preload clients, and so on. * * @Event("Drift\HttpKernel\Event\PreloadEvent") */ const PRELOAD = 'kernel.preload'; + + /** + * The SHUTDOWN event occurs once the kernel is asked to be closed + * gracefully. + * + * This event allows you to flush elements from memory, close connections + * and so on. + * + * @Event("Drift\HttpKernel\Event\ShutdownEvent") + */ + const SHUTDOWN = 'kernel.shutdown'; } diff --git a/Event/ShutdownEvent.php b/Event/ShutdownEvent.php new file mode 100644 index 0000000..8456058 --- /dev/null +++ b/Event/ShutdownEvent.php @@ -0,0 +1,25 @@ + + */ + +declare(strict_types=1); + +namespace Drift\HttpKernel\Event; + +use Symfony\Contracts\EventDispatcher\Event; + +/** + * Class TerminateEvent. + */ +class ShutdownEvent extends Event +{ +} diff --git a/PeriodicTimer/PeriodicTimer.php b/PeriodicTimer/PeriodicTimer.php index 7c94ea1..bd2b13e 100644 --- a/PeriodicTimer/PeriodicTimer.php +++ b/PeriodicTimer/PeriodicTimer.php @@ -52,10 +52,7 @@ public function addServiceCall( }); } - /** - * - */ - function onKernelPreload() + public function onKernelPreload() { // Just an empty method to be called. // The main goal are the previous calls diff --git a/Tests/Listener.php b/Tests/Listener.php index 5115286..1a63916 100644 --- a/Tests/Listener.php +++ b/Tests/Listener.php @@ -17,6 +17,7 @@ use Drift\HttpKernel\Event\DomainEventEnvelope; use Drift\HttpKernel\Event\PreloadEvent; +use Drift\HttpKernel\Event\ShutdownEvent; use Drift\HttpKernel\Tests\Event\Event1; use React\Promise\PromiseInterface; use function React\Promise\resolve; @@ -169,6 +170,16 @@ public function handlePreload(PreloadEvent $event) $_GET['preloaded'] = true; } + /** + * Handle shutdown. + * + * @param ShutdownEvent $event + */ + public function handleShutdown(ShutdownEvent $event) + { + $_GET['shutdown'] = true; + } + /** * Handle event1. *