Completed
Push — master ( c2606f...ff2953 )
by Cheren
08:57
created

AppController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 4
Bugs 1 Features 3
Metric Value
wmc 8
c 4
b 1
f 3
lcom 2
cbo 7
dl 0
loc 82
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 1
A beforeRender() 0 10 3
A beforeFilter() 0 5 1
A beforeRedirect() 0 4 1
A afterFilter() 0 4 1
A _setTheme() 0 5 1
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Controller;
17
18
use Core\Theme;
19
use Core\Plugin;
20
use Cake\Event\Event;
21
use Cake\Network\Response;
22
use Core\Event\EventManager;
23
use Cake\Controller\Controller as CakeController;
24
25
/**
26
 * Class AppController
27
 *
28
 * @package Core\Controller
29
 */
30
class AppController extends CakeController
31
{
32
33
    /**
34
     * Initialization hook method.
35
     *
36
     * @return void
37
     */
38
    public function initialize()
39
    {
40
        parent::initialize();
41
        $this->_setTheme();
42
        Plugin::manifestEvent('Controller.initialize', $this);
43
    }
44
45
    /**
46
     * Called after the controller action is run, but before the view is rendered. You can use this method
47
     * to perform logic or set view variables that are required on every request.
48
     *
49
     * @param \Cake\Event\Event $event The beforeRender event.
50
     * @return void
51
     */
52
    public function beforeRender(Event $event)
53
    {
54
        Plugin::manifestEvent('Controller.beforeRender', $this, $event);
55
56
        if (!array_key_exists('_serialize', $this->viewVars) &&
57
            in_array($this->response->type(), ['application/json', 'application/xml'])
58
        ) {
59
            $this->set('_serialize', true);
60
        }
61
    }
62
63
    /**
64
     * Called before the controller action. You can use this method to configure and customize components
65
     * or perform logic that needs to happen before each controller action.
66
     *
67
     * @param Event $event
68
     * @return void
69
     */
70
    public function beforeFilter(Event $event)
71
    {
72
        EventManager::trigger('Controller.setup', $this);
73
        Plugin::manifestEvent('Controller.beforeFilter', $this, $event);
74
    }
75
76
    /**
77
     * The beforeRedirect method is invoked when the controller's redirect method is called but before any
78
     * further action.
79
     *
80
     * @param Event $event
81
     * @param array|string $url
82
     * @param Response $response
83
     * @return void
84
     */
85
    public function beforeRedirect(Event $event, $url, Response $response)
86
    {
87
        Plugin::manifestEvent('Controller.beforeRedirect', $this, $event, $url, $response);
88
    }
89
90
    /**
91
     * Called after the controller action is run and rendered.
92
     *
93
     * @param Event $event
94
     * @return void
95
     */
96
    public function afterFilter(Event $event)
97
    {
98
        Plugin::manifestEvent('Controller.afterFilter', $this, $event);
99
    }
100
101
    /**
102
     * Setup application theme.
103
     *
104
     * @return void
105
     */
106
    protected function _setTheme()
107
    {
108
        $theme = Theme::get($this->request->param('prefix'));
109
        $this->viewBuilder()->theme($theme);
110
    }
111
}
112