-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSlugRoute.php
More file actions
145 lines (130 loc) · 3.51 KB
/
SlugRoute.php
File metadata and controls
145 lines (130 loc) · 3.51 KB
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
<?php
/**
* SlugRoute
*/
App::uses('ClassRegistry', 'Utility');
App::uses('Page', 'Pages.Model');
/**
* Automatically slugs routes based on named parameters
*
*/
class SlugRoute extends CakeRoute {
/**
* 何度も同じRouteチェックをさせないために、一度チェックしたものは、キャッシュする
*
* @var array
*/
private $__executedRoute = [];
/**
* 何度も同じRouteチェックをさせないために、一度チェックしたものは、キャッシュする
*
* @var array
*/
private $__defaultSpace = null;
/**
* parse
*
* @param string $url The URL to attempt to parse.
* @return mixed Boolean false on failure, otherwise an array or parameters
*
* 速度改善の修正に伴って発生したため抑制
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function parse($url) {
$currentRequest = Router::getRequest(true);
if (! empty($currentRequest)) {
$url = '/' . $currentRequest->url;
}
$params = parent::parse($url);
if (empty($params)) {
return false;
}
$PageModel = ClassRegistry::init('Pages.Page');
$dataSource = ConnectionManager::getDataSource($PageModel->useDbConfig);
$tables = $dataSource->listSources();
if (! in_array($PageModel->tablePrefix . $PageModel->useTable, $tables)) {
return false;
}
$passKey = json_encode($params['pass']);
if (array_key_exists($passKey, $this->__executedRoute)) {
return $this->__executedRoute[$passKey];
}
$this->Space = ClassRegistry::init('Rooms.Space');
if ($params['pass']) {
$spaces = $this->Space->getSpaces();
$result = [];
foreach ($spaces as $row) {
if ($row['Space']['permalink'] == $params['pass'][0]) {
$result = $row;
break;
}
}
if ($result) {
$params['spacePermalink'] = $result['Space']['permalink'];
$params['spaceId'] = $result['Space']['id'];
unset($params['pass'][0]);
}
$params['pass'] = array_values($params['pass']);
}
if (! isset($params['spaceId'])) {
$result = $this->__findDefaultSpace();
if (isset($result['Space'])) {
$params['spacePermalink'] = $result['Space']['permalink'];
$params['spaceId'] = $result['Space']['id'];
} else {
$params['spaceId'] = null;
$params['spacePermalink'] = null;
}
}
$path = implode('/', $params['pass']);
if ($path === '') {
$conditions = array('Page.sort_key' => Page::HOME_SORT_KEY);
} else {
$conditions = array(
'Page.permalink' => $path,
'Room.space_id' => $params['spaceId']
);
}
$Room = ClassRegistry::init('Rooms.Room');
$count = $PageModel->find('count', array(
'conditions' => $conditions,
'recursive' => -1,
'joins' => [
[
'table' => $Room->table,
'alias' => $Room->alias,
'type' => 'INNER',
'conditions' => array(
$Room->alias . '.id' . ' = ' . $PageModel->alias . '.room_id',
),
]
],
));
if ($count) {
$params['pagePermalink'] = $params['pass'];
$this->__executedRoute[$passKey] = true;
return $params;
}
$this->__executedRoute[$passKey] = false;
return false;
}
/**
* デフォルトのスペースデータ取得
*
* @return array
*/
private function __findDefaultSpace() {
if ($this->__defaultSpace) {
return $this->__defaultSpace;
} else {
$result = $this->Space->cacheFindQuery('first', array(
'fields' => ['id', 'permalink'],
'conditions' => array('permalink' => '', 'id !=' => Space::WHOLE_SITE_ID),
'recursive' => -1
));
$this->__defaultSpace = $result;
}
return $result;
}
}