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