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

Bootstrap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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