1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Router\RouteFactory; |
4
|
|
|
|
5
|
|
|
use Nip\Router\Route\LiteralRoute; |
6
|
|
|
use Nip\Router\Route\Route; |
7
|
|
|
use Nip\Router\Route\StandardRoute; |
8
|
|
|
use Nip\Router\RouteCollection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait HasBaseRoutesTrait |
12
|
|
|
* @package Nip\Router\RouteFactory |
13
|
|
|
*/ |
14
|
|
|
trait HasBaseRoutesTrait |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param RouteCollection $collection |
19
|
|
|
* @param $name |
20
|
|
|
* @param $class |
21
|
|
|
* @param string $mapPrefix |
22
|
|
|
* @param array $params |
23
|
|
|
* @return mixed |
24
|
|
|
*/ |
25
|
7 |
|
public static function generateIndexRoute( |
26
|
|
|
$collection, |
27
|
|
|
$name, |
28
|
|
|
$class = null, |
29
|
|
|
$mapPrefix = '', |
30
|
|
|
$params = [] |
31
|
|
|
) { |
32
|
7 |
|
$params["controller"] = "index"; |
33
|
7 |
|
$params["action"] = "index"; |
34
|
|
|
|
35
|
7 |
|
self::generateLiteralRoute( |
36
|
7 |
|
$collection, $name . '.slash', $class, $mapPrefix, '', $params |
37
|
|
|
); |
38
|
|
|
|
39
|
7 |
|
return self::generateLiteralRoute( |
40
|
7 |
|
$collection, $name, $class, $mapPrefix, '/', $params |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param RouteCollection $collection |
46
|
|
|
* @param $name |
47
|
|
|
* @param $class |
48
|
|
|
* @param string $mapPrefix |
49
|
|
|
* @param string $map |
50
|
|
|
* @param array $params |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
13 |
|
public static function generateLiteralRoute( |
54
|
|
|
$collection, |
55
|
|
|
$name, |
56
|
|
|
$class = null, |
57
|
|
|
$mapPrefix = '', |
58
|
|
|
$map = '/', |
59
|
|
|
$params = [] |
60
|
|
|
) { |
61
|
13 |
|
$map = $mapPrefix . $map; |
62
|
13 |
|
$class = empty($class) ? LiteralRoute::class : $class; |
63
|
|
|
|
64
|
13 |
|
return self::generateGenericRoute($collection, $name, $class, $map, $params); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param RouteCollection $collection |
69
|
|
|
* @param $name |
70
|
|
|
* @param $class |
71
|
|
|
* @param string $map |
72
|
|
|
* @param array $params |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
25 |
|
public static function generateGenericRoute( |
76
|
|
|
$collection, |
77
|
|
|
$name, |
78
|
|
|
$class = null, |
79
|
|
|
$map, |
80
|
|
|
$params = [] |
81
|
|
|
) { |
82
|
25 |
|
$map = str_replace('//', '/', $map); |
83
|
25 |
|
$class = static::generateRouteClassName($class); |
84
|
|
|
|
85
|
25 |
|
$route = new $class($map, $params); |
86
|
25 |
|
return $collection->addRoute($route, $name); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param RouteCollection $collection |
91
|
|
|
* @param $name |
92
|
|
|
* @param $class |
93
|
|
|
* @param string $mapPrefix |
94
|
|
|
* @param string $map |
95
|
|
|
* @param array $params |
96
|
|
|
*/ |
97
|
18 |
|
public static function generateStandardRoute( |
98
|
|
|
$collection, |
99
|
|
|
$name, |
100
|
|
|
$class = null, |
101
|
|
|
$mapPrefix = '', |
102
|
|
|
$map = null, |
103
|
|
|
$params = [] |
104
|
|
|
) { |
105
|
18 |
|
$class = empty($class) ? StandardRoute::class : $class; |
106
|
18 |
|
$map = $map ? $map : '/{controller}/{action?index}'; |
107
|
|
|
|
108
|
18 |
|
self::generateGenericRoute($collection, $name, $class, $mapPrefix . $map, $params); |
109
|
|
|
|
110
|
18 |
|
$params['action'] = 'index'; |
111
|
18 |
|
self::generateGenericRoute($collection, $name . '.index', $class, $mapPrefix . '/{controller}/', |
112
|
18 |
|
$params); |
113
|
18 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param null $class |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
25 |
|
protected static function generateRouteClassName($class = null) |
120
|
|
|
{ |
121
|
25 |
|
if (empty($class)) { |
122
|
|
|
return Route::class; |
123
|
|
|
} |
124
|
|
|
|
125
|
25 |
|
if (class_exists($class)) { |
126
|
21 |
|
return $class; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$maps = [ |
130
|
4 |
|
LiteralRoute::class => ['_Literal', '\Literal'], |
131
|
|
|
StandardRoute::class => ['_Standard', '\Standard'] |
132
|
|
|
]; |
133
|
4 |
|
foreach ($maps as $routeClass => $needles) { |
134
|
4 |
|
foreach ($needles as $needle) { |
135
|
4 |
|
if (strpos($class, $needle) !== false) { |
136
|
4 |
|
return $routeClass; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return Route::class; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|