1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Router; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class RouteCollection |
7
|
|
|
* @package Nip\Router |
8
|
|
|
*/ |
9
|
|
|
class RouteFactory |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param RouteCollection $collection |
14
|
|
|
* @param $name |
15
|
|
|
* @param $class |
16
|
|
|
* @param string $mapPrefix |
17
|
|
|
* @return mixed |
18
|
|
|
*/ |
19
|
1 |
|
public static function generateIndexRoute( |
20
|
|
|
$collection, |
21
|
|
|
$name, |
22
|
|
|
$class, |
23
|
|
|
$mapPrefix = '' |
24
|
|
|
) { |
25
|
1 |
|
$params = ["controller" => "index", "action" => "index"]; |
26
|
1 |
|
$map = '/'; |
27
|
|
|
|
28
|
1 |
|
return self::generateLiteralRoute( |
29
|
1 |
|
$collection, $name, $class, $mapPrefix, $map, $params |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param RouteCollection $collection |
35
|
|
|
* @param $name |
36
|
|
|
* @param $class |
37
|
|
|
* @param string $mapPrefix |
38
|
|
|
* @param string $map |
39
|
|
|
* @param array $params |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
6 |
|
public static function generateLiteralRoute( |
43
|
|
|
$collection, |
44
|
|
|
$name, |
45
|
|
|
$class, |
46
|
|
|
$mapPrefix = '', |
47
|
|
|
$map = '/', |
48
|
|
|
$params = [] |
49
|
|
|
) { |
50
|
6 |
|
$map = $mapPrefix . $map; |
51
|
|
|
|
52
|
6 |
|
return self::generateGenericRoute($collection, $name, $class, $map, $params); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param RouteCollection $collection |
57
|
|
|
* @param $name |
58
|
|
|
* @param $class |
59
|
|
|
* @param string $map |
60
|
|
|
* @param array $params |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
8 |
|
public static function generateGenericRoute( |
64
|
|
|
$collection, |
65
|
|
|
$name, |
66
|
|
|
$class, |
67
|
|
|
$map, |
68
|
|
|
$params = [] |
69
|
|
|
) { |
70
|
8 |
|
$map = str_replace('//', '/', $map); |
71
|
|
|
|
72
|
8 |
|
$route = new $class($map, $params); |
73
|
8 |
|
return $collection->addRoute($route, $name); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param RouteCollection $collection |
78
|
|
|
* @param $name |
79
|
|
|
* @param $class |
80
|
|
|
* @param string $mapPrefix |
81
|
|
|
* @param string $map |
82
|
|
|
* @param array $params |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
5 |
|
public static function generateStandardRoute( |
86
|
|
|
$collection, |
87
|
|
|
$name, |
88
|
|
|
$class, |
89
|
|
|
$mapPrefix = '', |
90
|
|
|
$map = '/:controller/:action', |
91
|
|
|
$params = [] |
92
|
|
|
) { |
93
|
5 |
|
return self::generateGenericRoute($collection, $name, $class, $mapPrefix . $map, $params); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|