Completed
Push — master ( e91368...26d5da )
by Anton
9s
created

Application::postProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Application;
13
14
use Bluz\Application\Exception\ApplicationException;
15
use Bluz\Application\Exception\ForbiddenException;
16
use Bluz\Application\Exception\RedirectException;
17
use Bluz\Common;
18
use Bluz\Controller\Controller;
19
use Bluz\Proxy\Config;
20
use Bluz\Proxy\Layout;
21
use Bluz\Proxy\Logger;
22
use Bluz\Proxy\Messages;
23
use Bluz\Proxy\Request;
24
use Bluz\Proxy\Response;
25
use Bluz\Proxy\Router;
26
use Bluz\Proxy\Session;
27
use Bluz\Proxy\Translator;
28
use Bluz\Request\RequestFactory;
29
use Bluz\Response\Response as ResponseInstance;
30
31
/**
32
 * Application
33
 *
34
 * @package  Bluz\Application
35
 * @link     https://github.com/bluzphp/framework/wiki/Application
36
 * @author   Anton Shevchuk
37
 * @created  06.07.11 16:25
38
 *
39
 * @method Controller error(\Exception $exception)
40
 * @method Controller forbidden(ForbiddenException $exception)
41
 * @method null redirect(string $url)
42
 */
43
class Application
44
{
45
    use Common\Helper;
46
    use Common\Singleton;
47
48
    /**
49
     * @var string Environment name
50
     */
51
    protected $environment = 'production';
52
53
    /**
54
     * @var string Application path
55
     */
56
    protected $path;
57
58
    /**
59
     * @var bool Debug application flag
60
     */
61
    protected $debugFlag = false;
62
63
    /**
64
     * @var bool Layout usage flag
65
     */
66
    protected $layoutFlag = true;
67
68
    /**
69
     * Get application environment
70
     *
71
     * @return string
72
     */
73
    public function getEnvironment()
74
    {
75
        return $this->environment;
76
    }
77
78
    /**
79
     * Get path to Application
80
     *
81
     * @return string
82
     */
83 630
    public function getPath()
84
    {
85 630
        if (!$this->path) {
86
            if (defined('PATH_APPLICATION')) {
87
                $this->path = PATH_APPLICATION;
88
            } else {
89
                $reflection = new \ReflectionClass($this);
90
                // 3 level up
91
                $this->path = dirname(dirname(dirname($reflection->getFileName())));
92
            }
93
        }
94 630
        return $this->path;
95
    }
96
97
    /**
98
     * Return Debug flag
99
     *
100
     * @return bool
101
     */
102 2
    public function isDebug()
103
    {
104 2
        return $this->debugFlag;
105
    }
106
107
    /**
108
     * Return Layout Flag
109
     *
110
     * @param  bool|null $flag
111
     * @return bool
112
     */
113 628
    public function useLayout($flag = null)
114
    {
115 628
        if (is_bool($flag)) {
116 628
            $this->layoutFlag = $flag;
117
        }
118
        
119 628
        return $this->layoutFlag;
120
    }
121
122
    /**
123
     * Initialize process
124
     *
125
     * @param  string $environment
126
     * @throws ApplicationException
127
     * @return void
128
     */
129 1
    public function init($environment = 'production')
130
    {
131 1
        $this->environment = $environment;
132
133
        try {
134
            // first log message
135 1
            Logger::info('app:init');
136
137
            // initial default helper path
138 1
            $this->addHelperPath(dirname(__FILE__) . '/Helper/');
139
140
            // init Config
141 1
            $this->initConfig();
142
143
            // init Session, start inside class (if needed)
144 1
            Session::getInstance();
145
146
            // init Messages
147 1
            Messages::getInstance();
148
149
            // init Translator
150 1
            Translator::getInstance();
151
152
            // init Request
153 1
            $this->initRequest();
154
155
            // init Response
156 1
            $this->initResponse();
157
158
            // init Router
159 1
            $this->initRouter();
160
        } catch (\Exception $e) {
161
            throw new ApplicationException("Application can't be loaded: " . $e->getMessage());
162
        }
163 1
    }
164
165
    /**
166
     * Initial Request instance
167
     *
168
     * @return void
169
     */
170 1
    protected function initConfig()
171
    {
172 1
        Config::getInstance();
173
174
        // setup configuration for current environment
175 1
        if ($debug = Config::getData('debug')) {
176
            $this->debugFlag = (bool) $debug;
177
        }
178
179
        // initial php settings
180 1
        if ($ini = Config::getData('php')) {
181
            foreach ($ini as $key => $value) {
182
                $result = ini_set($key, $value);
183
                Logger::info('app:init:php:'.$key.':'.($result?:'---'));
184
            }
185
        }
186 1
    }
187
188
    /**
189
     * Initial Request instance
190
     *
191
     * @return void
192
     */
193 1
    protected function initRequest()
194
    {
195 1
        $request = RequestFactory::fromGlobals();
196
197 1
        Request::setInstance($request);
198 1
    }
199
200
    /**
201
     * Initial Response instance
202
     *
203
     * @return void
204
     */
205 1
    protected function initResponse()
206
    {
207 1
        $response = new ResponseInstance();
208
209 1
        Response::setInstance($response);
210 1
    }
211
212
    /**
213
     * Initial Router instance
214
     *
215
     * @return void
216
     */
217 628
    protected function initRouter()
218
    {
219 628
        $router = new \Bluz\Router\Router();
220 628
        $router->setOptions(Config::getData('router'));
221
222 628
        Router::setInstance($router);
223 628
    }
224
225
    /**
226
     * Get Response instance
227
     *
228
     * @return \Bluz\Response\Response
229
     */
230 1
    public function getResponse()
231
    {
232 1
        return Response::getInstance();
233
    }
234
235
    /**
236
     * Get Request instance
237
     *
238
     * @return \Zend\Diactoros\ServerRequest
239
     */
240 1
    public function getRequest()
241
    {
242 1
        return Request::getInstance();
243
    }
244
245
    /**
246
     * Run application
247
     *
248
     * @return void
249
     */
250
    public function run()
251
    {
252
        $this->process();
253
        $this->render();
254
        $this->end();
255
    }
256
257
    /**
258
     * Process application
259
     *
260
     * Note:
261
     * - Why you don't use "X-" prefix for custom headers?
262
     * - Because it deprecated ({@link http://tools.ietf.org/html/rfc6648})
263
     *
264
     * @return void
265
     */
266 2
    public function process()
267
    {
268 2
        $this->preProcess();
269 2
        $this->doProcess();
270 2
        $this->postProcess();
271 2
    }
272
273
    /**
274
     * Pre process
275
     *
276
     * @return void
277
     * @throws ApplicationException
278
     */
279 2
    protected function preProcess()
280
    {
281 2
        Router::process();
282
283
        // disable Layout for XmlHttpRequests
284 2
        if (Request::isXmlHttpRequest()) {
285
            $this->layoutFlag = false;
286
        }
287
288
        // switch to JSON response based on Accept header
289 2
        if (Request::getAccept([Request::TYPE_HTML, Request::TYPE_JSON]) == Request::TYPE_JSON) {
290
            $this->layoutFlag = false;
291
            Response::switchType('JSON');
292
        }
293 2
    }
294
295
    /**
296
     * Do process
297
     *
298
     * @return void
299
     */
300 2
    protected function doProcess()
301
    {
302 2
        $module = Request::getModule();
303 2
        $controller = Request::getController();
304 2
        $params = Request::getParams();
305
306
        // try to dispatch controller
307
        try {
308
            // dispatch controller
309 2
            $result = $this->dispatch($module, $controller, $params);
310 1
        } catch (ForbiddenException $e) {
311
            $result = $this->forbidden($e);
312 1
        } catch (RedirectException $e) {
313
            // redirect to URL
314
            $result = $this->redirect($e->getUrl());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->redirect($e->getUrl()) (which targets Bluz\Application\Application::redirect()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
315 1
        } catch (\Exception $e) {
316 1
            $result = $this->error($e);
317
        }
318
319
        // setup layout, if needed
320 2
        if ($this->useLayout()) {
321
            // render view to layout
322
            // needed for headScript and headStyle helpers
323 2
            Layout::setContent($result->render());
324 2
            Response::setBody(Layout::getInstance());
325
        } else {
326
            Response::setBody($result);
327
        }
328 2
    }
329
330
    /**
331
     * Post process
332
     *
333
     * @return void
334
     */
335 2
    protected function postProcess()
336
    {
337
        // nothing
338 2
    }
339
340
    /**
341
     * Dispatch controller with params
342
     *
343
     * Call dispatch from any \Bluz\Package
344
     *     Application::getInstance()->dispatch($module, $controller, array $params);
345
     *
346
     * @param  string $module
347
     * @param  string $controller
348
     * @param  array  $params
349
     * @return Controller
350
     * @throws ApplicationException
351
     */
352 5
    public function dispatch($module, $controller, $params = [])
353
    {
354 5
        $this->preDispatch($module, $controller, $params);
355 5
        $result = $this->doDispatch($module, $controller, $params);
356 4
        $this->postDispatch($module, $controller, $params);
357
358 4
        return $result;
359
    }
360
361
    /**
362
     * Pre dispatch mount point
363
     *
364
     * @param  string $module
365
     * @param  string $controller
366
     * @param  array  $params
367
     * @return void
368
     */
369 5
    protected function preDispatch($module, $controller, $params = [])
370
    {
371 5
        Logger::info("app:dispatch:pre: " . $module . '/' . $controller);
372 5
    }
373
374
    /**
375
     * Do dispatch
376
     *
377
     * @param  string $module
378
     * @param  string $controller
379
     * @param  array  $params
380
     * @return Controller
381
     */
382 5
    protected function doDispatch($module, $controller, $params = [])
383
    {
384
        // @TODO: try to find custom controller class
385
386
        // create controller controller
387 5
        $controllerInstance = new Controller($module, $controller);
388
389
        // check HTTP Accept header
390 5
        $controllerInstance->checkAccept();
391
        // check HTTP method
392 5
        $controllerInstance->checkMethod();
393
        // check ACL privileges
394 4
        $controllerInstance->checkPrivilege();
395
396
        // run controller
397 4
        $controllerInstance->run($params);
398
399 4
        return $controllerInstance;
400
    }
401
402
    /**
403
     * Post dispatch mount point
404
     *
405
     * @param  string $module
406
     * @param  string $controller
407
     * @param  array  $params
408
     * @return void
409
     */
410 4
    protected function postDispatch($module, $controller, $params = [])
411
    {
412 4
        Logger::info("<<<:dispatch:post: " . $module . '/' . $controller);
413 4
    }
414
415
    /**
416
     * Render, is send Response
417
     *
418
     * @return void
419
     */
420
    public function render()
421
    {
422
        Response::send();
423
    }
424
425
    /**
426
     * Finally method
427
     *
428
     * @return void
429
     */
430
    public function end()
431
    {
432
        // nothing
433
    }
434
}
435