Router::parseRegexRoute()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 13
nc 1
nop 2
1
<?php
2
3
namespace Fastpress\Routing;
4
5
class Router{
6
    
7
    public $routes = [
8
        'GET'    => [],
9
        'POST'   => [],
10
        'PUT'    => [],
11
        'DELETE' => [],   
12
    ];
13
14
    public $patterns = [
15
        ':any'  => '.*',
16
        ':id'   => '[0-9]+',
17
        ':slug' => '[a-z-0-9\-]+',
18
        ':name' => '[a-zA-Z]+',
19
    ];
20
    
21
    const REGVAL = '/({:.+?})/';    
22
   
23
    public function any($path, $handler){
24
        $this->addRoute('GET', $path, $handler);
25
        $this->addRoute('POST', $path, $handler);
26
        $this->addRoute('PUT', $path, $handler);
27
        $this->addRoute('DELETE', $path, $handler);
28
    }
29
30
    public function get($path, $handler){
31
        $this->addRoute('GET', $path, $handler);
32
    }
33
    
34
    public function post($path, $handler){
35
        $this->addRoute('POST', $path, $handler);
36
    }
37
    
38
    public function put($path, $handler){
39
        $this->addRoute('PUT', $path, $handler);
40
    }
41
42
    public function delete($path, $handler){
43
        $this->addRoute('DELETE', $path, $handler);
44
    }
45
46
    protected function addRoute($method, $path, $handler){
47
        array_push($this->routes[$method], [$path => $handler]);
48
    }
49
50
    public function match(array $server = [], array $post){
51
        $requestMethod = $server['REQUEST_METHOD'];
52
        $requestUri    = $server['REQUEST_URI'];
53
54
        $restMethod = $this->getRestfullMethod($post); 
55
      
56
        if (null === $restMethod && !in_array($requestMethod, array_keys($this->routes))) {
57
            return false;
58
        }
59
        
60
        $method = $restMethod ?: $requestMethod;
61
62
        foreach ($this->routes[$method]  as $resource) {
63
64
            $args    = []; 
65
            $route   = key($resource); 
66
            $handler = reset($resource);
67
68
            if(preg_match(self::REGVAL, $route)){
69
                list($args, ,$route) = $this->parseRegexRoute($requestUri, $route);  
70
            }
71
72
            if(!preg_match("#^$route$#", $requestUri)){
73
                unset($this->routes[$method]);
74
                continue ;
75
            }
76
77
            if(is_string($handler) && strpos($handler, '@')){
78
                list($ctrl, $method) = explode('@', $handler); 
79
                return ['controller' => $ctrl, 'method' => $method, 'args' => $args];
80
            }
81
82
            if(empty($args)){
83
                return $handler(); 
84
            }
85
86
             return call_user_func_array($handler, $args);
87
88
          }
89
     }
90
   
91
    protected function getRestfullMethod($postVar){
92
        if(array_key_exists('_method', $postVar)){
93
        	$method = strtoupper($postVar['_method']);
94
            if(in_array($method, array_keys($this->routes))){
95
                return $method;
96
            }
97
        }
98
    } 
99
100
    protected function parseRegexRoute($requestUri, $resource){
101
        $route = preg_replace_callback(self::REGVAL, function($matches) {
102
            $patterns = $this->patterns; 
103
            $matches[0] = str_replace(['{', '}'], '', $matches[0]);
104
            
105
            if(in_array($matches[0], array_keys($patterns))){                       
106
                return  $patterns[$matches[0]];
107
            }
108
109
        }, $resource);
110
111
        $regUri = explode('/', $resource); 
112
       
113
        $args = array_diff(
114
                    array_replace($regUri, 
115
                    explode('/', $requestUri)
116
                ), $regUri
117
            );  
118
119
        return [array_values($args), $resource, $route]; 
120
    }
121
}
122