Completed
Push — 4.0 ( 63fdb6...88351d )
by Marco
05:36
created

Table::regex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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 \Monolog\Logger;
8
use \Comodojo\Dispatcher\Components\Configuration;
9
use \Comodojo\Cache\CacheManager;
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 DispatcherClassModel {
36
37
    private $routes = array();
38
    
39
    private $parser;
40
    
41
    private $cache;
42
    
43
    private $router;
44
45
    public function __construct(
46
        CacheManager $cache,
47
        Router $router
48
    ) {
49
50
        parent::__construct($router->configuration(), $router->logger());
51
        
52
        $this->router = $router;
53
54
        $this->parser = new Parser($router);
55
        
56
        $this->cache = $cache;
57
        
58
        $this->readCache();
59
60
    }
61
62
    public function add($route, $type, $class, $parameters = array()) {
63
64
        $routeData = $this->get($route);
65
66
        if (!is_null($routeData)) {
67
            
68
            $routeData->setType($type)
69
                ->setClassName($class)
70
                ->setParameters($parameters);
71
72
        } else {
73
            
74
            $folders = explode("/", $route);
75
            
76
            $this->register($folders, $type, $class, $parameters);
77
78
        }
79
        
80
        return $this;
81
82
    }
83
84 View Code Duplication
    public function get($route) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
85
86
        $regex = $this->regex($route);
87
88
        if (isset($this->routes[$regex]))
89
            return $this->routes[$regex];
90
        else
91
            return null;
92
93
    }
94
95
    public function regex($route) {
96
97
        $folders = explode("/", $route);
98
99
        return $this->parser->read($folders);
100
101
    }
102
103 View Code Duplication
    public function remove($route) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
104
105
        $regex = $this->regex($route);
106
107
        if (isset($this->routes[$regex])) {
108
            
109
            unset($this->routes[$regex]);
110
            
111
            return true;
112
            
113
        }
114
        
115
        return false;
116
117
    }
118
119
    public function routes($routes = null) {
120
121
        if (is_null($routes)) {
122
            
123
            return $this->routes;
124
            
125
        } else {
126
            
127
            $this->routes = $routes;
128
            
129
            return $this;
130
            
131
        }
132
133
    }
134
135
    public function defaultRoute() {
136
137
        return $this->get('/');
138
139
    }
140
141
    public function load($routes) {
142
143
        if ( !empty($routes) ) {
144
145
            foreach( $routes as $name => $route ) {
146
147
                $this->add($route['route'], $route['type'], $route['class'], $route['parameters']);
148
149
            }
150
151
        }
152
        
153
        $this->logger->debug("Routing table loaded");
154
155
        return $this->dumpCache();
156
157
    }
158
    
159
    private function readCache() {
160
        
161
        $routes = $this->cache->get("dispatcher_routes");
162
        
163
        if (is_null($routes)) return null;
164
        
165
        foreach ($routes as $name => $data) {
166
            
167
            $route = new Route($this->router);
168
                
169
            $this->routes[$name] = $route->setData($data);
170
            
171
        }
172
        
173
        $this->logger->debug("Routing table loaded from cache");
174
        
175
        return $this;
176
        
177
    }
178
    
179
    private function dumpCache() {
180
        
181
        $routes = array();
182
        
183
        foreach($this->routes as $name => $route) {
184
            
185
            $routes[$name] = $route->getData();
186
            
187
        }
188
        
189
        $this->cache->set("dispatcher_routes", $routes, 24 * 60 * 60);
190
        
191
        $this->logger->debug("Routing table saved to cache");
192
        
193
        return $this;
194
        
195
    }
196
197
    // This method add a route to the supported list
198
    private function register($folders, $type, $class, $parameters) {
199
200
        // The values associated with a route are as follows:
201
        $route = new Route($this->router);
202
        $route->setType($type) // Type of route
203
            ->setClassName($class) // Class to be invoked
204
            ->setParameters($parameters); // Parameters passed via the composer.json configuration (cache, ttl, etc...)
205
206
        $this->logger->debug("ROUTE: " . implode("/", $folders));
207
208
        //$this->logger->debug("PARAMETERS: " . var_export($value, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
209
210
        // This method generate a global regular expression which will be able to match all the URI supported by the route
211
        $regex = $this->parser->read($folders, $route);
212
213
        $this->logger->debug("ROUTE: " . $regex);
214
215
        //$this->logger->debug("PARAMETERS: " . var_export($value, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
216
217
        $this->routes[$regex] = $route;
218
219
    }
220
221
}
222