Skip to content

Commit

Permalink
Added shutdown kernel event (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoreram authored Oct 10, 2020
1 parent eba0af6 commit afe03c4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
11 changes: 11 additions & 0 deletions AsyncHttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions AsyncKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
13 changes: 12 additions & 1 deletion AsyncKernelEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
25 changes: 25 additions & 0 deletions Event/ShutdownEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the DriftPHP Project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/

declare(strict_types=1);

namespace Drift\HttpKernel\Event;

use Symfony\Contracts\EventDispatcher\Event;

/**
* Class TerminateEvent.
*/
class ShutdownEvent extends Event
{
}
5 changes: 1 addition & 4 deletions PeriodicTimer/PeriodicTimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions Tests/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit afe03c4

Please sign in to comment.