Completed
Pull Request — v1 (#422)
by
unknown
03:17
created

WhoopsServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 4
c 1
b 1
f 0
lcom 0
cbo 3
dl 0
loc 64
ccs 0
cts 37
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 58 4
1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
7
namespace Whoops\Provider\Phalcon;
8
9
use Phalcon\DI;
10
use Phalcon\DI\Exception;
11
use Whoops\Handler\JsonResponseHandler;
12
use Whoops\Handler\PrettyPageHandler;
13
use Whoops\Run;
14
15
class WhoopsServiceProvider
16
{
17
    /**
18
     * @param DI $di
19
     */
20
    public function __construct(DI $di = null)
21
    {
22
        if (!$di) {
23
            $di = DI::getDefault();
24
        }
25
26
        // There's only ever going to be one error page...right?
27
        $di->setShared('whoops.pretty_page_handler', function () {
28
            return new PrettyPageHandler();
29
        });
30
31
        // There's only ever going to be one error page...right?
32
        $di->setShared('whoops.json_response_handler', function () {
33
            $jsonHandler = new JsonResponseHandler();
34
            $jsonHandler->onlyForAjaxRequests(true);
35
            return $jsonHandler;
36
        });
37
38
        // Retrieves info on the Phalcon environment and ships it off
39
        // to the PrettyPageHandler's data tables:
40
        // This works by adding a new handler to the stack that runs
41
        // before the error page, retrieving the shared page handler
42
        // instance, and working with it to add new data tables
43
        $phalcon_info_handler = function () use ($di) {
44
            try {
45
                $request = $di['request'];
46
            } catch (Exception $e) {
47
                // This error occurred too early in the application's life
48
                // and the request instance is not yet available.
49
                return;
50
            }
51
52
            // Request info:
53
            $di['whoops.pretty_page_handler']->addDataTable('Phalcon Application (Request)', array(
54
                'URI'         => $request->getScheme().'://'.$request->getServer('HTTP_HOST').$request->getServer('REQUEST_URI'),
55
                'Request URI' => $request->getServer('REQUEST_URI'),
56
                'Path Info'   => $request->getServer('PATH_INFO'),
57
                'Query String' => $request->getServer('QUERY_STRING') ?: '<none>',
58
                'HTTP Method' => $request->getMethod(),
59
                'Script Name' => $request->getServer('SCRIPT_NAME'),
60
                //'Base Path'   => $request->getBasePath(),
61
                //'Base URL'    => $request->getBaseUrl(),
62
                'Scheme'      => $request->getScheme(),
63
                'Port'        => $request->getServer('SERVER_PORT'),
64
                'Host'        => $request->getServerName(),
65
            ));
66
        };
67
68
        $di->setShared('whoops', function () use ($di,$phalcon_info_handler) {
69
            $run = new Run();
70
            $run->pushHandler($di['whoops.pretty_page_handler']);
71
            $run->pushHandler($phalcon_info_handler);
72
            $run->pushHandler($di['whoops.json_response_handler']);
73
            return $run;
74
        });
75
76
        $di['whoops']->register();
77
    }
78
}
79