-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextension.driver.php
More file actions
217 lines (135 loc) · 5.18 KB
/
extension.driver.php
File metadata and controls
217 lines (135 loc) · 5.18 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
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
<?php
require_once CORE . '/class.cacheable.php';
class Extension_Routing extends Extension
{
private static $resolved;
// delegates
public function getSubscribedDelegates()
{
return array(
array('page' => '/frontend/',
'delegate' => 'FrontendPrePageResolve',
'callback' => 'frontendPrePageResolve')
);
}
// install
public function install()
{
// add configuration
Symphony::Configuration()->set('path', 'routes.xml', 'routing');
Administration::instance()->saveConfig();
// create example
if (!file_exists(WORKSPACE . '/routes.xml')) {
copy(EXTENSIONS . '/routing/routes.xml', WORKSPACE . '/routes.xml');
}
// create cache
$this->enable();
}
// uninstall
public function uninstall()
{
// remove configuration
Symphony::Configuration()->remove('routing');
Administration::instance()->saveConfig();
// clear cache
$this->disable();
}
// enable
public function enable()
{
// refresh cache
$this->cache(true);
}
// disable
public function disable()
{
$cache = new Cacheable(Symphony::Database());
$cache_id = md5('routes');
// clear cache
$cache->forceExpiry($cache_id);
}
// routing
public function frontendPrePageResolve($context)
{
if (!self::$resolved) {
if ($path_from = $context['page']) {
if (is_array($routes = $this->cache())) {
foreach ($routes as $route_from => $route_to) {
// actual routing
if (preg_match($route_from, $path_from)) {
$path_to = preg_replace($route_from, $route_to, $path_from);
break;
}
}
}
if ($path_to) {
$context['page'] = $path_to;
} else {
self::$resolved = true;
throw new FrontendPageNotFoundException();
}
}
self::$resolved = true;
}
}
// cache
private function cache($refresh = null)
{
$cache = new Cacheable(Symphony::Database());
$cache_id = md5('routes');
if ($refresh || !($cache_data = $cache->check($cache_id))) {
// clear cache
$cache->forceExpiry($cache_id);
// get xml path
$path = Symphony::Configuration()->get('path', 'routing');
$path = WORKSPACE . '/' . trim($path, '/');
// get routes from xml
$routes = array();
if (!$refresh) {
libxml_use_internal_errors(true);
}
if ($xml = simplexml_load_file($path)) {
// prepare routes
foreach ($xml->route as $route) {
if (isset($route['from']) && isset($route['to'])) {
// prepare route
$route_from = (string) $route['from'];
$route_to = (string) $route['to'];
$route_from = '/' . trim($route_from, '/') . '/';
$route_to = '/' . trim($route_to, '/') . '/';
// prepare filters
$route_filters = array();
foreach ($route->filter as $filter) {
if (isset($filter['parameter']) && isset($filter['match'])) {
$filter_parameter = (string) $filter['parameter'];
$filter_match = (string) $filter['match'];
$route_filters[$filter_parameter] = trim($filter_match, '()');
}
}
// prepare parameters
preg_match_all('/(:[\w-]+)/u', $route_from, $matches);
foreach ($matches[0] as $index => $parameter) {
// check filter
if (!isset($route_filters[$parameter])) {
// set default filter
$route_filters[$parameter] = '[\w\.-]+';
}
// subsitute parameters
$route_from = str_replace($parameter, '(' . $route_filters[$parameter] . ')', $route_from);
$route_to = str_replace($parameter, '$' . ($index + 1), $route_to);
}
$route_from = '/^' . str_replace('/', '\/', $route_from) . '$/u';
$routes[$route_from] = $route_to;
}
}
}
// serialize routes
$routes = serialize($routes);
// write cache
$cache->write($cache_id, $routes);
// get cache data
$cache_data = $cache->check($cache_id);
}
return unserialize($cache_data['data']);
}
}