ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
WhoopsServiceProvider.php
Go to the documentation of this file.
1<?php
8
9use RuntimeException;
10use Silex\Application;
11use Silex\ServiceProviderInterface;
12use Symfony\Component\HttpFoundation\Request;
13use Symfony\Component\HttpFoundation\Response;
14use Symfony\Component\HttpKernel\Exception\HttpException;
18use Whoops\Run;
19
20class WhoopsServiceProvider implements ServiceProviderInterface
21{
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 {
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
52 $errorPageHandler = $app["whoops.error_page_handler"];
53
54 if ($errorPageHandler instanceof PrettyPageHandler) {
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
108 public function boot(Application $app)
109 {
110 }
111}
Abstract implementation of a Handler.
Definition: Handler.php:17
Handler outputing plaintext error messages.
const EXCEPTION_HANDLER
Definition: Run.php:19
$code
Definition: example_050.php:99
Whoops - php errors for cool kids.