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\Cms; |
19
|
|
|
use Core\Plugin; |
20
|
|
|
use Cake\Event\Event; |
21
|
|
|
use Cake\Http\Response; |
22
|
|
|
use Core\Event\EventManager; |
23
|
|
|
use Core\Controller\Component\AppComponent; |
24
|
|
|
use Core\Controller\Component\MoveComponent; |
25
|
|
|
use Core\Controller\Component\FlashComponent; |
26
|
|
|
use Core\Controller\Component\ProcessComponent; |
27
|
|
|
use Cake\Controller\Controller as CakeController; |
28
|
|
|
use Cake\Controller\Component\RequestHandlerComponent; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class AppController |
32
|
|
|
* |
33
|
|
|
* @package Core\Controller |
34
|
|
|
* @property AppComponent $App |
35
|
|
|
* @property MoveComponent $Move |
36
|
|
|
* @property FlashComponent $Flash |
37
|
|
|
* @property ProcessComponent $Process |
38
|
|
|
* @property \Cake\Http\Response $response |
39
|
|
|
* @property RequestHandlerComponent $RequestHandler |
40
|
|
|
*/ |
41
|
|
|
class AppController extends CakeController |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Hold CMS object. |
46
|
|
|
* |
47
|
|
|
* @var Cms |
48
|
|
|
*/ |
49
|
|
|
public $cms; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initialization hook method. |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
* |
56
|
|
|
* @throws \JBZoo\Utils\Exception |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function initialize() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
parent::initialize(); |
61
|
|
|
$this->cms = Cms::getInstance(); |
62
|
|
|
$this->_setTheme(); |
63
|
|
|
|
64
|
|
|
$pluginEvent = Plugin::getData('Core', 'Controller.initialize'); |
65
|
|
|
if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('Controller.initialize')) { |
66
|
|
|
call_user_func_array($pluginEvent->find(0), [$this]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Called after the controller action is run, but before the view is rendered. You can use this method |
72
|
|
|
* to perform logic or set view variables that are required on every request. |
73
|
|
|
* |
74
|
|
|
* @param \Cake\Event\Event $event The beforeRender event. |
75
|
|
|
* @return void |
76
|
|
|
* |
77
|
|
|
* @throws \JBZoo\Utils\Exception |
78
|
|
|
*/ |
79
|
|
|
public function beforeRender(Event $event) |
80
|
|
|
{ |
81
|
|
|
$pluginEvent = Plugin::getData('Core', 'Controller.beforeRender'); |
82
|
|
|
if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('Controller.beforeRender')) { |
83
|
|
|
call_user_func_array($pluginEvent->find(0), [$this, $event]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!array_key_exists('_serialize', $this->viewVars) && |
87
|
|
|
in_array($this->response->getType(), ['application/json', 'application/xml']) |
88
|
|
|
) { |
89
|
|
|
$this->set('_serialize', true); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Called before the controller action. You can use this method to configure and customize components |
95
|
|
|
* or perform logic that needs to happen before each controller action. |
96
|
|
|
* |
97
|
|
|
* @param Event $event |
98
|
|
|
* @return void |
99
|
|
|
* |
100
|
|
|
* @throws \JBZoo\Utils\Exception |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function beforeFilter(Event $event) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
EventManager::trigger('Controller.setup', $this); |
105
|
|
|
|
106
|
|
|
$pluginEvent = Plugin::getData('Core', 'Controller.beforeFilter'); |
107
|
|
|
if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('Controller.beforeFilter')) { |
108
|
|
|
call_user_func_array($pluginEvent->find(0), [$this, $event]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* The beforeRedirect method is invoked when the controller's redirect method is called but before any |
114
|
|
|
* further action. |
115
|
|
|
* |
116
|
|
|
* @param Event $event |
117
|
|
|
* @param array|string $url |
118
|
|
|
* @param Response $response |
119
|
|
|
* @return void |
120
|
|
|
* |
121
|
|
|
* @throws \JBZoo\Utils\Exception |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function beforeRedirect(Event $event, $url, Response $response) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$pluginEvent = Plugin::getData('Core', 'Controller.beforeRedirect'); |
126
|
|
|
if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('Controller.beforeRedirect')) { |
127
|
|
|
call_user_func_array($pluginEvent->find(0), [$this, $event, $url, $response]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Called after the controller action is run and rendered. |
133
|
|
|
* |
134
|
|
|
* @param Event $event |
135
|
|
|
* @return void |
136
|
|
|
* |
137
|
|
|
* @throws \JBZoo\Utils\Exception |
138
|
|
|
*/ |
139
|
|
|
public function afterFilter(Event $event) |
140
|
|
|
{ |
141
|
|
|
$pluginEvent = Plugin::getData('Core', 'Controller.afterFilter'); |
142
|
|
|
if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('Controller.afterFilter')) { |
143
|
|
|
call_user_func_array($pluginEvent->find(0), [$this, $event]); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Setup application theme. |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
protected function _setTheme() |
153
|
|
|
{ |
154
|
|
|
if ($this->request->is('theme')) { |
155
|
|
|
$theme = $this->request->getParam('theme'); |
156
|
|
|
$this->viewBuilder()->setTheme($theme); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.