|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Jitamin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Jitamin Team |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation\Http; |
|
13
|
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Route Dispatcher. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Router extends Base |
|
20
|
|
|
{ |
|
21
|
|
|
const DEFAULT_CONTROLLER = 'Dashboard/DashboardController'; |
|
22
|
|
|
const DEFAULT_METHOD = 'index'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Plugin name. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $currentPluginName = ''; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Controller. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $currentControllerName = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Action. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $currentActionName = ''; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get plugin name. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getPlugin() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->currentPluginName; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get controller. |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getController() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->currentControllerName; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get action. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getAction() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->currentActionName; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the path to compare patterns. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getPath() |
|
81
|
|
|
{ |
|
82
|
|
|
$path = substr($this->request->getUri(), strlen($this->helper->url->dir())); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if ($this->request->getQueryString() !== '') { |
|
|
|
|
|
|
85
|
|
|
$path = substr($path, 0, -strlen($this->request->getQueryString()) - 1); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($path !== '' && $path[0] === '/') { |
|
89
|
|
|
$path = substr($path, 1); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $path; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Find controller/action from the route table or from get arguments. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function dispatch() |
|
99
|
|
|
{ |
|
100
|
|
|
$controller = urldecode($this->request->getStringParam('controller')); |
|
|
|
|
|
|
101
|
|
|
$action = $this->request->getStringParam('action'); |
|
|
|
|
|
|
102
|
|
|
$plugin = $this->request->getStringParam('plugin'); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
if ($controller === '') { |
|
105
|
|
|
$route = $this->route->findRoute($this->getPath()); |
|
|
|
|
|
|
106
|
|
|
$controller = $route['controller']; |
|
107
|
|
|
$action = $route['action']; |
|
108
|
|
|
$plugin = $route['plugin']; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->currentControllerName = ucfirst($this->sanitize($controller, self::DEFAULT_CONTROLLER, true)); |
|
112
|
|
|
$this->currentActionName = $this->sanitize($action, self::DEFAULT_METHOD); |
|
113
|
|
|
$this->currentPluginName = ucfirst($this->sanitize($plugin)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Check controller and action parameter. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $value |
|
120
|
|
|
* @param string $default |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function sanitize($value, $default = '', $is_controller = false) |
|
125
|
|
|
{ |
|
126
|
|
|
$pattern = $is_controller ? '/^[a-zA-Z_0-9\/]+$/' : '/^[a-zA-Z_0-9]+$/'; |
|
127
|
|
|
|
|
128
|
|
|
return preg_match($pattern, $value) ? $value : $default; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.