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