Passed
Branch master (783ca0)
by Henri
05:36 queued 04:17
created

Router::currentRouteAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
use Exception;
6
7
class Router{
8
    use Helper, CheckTrait;
9
10
    private static $instance = null;
11
    private array $routers = [];
12
    private ?string $prefix = null;
13
    private $group = false;
14
    private $lastReturn = null;
15
    private bool $instanced = false;
16
17
    public function __construct()
18
    {
19
        return $this;
20
    }
21
22
    public static function create(): Router
23
    {
24
        if(!self::getInstance()->instanced){
25
            self::getInstance()->checkConfig();
26
            self::getInstance()->import(ROUTER_CONFIG['path']);
27
            self::getInstance()->instanced = true;
28
        }
29
        
30
        return self::getInstance();
31
    }
32
33
    public static function getInstance(): Router
34
    {
35
        self::$instance = (is_null(self::$instance)) ? new self() : self::$instance;
36
        return self::$instance;
37
    }
38
39
    public static function any(string $uri, $walking): Router
40
    {
41
        self::getInstance()->add($uri, $walking, 'get');
42
        self::getInstance()->add($uri, $walking, 'post');
43
        return self::getInstance()->add($uri, $walking, 'ajax');
44
    }
45
46
    public static function get(string $uri, $walking): Router
47
    {
48
        return self::getInstance()->add($uri, $walking, 'get');
49
    }
50
51
    public static function post(string $uri, $walking): Router
52
    {
53
        return self::getInstance()->add($uri, $walking, 'post');
54
    }
55
56
    public static function ajax(string $uri, $walking): Router
57
    {
58
        return self::getInstance()->add($uri, $walking, 'ajax');
59
    }
60
61
    public static function form(string $uri, $walking): Router
62
    {
63
        return self::getInstance()->add($uri, $walking, 'form');
64
    }
65
66
    public static function add(string $uri, $walking, string $protocol): Router
67
    {
68
        return self::getInstance()->set($uri, $walking, $protocol);
69
    }
70
71
    public function set($url ,$walking , string $protocol): Router
72
    {
73
		$url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url;
74
75
    	foreach($this->routers as $key => $value){
76
    		if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
77
                throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured.");
78
            }
79
        }
80
        
81
        $this->checkTypeRole($walking);
82
83
		$route = [
84
			'url' => $this->prefix.$url,
85
			'role' => $walking,
86
			'protocol' => $protocol,
87
			'filters' => null,
88
            'group' => self::getInstance()->group
89
		];
90
91
		$this->routers[] = $route;		
92
        
93
        return self::getInstance();
94
    }
95
96
    public static function group(string $prefix,$callback): Router
97
    {
98
        self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix;
99
        self::getInstance()->group = sha1(date('d/m/Y h:m:i'));
100
        $callback();
101
        self::getInstance()->group = null;
102
        self::getInstance()->prefix = null;
103
        self::getInstance()->lastReturn = true;
104
        return self::getInstance();
105
    }
106
107
    public static function name(string $name): Router
108
    {
109
        if(self::getInstance()->lastReturn){
110
            throw new Exception("There is no reason to call a {$name} route group.");
111
        }
112
113
        $currentRoute = end(self::getInstance()->routers);
114
115
        foreach(self::getInstance()->routers as $key => $value){
116
            if(array_key_exists($name, self::getInstance()->routers)){
117
                throw new Exception("There is already a route with the name {$name} configured.");
118
            }
119
        }
120
121
        $currentRoute['name'] = $name;
122
123
        self::getInstance()->routers[count(self::getInstance()->routers)-1] = $currentRoute;
124
125
        self::getInstance()->lastReturn = null;
126
127
        return self::getInstance();
128
    }
129
130
    private function byName(?string $routName)
131
    {
132
        if(!is_null($routName)){
133
            $currentProtocol = $this->getProtocol();
134
135
            $this->checkName($routName);
136
    
137
            $route = $this->routers[$routName];
138
    
139
            if(!$this->checkProtocol($route['protocol'], $currentProtocol)){
140
                throw new Exception('Page not found.',404);
141
            }
142
    
143
            $this->checkFiltering($route);
144
    
145
            $this->toHiking($route['role']);
146
            throw true;
147
        }
148
    }
149
150
    public static function dispatch(?string $routeName = null): bool
151
    {
152
        $instance = self::create();
153
154
        $instance->getInstance()->byName($routeName);
155
156
		$currentProtocol = $instance->getInstance()->getProtocol();
157
158
        foreach(array_reverse($instance->getInstance()->routers) as $r => $route){
159
160
            $instance->getInstance()->currentRoute = $route;
161
162
            if(!$instance->getInstance()->checkProtocol($route['protocol'], $currentProtocol)){
163
                continue;
164
            }
165
166
            $instance->getInstance()->hasProtocol($route, $currentProtocol);
167
168
            $routeLoop = $instance->getInstance()->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']);
169
            
170
            $_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : '';
171
172
            $routeRequest = $instance->getInstance()->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']);
173
174
	        if($instance->getInstance()->checkNumparams($routeLoop, $routeRequest) || !$instance->getInstance()->checkParameters($routeLoop, $routeRequest)){
175
                continue;
176
            }
177
            
178
            $instance->getInstance()->checkFiltering($route);
179
180
            $instance->getInstance()->toHiking($route['role']);
181
	        return true;
182
        }
183
        
184
        $instance->getInstance()->currentRoute = null;
185
186
	    throw new Exception('Page not found.',404);
187
    }
188
189
    public static function filter($filters): Router
190
    {
191
        if(self::getInstance()->lastReturn !== null){
192
            $currentGroup = end(self::getInstance()->routers)['group'];
193
194
            foreach (self::getInstance()->routers as $key => $value) {
195
196
                if($value['group'] === $currentGroup){
197
                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters);
198
                    self::getInstance()->routers[$key] = $currentRoute;
199
                }
200
201
            }
202
203
            self::getInstance()->lastReturn = null;
204
            
205
            return self::getInstance();
206
        }
207
        
208
        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
209
        return self::getInstance();
210
    }
211
212
    public static function addFilter(array $route, $filter): array
213
    {
214
        if(is_null($route['filters'])){
215
            $route['filters'] = $filter;
216
            return $route;
217
        }
218
219
        $filters = (is_array($filter)) ? $filter : [0 => $filter];
220
221
        if(is_array($route['filters'])){
222
            foreach ($route['filters'] as $key => $value) {
223
                $filters[] = $value;
224
            }
225
        }else{
226
            $filters[] = $route['filters'];
227
        }
228
229
        $route['filters'] = $filters;
230
        return $route;
231
    }
232
233
}
234