-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouting.php
executable file
·298 lines (258 loc) · 7.46 KB
/
Routing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* @property int idPage
* @property int idCategory
* @property int page
* @property int idContentPage
* @property int routeStatus
* @property int idRoute
* @property int type
* @property string name
* @property string action
* @property string url
* @property string controller
* @property string module
* @property string defaultParams
*/
class RM_Routing
extends
RM_Entity
implements
RM_Interface_Deletable {
const TABLE_NAME = 'routing';
const CACHE_NAME = 'routing';
const AUTO_CACHE = true;
protected static $_properties = array(
'idRoute' => array(
'id' => 'true',
'type' => 'int'
),
'type' => array(
'default' => self::TYPE_ROUTE,
'type' => 'int'
),
'name' => array(
'type' => 'string'
),
'module' => array(
'default' => 'public',
'type' => 'string'
),
'controller' => array(
'type' => 'string'
),
'action' => array(
'type' => 'string'
),
'url' => array(
'type' => 'string'
),
'defaultParams' => array(
'default' => '{}',
'type' => 'string'
),
'routeStatus' => array(
'default' => self::STATUS_UNDELETED,
'type' => 'int'
)
);
/**
* @var RM_Routing_Url
*/
private $_url;
/**
* @var RM_Routing_DefaultParams
*/
private $_defaultParams;
/**
* @var RM_Entity_Worker_Data
*/
protected $_routingDataWorker;
const TYPE_ROUTE = 1;
const TYPE_STATIC = 2;
const TYPE_REGEX = 3;
const ERROR_INVALID_ROUTE_ID = 'Invalid route id';
const ERROR_ALIAS = 'Wrong format of page alias';
const ERROR_ALIAS_EXISTS = 'Such alias already exists';
const ERROR_INVALID_ROUTE_TYPE = 'Invalid route type';
const ROUTE_CACHE = 'ALL';
const TMP_ROUTE_NAME = '~tmp';
public function __construct(stdClass $data) {
parent::__construct( $data );
$this->_url = new RM_Routing_Url( $data->url );
$this->_defaultParams = new RM_Routing_DefaultParams($data->defaultParams);
$this->_routingDataWorker = new RM_Entity_Worker_Data(get_called_class(), $data);
}
public function getData() {
return $this->_routingDataWorker->getAllData();
}
public static function create(
$routeName,
$routeController,
$routeAction,
$routeUrl
) {
$url = new RM_Routing_Url($routeUrl);
$route = new self( new RM_Compositor( array(
'name' => $routeName,
'controller' => $routeController,
'action' => $routeAction,
'url' => $url->format()->getInitialUrl(),
'defaultParams' => '{}'
) ) );
return $route;
}
public static function getByUrl($url) {
$url = trim($url);
$select = self::_getSelect();
$select->where( 'url = ?', $url);
return self::_initItem( $select );
}
public static function getByName($name) {
$select = self::_getSelect();
$select->where( 'name = ?', $name);
return self::_initItem( $select );
}
public function validate(RM_Exception $e) {
if (!$this->getRoutingUrl()->checkFormat( $this->getParams() )) {
$e[] = self::ERROR_ALIAS;
}
if (!$this->getRoutingUrl()->checkUnique( $this->getId() )) {
$e[] = self::ERROR_ALIAS_EXISTS;
}
}
public function getId() {
return $this->idRoute;
}
public function getType() {
return $this->type;
}
public function getName() {
return $this->name;
}
public function getStatus() {
return $this->routeStatus;
}
/**
* @param int $status
* @throws Exception
* @return RM_Routing
*/
public function setStatus($status) {
$status = (int)$status;
if (in_array($status, array(
self::STATUS_DELETED,
self::STATUS_UNDELETED,
))) {
$this->routeStatus = $status;
} else {
throw new Exception('WRONG STATUS GIVEN');
}
return $this;
}
public function setName($name) {
$this->name = $name;
}
public function getControllerName() {
return $this->controller;
}
public function getAction() {
return $this->action;
}
public function getModuleName() {
return $this->module;
}
public function setAction($actionName) {
if ($actionName === '') {
throw new Exception('Empty action name given');
}
$this->action = $actionName;
}
/**
* @return RM_Routing_Url
*/
public function getRoutingUrl() {
return $this->_url;
}
public function getUrl(array $params = array()) {
$params = array_merge($this->_defaultParams->getParams(), $params);
return $this->getRoutingUrl()->getAssembledUrl( $params );
}
public function setUrl($url) {
if ($this->getRoutingUrl()->getInitialUrl() !== $url) {
$urlObject = new RM_Routing_Url($url);
$urlObject->format();
$this->_url = $urlObject;
$this->url = $urlObject->getInitialUrl();
}
}
public function setRawUrl($rawUrl) {
$this->url = $rawUrl;
}
public function __get($name) {
$val = $this->_routingDataWorker->getValue($name);
if (is_null($val)) {
return $this->_defaultParams->__get($name);
} else {
return $val;
}
}
public function __set($name, $value) {
if (is_null($this->_routingDataWorker->setValue($name, $value))) {
$this->_defaultParams->__set($name, $value);
$this->defaultParams = $this->_defaultParams->__toString();
}
}
public function save() {
if ($this->_routingDataWorker->save()) {
static::clearCache();
if (static::AUTO_CACHE) {
$this->__refreshCache();
}
}
return $this;
}
public function getParams() {
return array_merge(array(
'controller' => $this->controller,
'action' => $this->action,
'module' => $this->module,
'idRoute' => $this->idRoute
), $this->_defaultParams->getParams());
}
/**
* @deprecated
*/
public function delete() {
$this->remove();
}
public function remove(){
$this->setStatus( self::STATUS_DELETED );
$this->save();
$this->clear();
}
public function clear() {
$cm = Zend_Registry::get('cachemanager');
/* @var Zend_Cache_Manager $cm */
$cache = $cm->getCache('routing');
/* @var Zend_Cache_Core $cache */
$cache->remove( $this->getId() );
}
/**
* @static
* @deprecated
* @return RM_Routing[]
*/
public static function getRoutingList() {
return self::getList();
}
public static function _setSelectRules(Zend_Db_Select $select) {
$select->where('routeStatus != ?', self::STATUS_DELETED);
}
/**
* @deprecated
*/
public static function clearCache() {
RM_Routing_Installer::getInstance()->clear();
}
}