Passed
Push — master ( 81da44...a02115 )
by Henri
01:33
created

Router::defineMiddlewares()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 1
dl 0
loc 4
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
use HnrAzevedo\Http\ServerRequest;
6
7
class Router{
8
    use Helper, DefinitionsTrait, ExtraJobsTrait;
9
10
    private static Router $instance;
11
    private ?string $group = null;
12
    private $beforeAll = null;
13
    private $afterAll = null;
14
    private array $afterExcepts = [];
15
    private array $beforeExcepts = [];
16
    private bool $instanced = false;
17
    private string $host = '';
18
    private RequestHandler $request;
0 ignored issues
show
introduced by
The private property $request is not used, and could be removed.
Loading history...
19
    private ServerRequest $serverRequest;
0 ignored issues
show
introduced by
The private property $serverRequest is not used, and could be removed.
Loading history...
20
21
    public function __construct()
22
    {
23
        return $this;
24
    }
25
26
    public static function create(): Router
27
    {
28
        if(!self::getInstance()->instanced){
29
            self::getInstance()->instanced = true;
30
        }
31
        
32
        return self::getInstance();
33
    }
34
35
    public static function getInstance(): Router
36
    {
37
        self::$instance = (!isset(self::$instance)) ? new self() : self::$instance;
38
        return self::$instance;
39
    }
40
41
    public static function host(string $host): Router
42
    {
43
        self::getInstance()->host = $host;
44
        return self::getInstance();
45
    }
46
47
    public function set(string $url ,$walking , string $protocol): Router
48
    {
49
        $this->lastReturn = null;
50
        
51
		$url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url;
52
53
    	$this->checkExistence($url,$protocol);
54
        $this->checkTypeRole($walking);
55
56
		$route = [
57
			'url' => $this->prefix.$url,
58
			'role' => $walking,
59
			'protocol' => $protocol,
60
            'middlewares' => null,
61
            'where' => null,
62
            'before' => null,
63
            'beforeAll' => $this->beforeAll,
64
            'after' => null,
65
            'afterAll' => $this->afterAll,
66
            'group' => self::getInstance()->group
67
		];
68
69
		$this->routers[] = $route;		
70
        
71
        return self::getInstance();
72
    }
73
74
    public static function group(string $prefix, callable $callback): Router
75
    {
76
        self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix;
77
        self::getInstance()->group = sha1(date('d/m/Y h:m:i'));
78
        $callback();
79
        self::getInstance()->group = null;
80
        self::getInstance()->prefix = null;
81
        self::getInstance()->lastReturn = true;
82
        return self::getInstance();
83
    }
84
85
    public static function where(): Router
86
    {
87
        return self::getInstance()->checkInGroup()->callWhereAdd(func_get_args());
88
    }
89
90
    public static function name(string $name): Router
91
    {
92
        self::getInstance()->checkInGroup()->hasRouteName($name);
93
94
        $currentRoute = end(self::getInstance()->routers);
95
96
        if(in_array($name,self::getInstance()->beforeExcepts)){
97
            $currentRoute['beforeAll'] = null;
98
        }
99
100
        if(in_array($name,self::getInstance()->afterExcepts)){
101
            $currentRoute['afterAll'] = null;
102
        }
103
104
        unset(self::getInstance()->routers[count(self::getInstance()->routers)-1]);
105
        
106
        self::getInstance()->routers[$name] = $currentRoute;
107
108
        return self::getInstance();
109
    }
110
111
    public static function load(?string $routeName = null): Router
112
    {
113
        return (!is_null($routeName)) ? self::create()->getInstance()->loadByName($routeName) : self::create()->getInstance()->loadByArray();
114
    }
115
116
    public static function dispatch(?string $routeName = null)
117
    {
118
        $instance = self::create()->getInstance();
119
120
        if(!$instance->loaded){
121
            self::load($routeName);
122
        }
123
124
        $instance->checkMiddleware($instance->currentRoute)->toHiking($instance->currentRoute);
125
    }
126
127
    public function middleware($middlewares): Router
128
    {
129
        if($this->lastReturn !== null){
130
            $currentGroup = end($this->routers)['group'];
131
132
            foreach ($this->routers as $key => $value) {
133
                if($value['group'] === $currentGroup){
134
                    $this->routers[$key] = $this->addMiddleware($this->routers[$key],$middlewares);
135
                }
136
            }
137
            
138
            return $this;
139
        }
140
        
141
        $this->routers[count($this->routers)-1] = $this->addMiddleware(end($this->routers),$middlewares);
142
        return $this;
143
    }
144
145
    public static function addMiddleware(array $route, $filter): array
146
    {
147
        if(is_null($route['middlewares'])){
148
            $route['middlewares'] = $filter;
149
            return $route;
150
        }
151
152
        $middlewares = (is_array($filter)) ? $filter : [0 => $filter];
153
154
        if(is_array($route['middlewares'])){
155
            foreach ($route['middlewares'] as $key => $value) {
156
                $middlewares[] = $value;
157
            }
158
        }else{
159
            $middlewares[] = $route['middlewares'];
160
        }
161
162
        $route['middlewares'] = $middlewares;
163
        return $route;
164
    }
165
166
    public static function defineMiddlewares(array $middlewares): Router
167
    {
168
        self::getInstance()->middlewares = $middlewares;
169
        return self::getInstance();
170
    }
171
172
}
173