Skip to content

Commit 2947e09

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

3 files changed

Lines changed: 91 additions & 9 deletions

File tree

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,14 @@ $app->run();
200200

201201
#### Access app from anonymous function
202202

203-
Hawkbit\Application allows to access `Hawkbit\Application\Application` from anonymous function through closure binding.
203+
Hawkbit\Application allows to access itself from anonymous function through closure binding.
204204

205205
```php
206206
<?php
207207

208208
$app->get('/hello/{name}', function ($request, $response, $args) {
209209

210-
// access Hawkbit\Application\Application
210+
// access Hawkbit\Application
211211
$app = $this;
212212

213213
$response->getBody()->write(
@@ -427,6 +427,12 @@ By default Hawkbit\Application is catching all errors. To disable error catching
427427
$app->setConfig('error.catch', false);
428428
```
429429

430+
## Console
431+
432+
The console application inherit all methods from Http Application except routing and PSR-7 handling and capturing.
433+
In addition to http application, the console application does not support all events (Refer to events for more
434+
information!)
435+
430436
## Logging
431437

432438
Hawkbit\Application has built in support for Monolog. To access a channel call:
@@ -446,7 +452,6 @@ ErrorResponse via `Hawkbit\Application\ApplicationEvent`.
446452

447453
### Application event
448454

449-
450455
```php
451456
<?php
452457

@@ -478,6 +483,8 @@ This event is fired when a request is received but before it has been processed
478483

479484
### response.created
480485

486+
*Not available for Console applications!*
487+
481488
```php
482489
<?php
483490

@@ -496,6 +503,8 @@ This event is fired when a response has been created but before it has been outp
496503

497504
### response.sent
498505

506+
*Not available for Console applications! Please use `shutdown`*
507+
499508
```php
500509
<?php
501510

@@ -510,7 +519,7 @@ $app->addListener($app::EVENT_RESPONSE_SENT, function (\Hawkbit\Application\Appl
510519
});
511520
```
512521

513-
This event is fired when a response has been output and before the application lifecycle is completed.
522+
This event is fired when a response has been output and before the application lifecycle is completed. Not available for Console Applications!
514523

515524
### runtime.error
516525

@@ -526,6 +535,8 @@ This event is always fired when an error occurs.
526535

527536
### lifecycle.error
528537

538+
*Not available for Console applications! Please use `runtime.error`*
539+
529540
`$errorResponse` is used as default response
530541

531542
```php
@@ -545,6 +556,8 @@ This event is fired after runtime.error
545556

546557
### lifecycle.complete
547558

559+
*Not available for Console applications! Please use `shutdown`*
560+
548561
```php
549562
<?php
550563

src/Console.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Hawkbit;
1010

11+
use Hawkbit\Application\Init\InitHaltHookTrait;
1112
use Hawkbit\Console\ConsoleEvent;
1213
use Hawkbit\Application\AbstractApplication;
1314
use Hawkbit\Application\Init\InitConfigurationTrait;
@@ -27,10 +28,18 @@ final class Console extends AbstractApplication
2728
{
2829

2930
use InitConfigurationTrait;
31+
use InitHaltHookTrait;
3032

33+
/**
34+
* A list of commands
35+
*
36+
* @var Command[]
37+
*/
3138
private $commands;
3239

3340
/**
41+
* Represents event class for this application
42+
*
3443
* @var string
3544
*/
3645
protected $applicationEventClass = ConsoleEvent::class;
@@ -60,6 +69,11 @@ public function __construct($configuration = [], array $defaultProviders = [
6069
*/
6170
public function shutdown()
6271
{
72+
// dispatch shutdown event
73+
$applicationEvent = $this->getApplicationEvent();
74+
$applicationEvent->setName(self::EVENT_SYSTEM_SHUTDOWN);
75+
$this->emit($applicationEvent);
76+
6377
exit((int)$this->isError());
6478
}
6579

@@ -72,6 +86,7 @@ public function shutdown()
7286
public function init($configuration = [])
7387
{
7488
$this->initConfiguration($configuration);
89+
$this->initHaltHooks();
7590
}
7691

7792
/**
@@ -95,22 +110,28 @@ public function map($name, $callback, array $arguments = [])
95110
}
96111

97112
/**
98-
* ispatch command from given args
113+
* Dispatch command from given args
99114
*
100115
* @param array $args
101116
*/
102117
public function handle(array $args = [])
103118
{
104119

105120
// remove source file name from argv
106-
// @todo reuse as source file or something like that
107-
array_shift($args);
121+
$source = array_shift($args);
122+
123+
/** @var ConsoleEvent $applicationEvent */
124+
$applicationEvent = $this->getApplicationEvent();
125+
$applicationEvent->setName(self::EVENT_REQUEST_RECEIVED);
126+
$applicationEvent->setSourceFile($source);
127+
$applicationEvent->setArguments($args);
128+
$this->emit($applicationEvent);
108129

109130
// init dispatcher
110131
$dispatcher = new Dispatcher($this->commands, $this->container);
111132

112133
// dispatch command with args from cli
113-
$dispatcher->dispatch($args);
134+
$dispatcher->dispatch($applicationEvent->getArguments());
114135
}
115136

116137
/**

src/Console/ConsoleEvent.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,55 @@
99
namespace Hawkbit\Console;
1010

1111

12-
class ConsoleEvent
12+
use Hawkbit\Application\ApplicationEvent;
13+
14+
class ConsoleEvent extends ApplicationEvent
1315
{
1416

17+
/**
18+
* @var array
19+
*/
20+
public $arguments;
21+
22+
/**
23+
* @var string
24+
*/
25+
public $sourceFile;
26+
27+
/**
28+
* @return array
29+
*/
30+
public function getArguments()
31+
{
32+
return $this->arguments;
33+
}
34+
35+
/**
36+
* @param array $arguments
37+
* @return ConsoleEvent
38+
*/
39+
public function setArguments($arguments)
40+
{
41+
$this->arguments = $arguments;
42+
return $this;
43+
}
44+
45+
/**
46+
* @return string
47+
*/
48+
public function getSourceFile()
49+
{
50+
return $this->sourceFile;
51+
}
52+
53+
/**
54+
* @param string $sourceFile
55+
* @return ConsoleEvent
56+
*/
57+
public function setSourceFile($sourceFile)
58+
{
59+
$this->sourceFile = $sourceFile;
60+
return $this;
61+
}
62+
1563
}

0 commit comments

Comments
 (0)