Completed
Push — master ( bf5e7d...23f42c )
by Park Jong-Hun
03:01
created

Application::resolveView()   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 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
13
class Application
14
{
15
    /**
16
     * @var Map
17
     */
18
    private $routerMap;
19
20
    private $routeConfig = [];
21
    private $siteConfig = [];
22
    private $errorReporterConfig = [];
23
    private $viewEngineConfig = [];
24
    private $dbConfig = [];
25
26
    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...
27
28
    /**
29
     * Singleton: private constructor
30
     */
31
    private function __construct()
32
    {
33
    }
34
35
    /**
36
     * @return Application
37
     */
38
    public static function getInstance()
39
    {
40
        static $instance = null;
41
42
        if ($instance === null) {
43
            $instance = new self();
44
        }
45
46
        return $instance;
47
    }
48
49
    public function setSiteConfig(array $siteConfig)
50
    {
51
        $this->siteConfig = $siteConfig;
52
    }
53
54
    public function setErrorReporterConfig(array $errorReporterConfig)
55
    {
56
        $this->errorReporterConfig = $errorReporterConfig;
57
    }
58
59
    public function setDbConfig(array $dbConfig)
60
    {
61
        $this->dbConfig = $dbConfig;
62
    }
63
64
    public function setViewEngineConfig(array $viewEngineConfig)
65
    {
66
        $this->viewEngineConfig = $viewEngineConfig;
67
    }
68
69
    public function setDisplayError($isDisplay)
70
    {
71
        error_reporting(E_ALL);
72
        ini_set('display_errors', $isDisplay);
73
    }
74
75
    public function registerErrorReporters()
76
    {
77
        $register = new ErrorReporterRegister();
78
        $register->setErrorReporterConfig($this->errorReporterConfig);
79
        $register->setEnabledReporters($this->siteConfig['errorReporters']);
80
        $register->regist();
81
    }
82
83
    public function setRouterConfig(array $routeConfig)
84
    {
85
        $this->routeConfig = $routeConfig;
86
        $this->buildRouterMap();
87
    }
88
89
    private function buildRouterMap()
90
    {
91
        $routerMap = new Map();
92
        $routerMap->setNamespace($this->routeConfig['namespace']);
93
94
        foreach ($this->getRoutePathMap() as $k => $v) {
95
            $this->addRouterMap($routerMap, $k, $v);
96
        }
97
98
        $this->routerMap = $routerMap;
99
    }
100
101
    private function getRoutePathMap()
102
    {
103
        $paths = $this->routeConfig;
104
        unset($paths['namespace']);
105
106
        return $paths;
107
    }
108
109
    /**
110
     * @param Map    $routerMap
111
     * @param string $path  url path
112
     * @param string|array|closure $handler
113
     */
114
    private function addRouterMap(Map $routerMap, $path, $handler)
115
    {
116
        // string | closure
117
        if (gettype($handler) === 'string' || is_callable($handler)) {
118
            $routerMap->get($path, $handler);
119
            return;
120
        }
121
122
        /**
123
         * $handler array schema
124
         * (optional) $handler['GET' | 'POST']
125
         *                => method_name(ex. 'classname.methodname')
126
         *                    or function_name
127
         *                    or closure
128
         */
129
        if (isset($handler['GET'])) {
130
            $routerMap->get($path, $handler['GET']);
131
        }
132
        if (isset($handler['POST'])) {
133
            $routerMap->post($path, $handler['POST']);
134
        }
135
    }
136
137
138
    public function dispatcher(Request $request)
139
    {
140
        $url = $this->resolveUrl($request);
141
        $viewModel = new ViewModel();
142
143
        $parameterMap = new ParameterMap();
144
        $this->bindUrlParameter($parameterMap, $url);
145
        $this->bindViewModelParameter($parameterMap, $viewModel);
146
147
        $returnOfController = $this->executeController($request, $parameterMap);
148
149
        $view = $this->resolveView($returnOfController);
150
        $this->setViewVariables($view, $viewModel->getVariables());
151
        $view->render();
152
    }
153
154
    private function resolveUrl(Request $request)
155
    {
156
        $matcher = new Matcher($this->routerMap);
157
        return $matcher->match($request)['urlNameMatching'] ?: [];
158
    }
159
160
    private function executeController(Request $request, ParameterMap $parameterMap)
161
    {
162
        $dispatcher = new Dispatcher($this->routerMap);
163
        return $dispatcher->dispatch($request, $parameterMap);
164
    }
165
166
    /**
167
     * @param  mixed $returnOfController
168
     * @return View
169
     */
170
    private function resolveView($returnOfController)
171
    {
172
        $viewResolver = new ViewResolver($returnOfController);
173
        return $viewResolver->resolve($this->viewEngineConfig[$this->siteConfig['viewEngine']]);
174
    }
175
176
    private function bindUrlParameter(ParameterMap $map, array $url)
177
    {
178
        $map->bindByNameWithType('array', 'url', $url);
179
180
        foreach ($url as $name => $value) {
181
            $map->bindByName($name, $value);
182
        }
183
    }
184
185
    private function bindViewModelParameter(ParameterMap $map, ViewModel $viewModel)
186
    {
187
        $map->bindByType(ViewModel::class, $viewModel);
188
    }
189
190
    private function setViewVariables(View $view, array $var)
191
    {
192
        foreach ($var as $key => $value) {
193
            $view->set($key, $value);
194
        }
195
    }
196
197
198
    /**
199
     * Return url path with site url
200
     * @param  string $url sub url
201
     * @return string
202
     */
203
    public function url($url = '')
204
    {
205
        return $this->siteConfig['url'] . $url;
206
    }
207
208
209
    /**
210
     * @return EntityManager
211
     */
212
    public function getEntityManager()
213
    {
214
        $config = Setup::createAnnotationMetadataConfiguration(
215
                    $this->dbConfig['entityPath'],
216
                    $this->dbConfig['devMode']
217
                    );
218
        return EntityManager::create($this->dbConfig[$this->siteConfig['database']], $config);
219
    }
220
}
221