1
|
|
|
<?php namespace Comodojo\Dispatcher\Router; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel; |
4
|
|
|
use \Comodojo\Dispatcher\Router\Parser; |
5
|
|
|
use \Comodojo\Dispatcher\Router\Route; |
6
|
|
|
use \Comodojo\Dispatcher\Router\Model as Router; |
7
|
|
|
use \Comodojo\Dispatcher\Components\Configuration; |
8
|
|
|
use \Comodojo\Cache\CacheManager; |
9
|
|
|
use \Comodojo\Exception\DispatcherException; |
10
|
|
|
use \Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @package Comodojo Dispatcher |
14
|
|
|
* @author Marco Giovinazzi <[email protected]> |
15
|
|
|
* @author Marco Castiello <[email protected]> |
16
|
|
|
* @license GPL-3.0+ |
17
|
|
|
* |
18
|
|
|
* LICENSE: |
19
|
|
|
* |
20
|
|
|
* This program is free software: you can redistribute it and/or modify |
21
|
|
|
* it under the terms of the GNU Affero General Public License as |
22
|
|
|
* published by the Free Software Foundation, either version 3 of the |
23
|
|
|
* License, or (at your option) any later version. |
24
|
|
|
* |
25
|
|
|
* This program is distributed in the hope that it will be useful, |
26
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
27
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
28
|
|
|
* GNU Affero General Public License for more details. |
29
|
|
|
* |
30
|
|
|
* You should have received a copy of the GNU Affero General Public License |
31
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
class Table extends DispatcherClassModel { |
35
|
|
|
|
36
|
2 |
|
public function __construct( |
37
|
|
|
CacheManager $cache, |
38
|
|
|
Router $router |
39
|
1 |
|
) { |
40
|
|
|
|
41
|
2 |
|
parent::__construct($router->configuration, $router->logger); |
42
|
|
|
|
43
|
2 |
|
$this->routes = array(); |
|
|
|
|
44
|
|
|
|
45
|
2 |
|
$this->router = $router; |
|
|
|
|
46
|
|
|
|
47
|
2 |
|
$this->parser = new Parser($this->logger); |
|
|
|
|
48
|
|
|
|
49
|
2 |
|
$this->cache = $cache; |
|
|
|
|
50
|
|
|
|
51
|
1 |
|
$this->readCache(); |
52
|
|
|
|
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
public function add($route, $type, $class, $parameters = array()) { |
56
|
|
|
|
57
|
1 |
|
$routeData = $this->get($route); |
58
|
|
|
|
59
|
1 |
|
if (!is_null($routeData)) { |
60
|
|
|
|
61
|
1 |
|
$routeData->setType($type) |
62
|
|
|
->setClassName($class) |
63
|
|
|
->setParameters($parameters); |
64
|
|
|
|
65
|
|
|
} else { |
66
|
|
|
|
67
|
1 |
|
$folders = explode("/", $route); |
68
|
|
|
|
69
|
1 |
|
$this->register($folders, $type, $class, $parameters); |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return $this; |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function get($route) { |
78
|
|
|
|
79
|
1 |
|
$regex = $this->regex($route); |
80
|
|
|
|
81
|
1 |
|
if (isset($this->routes[$regex])) |
|
|
|
|
82
|
1 |
|
return $this->routes[$regex]; |
|
|
|
|
83
|
|
|
else |
84
|
1 |
|
return null; |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function regex($route) { |
89
|
|
|
|
90
|
1 |
|
$folders = explode("/", $route); |
91
|
|
|
|
92
|
1 |
|
return $this->parser->read($folders); |
|
|
|
|
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function remove($route) { |
97
|
|
|
|
98
|
1 |
|
$regex = $this->regex($route); |
99
|
|
|
|
100
|
1 |
|
$routes = $this->routes; |
|
|
|
|
101
|
|
|
|
102
|
1 |
|
if (isset($routes[$regex])) { |
103
|
|
|
|
104
|
1 |
|
unset($routes[$regex]); |
105
|
|
|
|
106
|
1 |
|
$this->routes = $routes; |
|
|
|
|
107
|
|
|
|
108
|
1 |
|
return true; |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return false; |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function defaultRoute() { |
117
|
|
|
|
118
|
|
|
return $this->get('/'); |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function load($routes) { |
123
|
|
|
|
124
|
|
|
if ( !empty($routes) ) { |
125
|
|
|
|
126
|
|
|
foreach( $routes as $name => $route ) { |
127
|
|
|
|
128
|
|
|
$this->add($route['route'], $route['type'], $route['class'], $route['parameters']); |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->logger->debug("Routing table loaded"); |
135
|
|
|
|
136
|
|
|
$this->dumpCache(); |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
private function readCache() { |
141
|
|
|
|
142
|
1 |
|
if ( $this->configuration->get('routing-table-cache') !== true ) return; |
143
|
|
|
|
144
|
|
|
$this->routes = $this->cache->setNamespace('dispatcherinternals')->get("dispatcher-routes"); |
|
|
|
|
145
|
|
|
|
146
|
|
|
if (is_null($this->routes)) { |
|
|
|
|
147
|
|
|
|
148
|
|
|
$this->routes = array(); |
|
|
|
|
149
|
|
|
|
150
|
|
|
return; |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->logger->debug("Routing table loaded from cache"); |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function dumpCache() { |
159
|
|
|
|
160
|
|
|
if ( $this->configuration->get('routing-table-cache') !== true ) return; |
161
|
|
|
|
162
|
|
|
$ttl = $this->configuration->get('routing-table-ttl'); |
163
|
|
|
|
164
|
|
|
$this->cache->setNamespace('dispatcherinternals')->set("dispatcher-routes", $this->routes, $ttl == null ? 86400 : intval($ttl)); |
|
|
|
|
165
|
|
|
|
166
|
|
|
$this->logger->debug("Routing table saved to cache"); |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// This method add a route to the supported list |
171
|
1 |
|
private function register($folders, $type, $class, $parameters) { |
172
|
|
|
|
173
|
|
|
// The values associated with a route are as follows: |
174
|
1 |
|
$route = new Route($this->router); |
|
|
|
|
175
|
1 |
|
$route->setType($type) // Type of route |
176
|
1 |
|
->setClassName($class) // Class to be invoked |
177
|
1 |
|
->setParameters($parameters); // Parameters passed via the composer.json configuration (cache, ttl, etc...) |
178
|
|
|
|
179
|
1 |
|
$this->logger->debug("ROUTE: " . implode("/", $folders)); |
180
|
|
|
|
181
|
|
|
//$this->logger->debug("PARAMETERS: " . var_export($value, true)); |
|
|
|
|
182
|
|
|
|
183
|
|
|
// This method generate a global regular expression which will be able to match all the URI supported by the route |
184
|
1 |
|
$regex = $this->parser->read($folders, $route); |
|
|
|
|
185
|
|
|
|
186
|
1 |
|
$this->logger->debug("ROUTE: " . $regex); |
187
|
|
|
|
188
|
|
|
//$this->logger->debug("PARAMETERS: " . var_export($value, true)); |
|
|
|
|
189
|
|
|
|
190
|
1 |
|
$this->routes = array_merge($this->routes, array( |
|
|
|
|
191
|
|
|
$regex => $route |
192
|
1 |
|
)); |
193
|
|
|
|
194
|
1 |
|
} |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write 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.