|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Whoops - php errors for cool kids |
|
4
|
|
|
* @author Filipe Dobreira <http://github.com/filp> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Whoops\Provider\Silex; |
|
8
|
|
|
|
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Silex\Application; |
|
11
|
|
|
use Silex\ServiceProviderInterface; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
15
|
|
|
use Whoops\Handler\Handler; |
|
16
|
|
|
use Whoops\Handler\PlainTextHandler; |
|
17
|
|
|
use Whoops\Handler\PrettyPageHandler; |
|
18
|
|
|
use Whoops\Run; |
|
19
|
|
|
|
|
20
|
|
|
class WhoopsServiceProvider implements ServiceProviderInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param Application $app |
|
24
|
|
|
*/ |
|
25
|
|
|
public function register(Application $app) |
|
26
|
|
|
{ |
|
27
|
|
|
// There's only ever going to be one error page...right? |
|
28
|
|
|
$app['whoops.error_page_handler'] = $app->share(function () { |
|
29
|
|
|
if (PHP_SAPI === 'cli') { |
|
30
|
|
|
return new PlainTextHandler(); |
|
31
|
|
|
} else { |
|
32
|
|
|
return new PrettyPageHandler(); |
|
33
|
|
|
} |
|
34
|
|
|
}); |
|
35
|
|
|
|
|
36
|
|
|
// Retrieves info on the Silex environment and ships it off |
|
37
|
|
|
// to the PrettyPageHandler's data tables: |
|
38
|
|
|
// This works by adding a new handler to the stack that runs |
|
39
|
|
|
// before the error page, retrieving the shared page handler |
|
40
|
|
|
// instance, and working with it to add new data tables |
|
41
|
|
|
$app['whoops.silex_info_handler'] = $app->protect(function () use ($app) { |
|
42
|
|
|
try { |
|
43
|
|
|
/** @var Request $request */ |
|
44
|
|
|
$request = $app['request']; |
|
45
|
|
|
} catch (RuntimeException $e) { |
|
46
|
|
|
// This error occurred too early in the application's life |
|
47
|
|
|
// and the request instance is not yet available. |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** @var Handler $errorPageHandler */ |
|
52
|
|
|
$errorPageHandler = $app["whoops.error_page_handler"]; |
|
53
|
|
|
|
|
54
|
|
|
if ($errorPageHandler instanceof PrettyPageHandler) { |
|
55
|
|
|
/** @var PrettyPageHandler $errorPageHandler */ |
|
56
|
|
|
|
|
57
|
|
|
// General application info: |
|
58
|
|
|
$errorPageHandler->addDataTable('Silex Application', array( |
|
59
|
|
|
'Charset' => $app['charset'], |
|
60
|
|
|
'Locale' => $app['locale'], |
|
61
|
|
|
'Route Class' => $app['route_class'], |
|
62
|
|
|
'Dispatcher Class' => $app['dispatcher_class'], |
|
63
|
|
|
'Application Class' => get_class($app), |
|
64
|
|
|
)); |
|
65
|
|
|
|
|
66
|
|
|
// Request info: |
|
67
|
|
|
$errorPageHandler->addDataTable('Silex Application (Request)', array( |
|
68
|
|
|
'URI' => $request->getUri(), |
|
69
|
|
|
'Request URI' => $request->getRequestUri(), |
|
70
|
|
|
'Path Info' => $request->getPathInfo(), |
|
71
|
|
|
'Query String' => $request->getQueryString() ?: '<none>', |
|
72
|
|
|
'HTTP Method' => $request->getMethod(), |
|
73
|
|
|
'Script Name' => $request->getScriptName(), |
|
74
|
|
|
'Base Path' => $request->getBasePath(), |
|
75
|
|
|
'Base URL' => $request->getBaseUrl(), |
|
76
|
|
|
'Scheme' => $request->getScheme(), |
|
77
|
|
|
'Port' => $request->getPort(), |
|
78
|
|
|
'Host' => $request->getHost(), |
|
79
|
|
|
)); |
|
80
|
|
|
} |
|
81
|
|
|
}); |
|
82
|
|
|
|
|
83
|
|
|
$app['whoops'] = $app->share(function () use ($app) { |
|
84
|
|
|
$run = new Run(); |
|
85
|
|
|
$run->allowQuit(false); |
|
86
|
|
|
$run->pushHandler($app['whoops.error_page_handler']); |
|
87
|
|
|
$run->pushHandler($app['whoops.silex_info_handler']); |
|
88
|
|
|
return $run; |
|
89
|
|
|
}); |
|
90
|
|
|
|
|
91
|
|
|
$app->error(function ($e) use ($app) { |
|
92
|
|
|
$method = Run::EXCEPTION_HANDLER; |
|
93
|
|
|
|
|
94
|
|
|
ob_start(); |
|
95
|
|
|
$app['whoops']->$method($e); |
|
96
|
|
|
$response = ob_get_clean(); |
|
97
|
|
|
$code = $e instanceof HttpException ? $e->getStatusCode() : 500; |
|
98
|
|
|
|
|
99
|
|
|
return new Response($response, $code); |
|
100
|
|
|
}); |
|
101
|
|
|
|
|
102
|
|
|
$app['whoops']->register(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @see Silex\ServiceProviderInterface::boot |
|
107
|
|
|
*/ |
|
108
|
|
|
public function boot(Application $app) |
|
109
|
|
|
{ |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|