Completed
Push — master ( f2cf60...434979 )
by Park Jong-Hun
02:34
created

DispatcherBootstrap::getSiteConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Bootstrap;
4
5
use Core\Bootstrap\BootstrapInterface;
6
use Core\ControllerDispatcher\Dispatcher;
7
use Zend\Diactoros\Uri;
8
use Zend\Diactoros\ServerRequestFactory;
9
10
class DispatcherBootstrap implements BootstrapInterface
11
{
12
    public function boot(array $env)
13
    {
14
        $dispatcher = new Dispatcher();
15
16
        $dispatcher->setRequest($this->getServerRequest($env['site']['url']));
17
        $dispatcher->setRouterConfig($env['router']);
18
        $dispatcher->setViewEngineConfig($env['viewEngine']);
19
        $dispatcher->setViewResolver($env['viewResolver']);
20
21
        $dispatcher->dispatch();
22
    }
23
24
    private function getServerRequest($appUrl)
25
    {
26
        $request = ServerRequestFactory::fromGlobals();
27
28
        $stripedUri = new Uri(
29
            $this->stripAppUrlPrefix($request->getUri()->getPath(), $appUrl)
30
        );
31
32
        return $request->withUri($stripedUri);
33
    }
34
35
    private function stripAppUrlPrefix($url, $appUrl)
36
    {
37
        if (substr($url, 0, strlen($appUrl)) === $appUrl) {
38
            return '/' . substr($url, strlen($appUrl)) ?: '/';
39
        }
40
41
        return $url;
42
    }
43
}
44