Completed
Push — master ( d935de...f6e733 )
by Park Jong-Hun
04:25
created

Bootstrap   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 54
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A boot() 0 16 3
A getServerRequest() 0 10 1
A stripUrlPrefix() 0 8 3
1
<?php
2
3
namespace Core\Bootstrap;
4
5
use Core\Application;
6
use Zend\Diactoros\ServerRequestFactory;
7
use Zend\Diactoros\Uri;
8
9
class Bootstrap
10
{
11
    /**
12
     * @var Application
13
     */
14
    private $app;
15
16
    private $bootstraps = [];
17
18
    private $appUrl = '';
19
20
    public function __construct()
21
    {
22
        $this->app = Application::getInstance();
23
        $this->bootstraps = require __DIR__ . '/../../config/bootstrap.php';
24
    }
25
26
    public function boot()
27
    {
28
        foreach ($this->bootstraps as $bootstrapClassName) {
29
            /**
30
             * @var BootstrapInterface
31
             */
32
            $bootstrap = new $bootstrapClassName();
33
            $bootstrap->boot($this->app);
34
35
            if ($bootstrap instanceof SiteConfigLoader) {
36
                $this->appUrl = $bootstrap->getSiteConfig()['url'];
37
            }
38
        }
39
40
        $this->app->dispatch($this->getServerRequest());
41
    }
42
43
    private function getServerRequest()
44
    {
45
        $request = ServerRequestFactory::fromGlobals();
46
47
        $stripedUri = new Uri(
48
            $this->stripUrlPrefix($request->getUri()->getPath())
49
        );
50
51
        return $request->withUri($stripedUri);
52
    }
53
54
    private function stripUrlPrefix($url)
55
    {
56
        if (substr($url, 0, strlen($this->appUrl)) === $this->appUrl) {
57
            return '/' . substr($url, strlen($this->appUrl)) ?: '/';
58
        }
59
60
        return $url;
61
    }
62
}
63