Repository navigation
Expand file tree
/
Copy pathPvcController.php
More file actions
266 lines (245 loc) · 7.97 KB
/
Copy pathPvcController.php
File metadata and controls
266 lines (245 loc) · 7.97 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
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
<?php
/**
* Pvc Controller Class V. 0.9.0
* Part of Pvc, a module for ProcessWire 2.3+
*
* by Oliver Wehn
* https://github.com/oliverwehn
*
* inspired of and based on parts of MVC Module by Harmster
* https://github.com/Hawiak
* hawiak.nl
*
* Basic Controller class that extends PW’s Wire class and
* so provides access to fuel vars besides basic module-specific
* methods. Don’t modifiy.
*
*/
class PvcController extends WireData {
const DEFAULT_ACTION = 'index';
protected $_page;
protected $routes = array();
/**
* Initialization and setup
*/
public function __construct(PvcPageProxy $page) {
$this->set('page', $page);
$this->set('assets', new WireArray);
$this->set('layout', PvcCore::DEFAULT_LAYOUT);
$this->init();
}
public function init() { }
public function get($key) {
switch($key) {
case 'page': {
return $this->_page;
break;
}
default: {
$method = 'get' . PvcCore::camelcase($key);
if(method_exists($this, $method)) {
return $this->$method();
}
return parent::get($key);
}
}
}
public function set($key, $value) {
switch($key) {
case 'page': {
$this->_page = $value;
return $this;
break;
}
default: {
$method = 'set' . PvcCore::camelcase($key);
if(method_exists($this, $method)) {
return $this->$method($value);
}
return parent::set($key, $value);
}
}
}
public function execute($action, $input=array()) {
if(count($input))
$this->input->route = new WireInputData($input);
if($this->call($action)) {
$result = array('action' => $action);
if($page = $this->get('page')) {
$pageScope = $page->getArray();
foreach($pageScope as $k => $v) {
$result[$k] = $v;
}
foreach($page->fields as $field) {
$k = $field->name;
$result[$k] = $page->$k;
}
}
$controllerScope = $this->getArray();
foreach($controllerScope as $k => $v) {
$result[$k] = $v;
}
return $result;
}
return false;
}
public function validateAction($action) {
if(!(is_string($action)) || !(method_exists($this, $action))) return FALSE;
$reflMeth = new ReflectionMethod($this, $action);
if($reflMeth->isPublic()) {
return TRUE;
} else {
return FALSE;
}
}
public function call($action) {
if(!$this->validateAction($action)) throw new \WireException(sprintf($this->_('Called invalid action "%s" on controller "%s".'), $action, get_class($this)));
call_user_func(array(&$this, $action));
return true;
}
public function route($path, array $match, $method) {
// alias could be '/:id/edit/'
// should match '/17/edit/'
// route to action 'actionEdit' with input id=>17 available
if(!preg_match("#^/(([a-z0-9\-_:]+(/|$))+)$#i", $path, $pathMatch))
throw new \WireException(sprintf($this->_("Path '%s' is not valid for route alias."), $path));
if(!method_exists($this, $method))
throw new \WireException(sprintf($this->_("Tried to set up a route to method '%s' which doesn’t exist."), $method));
$pathElements = explode("/", rtrim($pathMatch[1], '/'));
$route = '#^' . preg_quote(rtrim($path, '/')) . '(/(page[0-9]+/?)?)?$#';
$keys = array();
$i = 0;
do {
$i++;
$start = mb_strpos($route, ':');
if($start !== FALSE) {
if($start == mb_strlen($route) - 1) break;
$end = mb_strpos($route, '/', $start);
$key = mb_substr($route, $start + 1, $end - ($start + 1));
$keys[$i] = $key;
$route = str_replace(preg_quote(':'.$key), "(".$match[$key].")", $route);
}
} while($start !== FALSE);
$pattern = array(
'keys' => $keys,
'action' => $method
);
$this->routes[$route] = $pattern;
return $route;
}
public function calledAction($justName=false) {
if((!$justName) && ($action = $this->get('calledAction'))) return $action;
if(!($options = $this->get('options'))) $options = array('route' => '/');
$actionName = self::DEFAULT_ACTION;
$actionInput = array();
if(array_key_exists('action', $options)) {
$actionName = $options['action'];
}
elseif(array_key_exists('route', $options)) {
$route = $options['route'];
if($resolved = $this->routeToAction($route)) {
$actionName = $resolved['action'];
$actionInput = $resolved['input'];
}
else {
throw new \WireException(sprintf($this->_('Route "%s" isn’t valid for Page "%s".'), $route, $this->get('page')->path));
}
}
if(!$justName) {
$self = $this;
$action = function() use($self, $actionName, $actionInput) {
return call_user_func(array(&$self, 'execute'), $actionName, $actionInput);
};
$this->set('calledAction', $action);
}
else {
$action = $actionName;
}
return $action;
}
public function routeToAction($route="/") {
$result = array('action' => self::DEFAULT_ACTION, 'input' => array());
if($route === '/') return $result;
$route_segments = explode('/', rtrim(ltrim($route, '/'), '/'));
if(count($route_segments) > 0) {
// maybe a regular action?
$action = implode('_', $route_segments);
if($this->validateAction($action)) $result['action'] = $action;
// or more special? Check for patterns
if(count($this->routes) > 0) {
foreach($this->routes as $pattern => $route_def) {
if(preg_match($pattern, $route, $matches)) {
$action = $route_def['action'];
$input = array();
$cnt_matches = count($matches);
for($j=1; $j<$cnt_matches; $j++) {
if(isset($route_def['keys'][$j])) {
$input[$route_def['keys'][$j]] = $matches[$j];
} else {
break;
}
}
$result['action'] = $action;
$result['input'] = $input;
}
}
}
}
return $result;
}
/* Add a script to be available to the view
* @method script
*/
public function script($name, $group="base", $prio=NULL) {
return $this->_setAsset('scripts', $group, $prio, $name);
}
/* Add a stylesheet to be available to the view
* @method script
*/
public function style($name, $group="base", $prio=0) {
return $this->_setAsset('styles', $group, $prio, $name);
}
/* Internal asset setter method
* @method script
*/
private function _setAsset($type, $group, $prio, $name) {
$assets = $this->get('assets');
if(!$assets->has($type)) $assets->set($type, new \WireArray);
$assetsForType = $assets->get($type);
if(!$assetsForType->has($group)) $assetsForType->set($group, new \WireArray);
$assetsGroup = $assetsForType->get($group);
$newAsset = new \WireData;
$newAssetPath = preg_match("#^(http:)?//#i", $name) ? $name : $this->pvc->urls->$type . $name;
$newAsset->set('path', $newAssetPath)->set('priority', $prio !== NULL ? $prio : $assetsGroup->count());
$assetsGroup->set($name, $newAsset);
return $assetsGroup->count;
}
private function _getAsset($type, $group, $name) {
$assets = $this->get('assets');
if(!(array_key_exists($type, $assets)) || !(array_key_exists($group, $assets[$type])) || !(array_key_exists($name, $assets[$type][$group])))
return false;
else
return $assets[$type][$group][$name];
}
// default action
public function index() {
}
protected function _sortByPriority($a, $b) {
if($a[1] == $b[1]) return 0;
return $a[1] < $b[1]?-1:1;
}
protected function _ext($type, $subtype=NULL) {
return ProcessPvc::ext($type, $this->pvc->template_engine);
}
public static function extend($className) {
$classCode = '
class '. $className . ' extends ' . get_called_class() . ' {
public function __constructor(PvcPageProxy $page) {
parent::__constructor($page);
}
}
';
eval($classCode);
return class_exists($className);
}
}