1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\traits; |
4
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UString; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Ubiquity\controllers\traits$RouterTestTrait |
9
|
|
|
* This class is part of Ubiquity |
10
|
|
|
* |
11
|
|
|
* @author jcheron <[email protected]> |
12
|
|
|
* @version 1.0.0 |
13
|
|
|
* |
14
|
|
|
* @property array $routes |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
trait RouterTestTrait { |
18
|
|
|
|
19
|
|
|
public static function testRoutes($path, $method = null) { |
20
|
|
|
$response = [ ]; |
21
|
|
|
if(isset($path)){ |
22
|
|
|
$path = self::slashPath ( $path ); |
23
|
|
|
if (isset ( self::$routes [$path] )) { |
24
|
|
|
self::addTestRoute ( $response, $path, $method ); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
foreach ( self::$routes as $routePath => $routeDetails ) { |
28
|
|
|
if (preg_match ( "@^" . $routePath . "$@s", $path ) || $path == null) { |
29
|
|
|
self::addTestRoute ( $response, $routePath, $method ); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
return $response; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private static function addTestRoute(&$response, $path, $method = null) { |
36
|
|
|
if (isset ( $method )) { |
37
|
|
|
$restrict = false; |
38
|
|
|
if (UString::startswith ( $method, '-' )) { |
39
|
|
|
$restrict = true; |
40
|
|
|
$method = ltrim ( $method, '-' ); |
41
|
|
|
} |
42
|
|
|
$routeMethod = self::getMethod ( self::$routes [$path] ); |
43
|
|
|
if ((sizeof($routeMethod)==0 && ! $restrict) || array_search( strtolower ( $method ),$routeMethod)!==false) { |
44
|
|
|
$response [$path] = self::$routes [$path]; |
45
|
|
|
} |
46
|
|
|
} else { |
47
|
|
|
$response [$path] = self::$routes [$path]; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private static function getMethod($routeDetails) { |
52
|
|
|
if (! isset ( $routeDetails ["controller"] )) { |
53
|
|
|
return array_keys($routeDetails); |
54
|
|
|
} |
55
|
|
|
return []; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|