Skip to content

Commit

Permalink
Add EventMiddleware and apply event hook in CommandRunner.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Feb 5, 2018
1 parent 22f74cf commit 970ea1e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Console/CommandRunner.php
Expand Up @@ -22,6 +22,7 @@
use Cake\Console\Exception\StopException;
use Cake\Console\Shell;
use Cake\Core\ConsoleApplicationInterface;
use Cake\Event\EventApplicationInterface;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Utility\Inflector;
Expand Down Expand Up @@ -117,6 +118,10 @@ public function setAliases(array $aliases)
public function run(array $argv, ConsoleIo $io = null)
{
$this->app->bootstrap();
if ($this->app instanceof EventApplicationInterface) {
$eventManager = $this->app->events($this->getEventManager());
$this->setEventManager($eventManager);
}

$commands = new CommandCollection([
'version' => VersionCommand::class,
Expand Down
74 changes: 74 additions & 0 deletions src/Event/Middleware/EventMiddleware.php
@@ -0,0 +1,74 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Event\Middleware;

use Cake\Event\EventApplicationInterface;
use Cake\Event\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;

class EventMiddleware
{
/**
* Application instance that implements `event()` hook.
*
* @var \Cake\Event\EventApplicationInterface
*/
protected $app;

/**
* Event dispatcher.
*
* @var \Cake\Event\EventDispatcherInterface
*/
protected $dispatcher;

/**
* Constructor.
*
* @param \Cake\Event\EventApplicationInterface $app Application instance.
* @param \Cake\Event\EventDispatcherInterface|null $dispatcher Event dispatcher. Can be ommited if `$app` implements `\Cake\Event\EventDispatcherInterface`.
* @throws RuntimeException
*/
public function __construct(EventApplicationInterface $app, EventDispatcherInterface $dispatcher = null)
{
$this->app = $app;
if ($dispatcher === null) {
if ($app instanceof EventDispatcherInterface) {
throw new RuntimeException('Event dispatcher has not been provided.');
}
$dispatcher = $app;
}
$this->dispatcher = $dispatcher;
}

/**
* Middleware invoke method.
*
* Executes `events()` callback on a `$dispatcher`'s event manager.
*
* @param \Psr\Http\Message\ServerRequestInterface $request Server request.
* @param \Psr\Http\Message\ResponseInterface $response Response.
* @param callable $next Callback to invoke the next middleware.
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$eventManager = $this->dispatcher->getEventManager();
$eventManager = $this->app->events($eventManager);
$this->dispatcher->setEventManager($eventManager);

return $next($request, $response);
}
}

0 comments on commit 970ea1e

Please sign in to comment.