Skip to content

Commit 36fa8f6

Browse files
author
Marco Bunge
committed
Add console events and fix typo
1 parent 2947e09 commit 36fa8f6

3 files changed

Lines changed: 62 additions & 6 deletions

File tree

src/Application.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Hawkbit\Application\Init\InitHaltHookTrait;
1515
use Hawkbit\Application\AbstractApplication;
1616
use Hawkbit\Application\Init\InitConfigurationTrait;
17+
use Hawkbit\Application\MiddlewareAwareInterface;
1718
use Hawkbit\Application\Providers\MonologServiceProvider;
1819
use Hawkbit\Application\Providers\WhoopsServiceProvider;
1920
use Hawkbit\Application\TerminableInterface;
@@ -33,7 +34,8 @@
3334
/**
3435
* Hawkbit Application Class.
3536
*/
36-
final class Application extends AbstractApplication implements RouteCollectionInterface, TerminableInterface
37+
final class Application extends AbstractApplication
38+
implements RouteCollectionInterface, TerminableInterface, MiddlewareAwareInterface
3739
{
3840

3941
use RouteCollectionMapTrait;
@@ -116,7 +118,7 @@ public function init($configuration = [])
116118
/**
117119
* Add a middleware
118120
*
119-
* @param $middleware
121+
* @param callable $middleware
120122
*/
121123
public function addMiddleware(callable $middleware)
122124
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: marco.bunge
5+
* Date: 05.02.2017
6+
* Time: 12:19
7+
*/
8+
9+
namespace Hawkbit\Application;
10+
11+
12+
interface MiddlewareAwareInterface
13+
{
14+
15+
/**
16+
* Add a middleware
17+
*
18+
* @param callable $middleware
19+
*/
20+
public function addMiddleware(callable $middleware);
21+
22+
/**
23+
* @return callable[]
24+
*/
25+
public function getMiddlewares();
26+
}

src/Console.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace Hawkbit;
1010

1111
use Hawkbit\Application\Init\InitHaltHookTrait;
12+
use Hawkbit\Application\MiddlewareAwareInterface;
13+
use Hawkbit\Application\MiddlewareRunner;
1214
use Hawkbit\Console\ConsoleEvent;
1315
use Hawkbit\Application\AbstractApplication;
1416
use Hawkbit\Application\Init\InitConfigurationTrait;
@@ -24,7 +26,7 @@
2426
*
2527
* @todo add event handling equals to application
2628
*/
27-
final class Console extends AbstractApplication
29+
final class Console extends AbstractApplication implements MiddlewareAwareInterface
2830
{
2931

3032
use InitConfigurationTrait;
@@ -44,6 +46,11 @@ final class Console extends AbstractApplication
4446
*/
4547
protected $applicationEventClass = ConsoleEvent::class;
4648

49+
/**
50+
* @var callable[]
51+
*/
52+
protected $middlewares = [];
53+
4754
/**
4855
* New Application.
4956
*
@@ -116,7 +123,6 @@ public function map($name, $callback, array $arguments = [])
116123
*/
117124
public function handle(array $args = [])
118125
{
119-
120126
// remove source file name from argv
121127
$source = array_shift($args);
122128

@@ -130,8 +136,12 @@ public function handle(array $args = [])
130136
// init dispatcher
131137
$dispatcher = new Dispatcher($this->commands, $this->container);
132138

133-
// dispatch command with args from cli
134-
$dispatcher->dispatch($applicationEvent->getArguments());
139+
$middlewareRunner = new MiddlewareRunner($this->getMiddlewares());
140+
141+
$middlewareRunner->run([$applicationEvent->getArguments()], function ($args) use ($applicationEvent, $dispatcher) {
142+
// dispatch command with args from cli
143+
$dispatcher->dispatch($args);
144+
});
135145
}
136146

137147
/**
@@ -161,4 +171,22 @@ public function hasCommand($command)
161171
{
162172
return isset($this->commands[$command]);
163173
}
174+
175+
/**
176+
* Add a middleware
177+
*
178+
* @param callable $middleware
179+
*/
180+
public function addMiddleware(callable $middleware)
181+
{
182+
$this->middlewares[] = $this->bindClosureToInstance($middleware, $this);
183+
}
184+
185+
/**
186+
* @return callable[]
187+
*/
188+
public function getMiddlewares()
189+
{
190+
return $this->middlewares;
191+
}
164192
}

0 commit comments

Comments
 (0)