|
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.1 |
|
13
|
|
|
* |
|
14
|
|
|
* @property array $routes |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
trait RouterTestTrait { |
|
18
|
|
|
|
|
19
|
1 |
|
public static function testRoutes($path, $method = null): array { |
|
20
|
1 |
|
$response = [ ]; |
|
21
|
1 |
|
if (isset ( $path )) { |
|
22
|
1 |
|
$path = self::slashPath ( $path ); |
|
23
|
1 |
|
if (isset ( self::$routes [$path] )) { |
|
24
|
|
|
self::addTestRoute ( $response, $path, $method ); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
1 |
|
foreach ( self::$routes as $routePath => $routeDetails ) { |
|
28
|
1 |
|
if (\preg_match ( "@^{$routePath}\$@s", $path ) || $path == null) { |
|
29
|
1 |
|
self::addTestRoute ( $response, $routePath, $method ); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
1 |
|
return $response; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
private static function addTestRoute(&$response, $path, $method = null): void { |
|
36
|
1 |
|
if (isset ( $method )) { |
|
37
|
1 |
|
$restrict = false; |
|
38
|
1 |
|
if (UString::startswith ( $method, '-' )) { |
|
39
|
|
|
$restrict = true; |
|
40
|
|
|
$method = \ltrim ( $method, '-' ); |
|
41
|
|
|
} |
|
42
|
1 |
|
$routeMethod = self::getMethod ( self::$routes [$path] ); |
|
43
|
1 |
|
if ((sizeof ( $routeMethod ) == 0 && ! $restrict) || \array_search ( \strtolower ( $method ), $routeMethod ) !== false) { |
|
44
|
1 |
|
$response [$path] = self::$routes [$path]; |
|
45
|
|
|
} |
|
46
|
|
|
} else { |
|
47
|
|
|
$response [$path] = self::$routes [$path]; |
|
48
|
|
|
} |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
private static function getMethod($routeDetails): array { |
|
52
|
1 |
|
if (! isset ( $routeDetails ['controller'] )) { |
|
53
|
|
|
return \array_keys ( $routeDetails ); |
|
54
|
|
|
} |
|
55
|
1 |
|
return [ ]; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|