Completed
Push — master ( 23f42c...e6b3d9 )
by Park Jong-Hun
02:39
created

Application::dispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace Core;
4
5
use Prob\Handler\ParameterMap;
6
use Prob\Router\Dispatcher;
7
use Prob\Rewrite\Request;
8
use Prob\Router\Map;
9
use Prob\Router\Matcher;
10
use Doctrine\ORM\Tools\Setup;
11
use Doctrine\ORM\EntityManager;
12
use Core\RouterMapBuilder;
13
14
class Application
15
{
16
    /**
17
     * @var Map
18
     */
19
    private $routerMap;
20
21
    private $routerConfig = [];
22
    private $siteConfig = [];
23
    private $errorReporterConfig = [];
24
    private $viewEngineConfig = [];
25
    private $dbConfig = [];
26
27
    private $errorReporters = [];
0 ignored issues
show
Unused Code introduced by
The property $errorReporters is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
29
    /**
30
     * Singleton: private constructor
31
     */
32
    private function __construct()
33
    {
34
    }
35
36
    /**
37
     * @return Application
38
     */
39
    public static function getInstance()
40
    {
41
        static $instance = null;
42
43
        if ($instance === null) {
44
            $instance = new self();
45
        }
46
47
        return $instance;
48
    }
49
50
    public function setSiteConfig(array $siteConfig)
51
    {
52
        $this->siteConfig = $siteConfig;
53
    }
54
55
    public function setErrorReporterConfig(array $errorReporterConfig)
56
    {
57
        $this->errorReporterConfig = $errorReporterConfig;
58
    }
59
60
    public function setDbConfig(array $dbConfig)
61
    {
62
        $this->dbConfig = $dbConfig;
63
    }
64
65
    public function setViewEngineConfig(array $viewEngineConfig)
66
    {
67
        $this->viewEngineConfig = $viewEngineConfig;
68
    }
69
70
    public function setDisplayError($isDisplay)
71
    {
72
        error_reporting(E_ALL);
73
        ini_set('display_errors', $isDisplay);
74
    }
75
76
    public function registerErrorReporters()
77
    {
78
        $register = new ErrorReporterRegister();
79
        $register->setErrorReporterConfig($this->errorReporterConfig);
80
        $register->setEnabledReporters($this->siteConfig['errorReporters']);
81
        $register->regist();
82
    }
83
84
    public function setRouterConfig(array $routerConfig)
85
    {
86
        $this->routerConfig = $routerConfig;
87
        $this->buildRouterMap();
88
    }
89
90
    private function buildRouterMap()
91
    {
92
        $builder = new RouterMapBuilder();
93
        $builder->setRouterConfig($this->routerConfig);
94
95
        $this->routerMap = $builder->build();
96
    }
97
98
    public function dispatcher(Request $request)
99
    {
100
        $url = $this->resolveUrl($request);
101
        $viewModel = new ViewModel();
102
103
        $parameterMap = new ParameterMap();
104
        $this->bindUrlParameter($parameterMap, $url);
105
        $this->bindViewModelParameter($parameterMap, $viewModel);
106
107
        $returnOfController = $this->executeController($request, $parameterMap);
108
109
        $view = $this->resolveView($returnOfController);
110
        $this->setViewVariables($view, $viewModel->getVariables());
111
        $view->render();
112
    }
113
114
    private function resolveUrl(Request $request)
115
    {
116
        $matcher = new Matcher($this->routerMap);
117
        return $matcher->match($request)['urlNameMatching'] ?: [];
118
    }
119
120
    private function executeController(Request $request, ParameterMap $parameterMap)
121
    {
122
        $dispatcher = new Dispatcher($this->routerMap);
123
        return $dispatcher->dispatch($request, $parameterMap);
124
    }
125
126
    /**
127
     * @param  mixed $returnOfController
128
     * @return View
129
     */
130
    private function resolveView($returnOfController)
131
    {
132
        $viewResolver = new ViewResolver($returnOfController);
133
        return $viewResolver->resolve($this->viewEngineConfig[$this->siteConfig['viewEngine']]);
134
    }
135
136
    private function bindUrlParameter(ParameterMap $map, array $url)
137
    {
138
        $map->bindByNameWithType('array', 'url', $url);
139
140
        foreach ($url as $name => $value) {
141
            $map->bindByName($name, $value);
142
        }
143
    }
144
145
    private function bindViewModelParameter(ParameterMap $map, ViewModel $viewModel)
146
    {
147
        $map->bindByType(ViewModel::class, $viewModel);
148
    }
149
150
    private function setViewVariables(View $view, array $var)
151
    {
152
        foreach ($var as $key => $value) {
153
            $view->set($key, $value);
154
        }
155
    }
156
157
158
    /**
159
     * Return url path with site url
160
     * @param  string $url sub url
161
     * @return string
162
     */
163
    public function url($url = '')
164
    {
165
        return $this->siteConfig['url'] . $url;
166
    }
167
168
169
    /**
170
     * @return EntityManager
171
     */
172
    public function getEntityManager()
173
    {
174
        $config = Setup::createAnnotationMetadataConfiguration(
175
                    $this->dbConfig['entityPath'],
176
                    $this->dbConfig['devMode']
177
                    );
178
        return EntityManager::create($this->dbConfig[$this->siteConfig['database']], $config);
179
    }
180
}
181