/
Common.php
126 lines (108 loc) · 2.86 KB
/
Common.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
<?php
namespace Nano\Route;
abstract class Common {
const PREFIX_REGEXP = '~';
protected $location = null;
protected $module = null;
protected $controller = null;
protected $action = null;
protected $params = null;
protected $matches = null;
protected $compiledParams = null;
public function __construct($location, $controller = 'index', $action = 'index', $module = null, array $params = array()) {
$this->location = $location;
$this->module = $module;
$this->controller = $controller;
$this->action = $action;
$this->params = $params;
}
/**
* @return boolean
* @param string $location
*/
abstract public function match($location);
/**
* @return \Nano\Route\Common
* @param string $location
* @param string $controller
* @param string $action
* @param string $module
* @param array $params
*/
public static function create($location, $controller = 'index', $action = 'index', $module = null, array $params = array()) {
if ('' === $location || null === $location) {
return new \Nano\Route\StaticLocation($location, $controller, $action, $module, $params);
}
if (self::PREFIX_REGEXP == $location[0]) {
return new \Nano\Route\RegExp(subStr($location, 1), $controller, $action, $module, $params);
}
return new \Nano\Route\StaticLocation($location, $controller, $action, $module, $params);
}
/**
* @return string
*/
public function location() {
return $this->location;
}
public function module() {
return $this->module;
}
/**
* @return string
*/
public function controller() {
return $this->controller;
}
/**
* @return string
*/
public function controllerClass() {
if (null === $this->module) {
return \Nano\Names::applicationClass($this->controller, \Nano\Names::NAMESPACE_CONTROLLER);
}
return \Nano\Names::moduleClass($this->module, $this->controller, \Nano\Names::NAMESPACE_CONTROLLER);
}
/**
* @return string
*/
public function action() {
return $this->action;
}
/**
* @return string[]
*/
public function matches() {
return $this->matches;
}
/**
* @return array
*/
public function params() {
if (null === $this->compiledParams) {
$this->compiledParams = $this->params;
if (is_array($this->matches())) {
foreach ($this->matches() as $name => $value) {
if (is_string($name)) {
$this->compiledParams[$name] = $value;
}
}
}
}
return $this->compiledParams;
}
public function addParams(array $params) {
$this->compiledParams = null;
foreach ($params as $name => $value) {
$this->params[$name] = $value;
}
}
/**
* @return string
*/
public function __toString() {
return $this->controller() . '::' . $this->action() . '() when location matches [' . $this->location() . ']';
}
public function __sleep() {
return array('location', 'module', 'controller', 'action', 'params');
}
}