|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Whoops - php errors for cool kids |
|
4
|
|
|
* @author Filipe Dobreira <http://github.com/filp> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace WhoopsPimple; |
|
8
|
|
|
|
|
9
|
|
|
use Pimple\Container; |
|
10
|
|
|
use Pimple\ServiceProviderInterface; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
13
|
|
|
use Whoops\Handler\PlainTextHandler; |
|
14
|
|
|
use Whoops\Handler\PrettyPageHandler; |
|
15
|
|
|
use Whoops\Run; |
|
16
|
|
|
|
|
17
|
|
|
class WhoopsServiceProvider implements ServiceProviderInterface |
|
18
|
|
|
{ |
|
19
|
|
|
public function register(Container $container) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->registerErrorPageHandler($container); |
|
22
|
|
|
$this->registerExceptionHandler($container); |
|
23
|
|
|
$this->registerWhoops($container); |
|
24
|
|
|
|
|
25
|
|
|
if (set_exception_handler($container['whoops.exception_handler']) !== null) { |
|
26
|
|
|
restore_exception_handler(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$container['whoops']->register(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function registerWhoops(Container $container) |
|
33
|
|
|
{ |
|
34
|
|
|
$container['whoops'] = function (Container $container) { |
|
35
|
|
|
$run = new Run; |
|
36
|
|
|
$run->allowQuit(false); |
|
37
|
|
|
$run->pushHandler($container['whoops.error_page_handler']); |
|
38
|
|
|
return $run; |
|
39
|
|
|
}; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function registerErrorPageHandler(Container $container) |
|
43
|
|
|
{ |
|
44
|
|
|
$container['whoops.error_page_handler'] = function () { |
|
45
|
|
|
if (PHP_SAPI === 'cli') { |
|
46
|
|
|
return new PlainTextHandler; |
|
47
|
|
|
} else { |
|
48
|
|
|
return new PrettyPageHandler; |
|
49
|
|
|
} |
|
50
|
|
|
}; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function registerExceptionHandler(Container $container) |
|
54
|
|
|
{ |
|
55
|
|
|
$container['whoops.exception_handler'] = $container->protect(function ($e) use ($container) { |
|
56
|
|
|
$method = Run::EXCEPTION_HANDLER; |
|
57
|
|
|
ob_start(); |
|
58
|
|
|
$container['whoops']->$method($e); |
|
59
|
|
|
$response = ob_get_clean(); |
|
60
|
|
|
$code = $e instanceof HttpException ? $e->getStatusCode() : 500; |
|
61
|
|
|
return new Response($response, $code); |
|
62
|
|
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|