Passed
Push — master ( e0eeef...ff46e0 )
by Henri
01:44
created

Router::addFilter()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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