1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* KNUT7 K7F (https://marciozebedeu.com/) |
4
|
|
|
* KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
9
|
|
|
* |
10
|
|
|
* @link https://github.com/knut7/framework/ for the canonical source repository |
11
|
|
|
* @copyright (c) 2015. KNUT7 Software Technologies AO Inc. (https://marciozebedeu.com/) |
12
|
|
|
* @license https://marciozebedeu.com/license/new-bsd New BSD License |
13
|
|
|
* @author Marcio Zebedeu - [email protected] |
14
|
|
|
* @version 1.0.2 |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Ballybran\Helpers\Routing; |
18
|
|
|
|
19
|
|
|
use Ballybran\Core\Http\RestUtilities; |
20
|
|
|
use Ballybran\Exception\KException; |
21
|
|
|
use Ballybran\Helpers\vardump\Vardump; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Map |
25
|
|
|
* @package Ballybran\Helpers\Routing |
26
|
|
|
*/ |
27
|
|
|
class Map |
28
|
|
|
{ |
29
|
|
|
private $url; |
30
|
|
|
private $callable; |
|
|
|
|
31
|
|
|
private $routes = []; |
32
|
|
|
private $nameRoute; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
if (!empty($_SERVER['REQUEST_URI'])) { |
38
|
|
|
$this->url = trim($_SERVER['REQUEST_URI'], '/'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $path |
45
|
|
|
* @param $callable |
46
|
|
|
* @param null $name |
|
|
|
|
47
|
|
|
* @return Route |
48
|
|
|
* example $router->add( '/user/:id', function($id) {}, 'name')->with('id','[0-9]+'); |
49
|
|
|
*/ |
50
|
|
|
public function get($path, $callable, $name = null) : Routes |
51
|
|
|
{ |
52
|
|
|
|
53
|
|
|
/** @var TYPE_NAME $callable */ |
54
|
|
|
return self::add($path, $callable, $name, 'GET'); |
|
|
|
|
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $path |
60
|
|
|
* @param $callable |
61
|
|
|
* @param null $name |
|
|
|
|
62
|
|
|
* @return Routes |
63
|
|
|
* example $router->post( '/user/:id', function() {}, 'name') |
64
|
|
|
*/ |
65
|
|
|
public function post($path, $callable, $name = null) : Routes |
66
|
|
|
{ |
67
|
|
|
|
68
|
|
|
return self::add($path, $callable, $name, 'POST'); |
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $path |
75
|
|
|
* @param $callable |
76
|
|
|
* @param $name |
77
|
|
|
* @param $method |
78
|
|
|
* @return Routes |
79
|
|
|
* example $router->add( '/user/:id', function($id) {}, 'name')->with('id','[0-9]+'); |
80
|
|
|
*/ |
81
|
|
|
public function add($path, $callable, $name, $method) : Routes |
82
|
|
|
{ |
83
|
|
|
$route = new Routes($path, $callable); |
84
|
|
|
$this->routes[$method][] = $route; |
85
|
|
|
|
86
|
|
|
if (is_string($callable) && $name === null) { |
87
|
|
|
|
88
|
|
|
$name = $callable; |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
if ($name) { |
92
|
|
|
$this->nameRoute[$name] = $route; |
93
|
|
|
} |
94
|
|
|
return $route; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function run() |
99
|
|
|
{ |
100
|
|
|
if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) { |
101
|
|
|
throw new \Exception(' REQUEST_METHOD does not exists', 1); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) { |
105
|
|
|
|
106
|
|
|
if ($route->match($this->url)) { |
107
|
|
|
|
108
|
|
|
return $route->call(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
RestUtilities::sendResponse(200); |
113
|
|
|
KException::notFound(); |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
/* |
117
|
|
|
* @param $name |
118
|
|
|
* @param $params array optional |
119
|
|
|
* $router->url('name', ['id'=> 1] ) |
120
|
|
|
* example $router->get( '/user/:id', function($id) { return $router->url('name', ['id'=> 1] ) }, 'name')->with('id','[0-9]+'); |
121
|
|
|
*/ |
122
|
|
|
|
123
|
|
|
public function url($name, $params = []) |
124
|
|
|
{ |
125
|
|
|
if (!isset($this->nameRoute[$name])) { |
126
|
|
|
|
127
|
|
|
throw new \Exception("No route match this name", 1); |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->nameRoute[$name]->getUrl($params); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths