Completed
Push — develop ( e92646...02469b )
by Henry
08:15
created

App::handleRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * This file is part of the Divergence package.
4
 *
5
 * (c) Henry Paradiz <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Divergence;
11
12
use Divergence\Routing\Path;
13
use Divergence\Controllers\Main;
0 ignored issues
show
Bug introduced by
The type Divergence\Controllers\Main was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Divergence\Responders\Emitter;
15
use GuzzleHttp\Psr7\ServerRequest;
16
use Divergence\Controllers\SiteRequestHandler;
17
18
class App
19
{
20
    const VERSION = '1.1';
21
    public $ApplicationPath;
22
    public $Config;
23
    public Path $Path;
24
    public \Whoops\Run $whoops;
25
    public static App $App; // instance of this class for reference
26
27
    public function __construct($Path)
28
    {
29
        static::$App = $this;
30
        $this->init($Path);
31
    }
32
33
    public function init($Path)
34
    {
35
        $this->ApplicationPath = $Path;
36
37
        $this->Path = new Path($_SERVER['REQUEST_URI']);
38
39
        $this->Config = $this->config('app');
40
41
        $this->registerErrorHandler();
42
    }
43
44
    public function config($Label)
45
    {
46
        $Config = $this->ApplicationPath . '/config/' . $Label . '.php';
47
        if (!file_exists($Config)) {
48
            throw new \Exception($Config . ' not found in '.static::class.'::config()');
49
        }
50
        return require $Config;
51
    }
52
53
    public function handleRequest()
54
    {
55
        $main = new SiteRequestHandler();
56
        $response = $main->handle(ServerRequest::fromGlobals());
57
        (new Emitter($response))->emit();
58
    }
59
60
    public function registerErrorHandler()
61
    {
62
        // only show errors in dev environment
63
        if ($this->Config['environment'] == 'dev') {
64
            $this->whoops = new \Whoops\Run;
65
66
            $Handler = new \Whoops\Handler\PrettyPageHandler;
67
            $Handler->setPageTitle("Divergence Error");
68
69
            $this->whoops->pushHandler($Handler);
70
            $this->whoops->register();
71
        } else {
72
            error_reporting(0);
73
        }
74
    }
75
}
76