Skip to content

Commit e822090

Browse files
author
Marco Bunge
committed
Separate error and shutdown handling into a single trait for reusability
1 parent 39423f9 commit e822090

4 files changed

Lines changed: 77 additions & 35 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
### Altered
4545

4646
- Whoops handlers has been refactored to `\Hawkbit\Application\Services\Whoops\HandlerService`
47-
47+
- Error und shutdown handling has been refactored to `Hawkbit\Application\Init\InitHaltHooksTrait`
48+
4849
### Added
4950

5051
- Add exception stack `Hawkbit\Application\Application\AbtractApplication::getExceptionStack` of all occured exceptions
5152
- Provide backwards / onwards compatibility for PHP 7 `\Throwables`
5253

54+
5355
### Altered
5456

5557
- Update [League Router](https://github.com/thephpleague/route/tree/507606b53d3935e7830aa7c48c43337bc2b1b2ba) and use router middleware implementation instead of application middleware.

src/Application.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Hawkbit;
1313

14+
use Application\Init\InitHaltHookTrait;
1415
use Hawkbit\Application\AbstractApplication;
1516
use Hawkbit\Application\Init\InitConfigurationTrait;
1617
use Hawkbit\Application\Providers\MonologServiceProvider;
@@ -37,6 +38,7 @@ final class Application extends AbstractApplication implements RouteCollectionIn
3738

3839
use RouteCollectionMapTrait;
3940
use InitConfigurationTrait;
41+
use InitHaltHookTrait;
4042

4143
/**
4244
* @var \League\Route\RouteCollection
@@ -102,7 +104,7 @@ public function init($configuration = [])
102104
{
103105
$this->initConfiguration($configuration);
104106
$this->initContentType();
105-
$this->initSystem();
107+
$this->initHaltHooks();
106108
}
107109

108110
/*******************************************
@@ -783,37 +785,4 @@ protected function initContentType()
783785
$this->setContentType(ServerRequestFactory::getHeader('content-type', ServerRequestFactory::fromGlobals()->getHeaders(), $this->getContentType()));
784786
}
785787

786-
/**
787-
*
788-
*/
789-
protected function initSystem(){
790-
// error handler
791-
set_error_handler(function($level, $message, $file = null, $line = null){
792-
$event = $this->getApplicationEvent();
793-
$this->emit($event->setName(self::EVENT_HANDLE_ERROR), $level, $message, $file, $line);
794-
});
795-
796-
// exception handler
797-
set_exception_handler(function($exception){
798-
/** @var \Exception|\Throwable $exception */
799-
// Convert throwable to exception fpr backwards compatibility
800-
if(!($exception instanceof \Exception)){
801-
$throwable = $exception;
802-
$exception = new \ErrorException(
803-
$throwable->getMessage(),
804-
$throwable->getCode(),
805-
E_ERROR,
806-
$throwable->getFile(),
807-
$throwable->getLine()
808-
);
809-
}
810-
811-
$event = $this->getApplicationEvent();
812-
$this->emit($event->setName(self::EVENT_SYSTEM_EXCEPTION), $exception);
813-
});
814-
815-
// shutdown function
816-
register_shutdown_function([$this, 'shutdown']);
817-
}
818-
819788
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: marco.bunge
5+
* Date: 05.02.2017
6+
* Time: 11:43
7+
*/
8+
9+
namespace Application\Init;
10+
11+
12+
use Hawkbit\Application;
13+
use Hawkbit\Console;
14+
15+
trait InitHaltHookTrait
16+
{
17+
18+
/**
19+
* Init all hooks which will be execute on system shutdown or error
20+
*/
21+
protected function initHaltHooks()
22+
{
23+
24+
// error handler
25+
set_error_handler(function ($level, $message, $file = null, $line = null) {
26+
/** @var $this Application|Console */
27+
$event = $this->getApplicationEvent();
28+
$this->emit($event->setName($this::EVENT_HANDLE_ERROR), $level, $message, $file, $line);
29+
});
30+
31+
// exception handler
32+
set_exception_handler(function ($exception) {
33+
/** @var $this Application|Console */
34+
/** @var \Exception|\Throwable $exception */
35+
// Convert throwable to exception for backwards compatibility
36+
if (!($exception instanceof \Exception)) {
37+
$throwable = $exception;
38+
$exception = new \ErrorException(
39+
$throwable->getMessage(),
40+
$throwable->getCode(),
41+
E_ERROR,
42+
$throwable->getFile(),
43+
$throwable->getLine()
44+
);
45+
}
46+
47+
$event = $this->getApplicationEvent();
48+
$this->emit($event->setName($this::EVENT_SYSTEM_EXCEPTION), $exception);
49+
});
50+
51+
if (method_exists($this, 'shutdown')) {
52+
// shutdown function
53+
register_shutdown_function([$this, 'shutdown']);
54+
}
55+
}
56+
}

src/Console/ConsoleEvent.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: marco.bunge
5+
* Date: 05.02.2017
6+
* Time: 11:47
7+
*/
8+
9+
namespace Console;
10+
11+
12+
class ConsoleEvent
13+
{
14+
15+
}

0 commit comments

Comments
 (0)