1
1
<?php
2
-
3
2
namespace Ubiquity \controllers \admin \popo ;
4
3
5
4
/**
6
5
* Ubiquity\controllers\admin\popo$Route
7
6
* This class is part of Ubiquity
7
+ *
8
8
* @author jcheron <[email protected] >
9
9
* @version 1.0.0
10
10
* @package ubiquity.dev
11
11
*
12
12
*/
13
13
class Route {
14
+
14
15
private $ path ;
16
+
15
17
private $ controller ;
18
+
16
19
private $ action ;
17
- private $ parameters =[];
20
+
21
+ private $ parameters = [];
22
+
18
23
private $ cache ;
24
+
19
25
private $ duration ;
26
+
20
27
private $ name ;
28
+
21
29
private $ methods ;
30
+
22
31
private $ id ;
32
+
23
33
private $ messages ;
24
34
25
- public function __construct ($ path= "" ,$ array= []){
26
- if (isset ($ array ['controller ' ])){
27
- $ this ->messages = [];
28
- $ this ->path = $ path ;
29
- $ this ->methods = $ array ['methods ' ]?? '' ;
35
+ public function __construct ($ path = "" , $ array = []) {
36
+ if (isset ($ array ['controller ' ])) {
37
+ $ this ->messages = [];
38
+ $ this ->path = $ path ;
39
+ $ this ->methods = $ array ['methods ' ] ?? '' ;
30
40
$ this ->fromArray ($ array );
31
- $ this ->id = \uniqid ();
41
+ $ this ->id = \uniqid ();
32
42
}
33
43
}
34
-
35
- private static function mergeRouteArray ($ routeArrays ){
36
- $ response= [];
37
- foreach ($ routeArrays as $ method=> $ route ){
38
- $ routeName= $ route ['name ' ];
39
- if (! isset ($ response [$ routeName ])){
40
- $ response [$ routeName ]= $ route ;
44
+
45
+ private static function mergeRouteArray ($ routeArrays ) {
46
+ $ response = [];
47
+ foreach ($ routeArrays as $ method => $ route ) {
48
+ $ routeName = $ route ['name ' ];
49
+ if (! isset ($ response [$ routeName ])) {
50
+ $ response [$ routeName ] = $ route ;
41
51
}
42
- $ response [$ routeName ]['methods ' ][]= $ method ;
52
+ $ response [$ routeName ]['methods ' ][] = $ method ;
43
53
}
44
54
return $ response ;
45
55
}
46
56
47
- private function fromArray ($ array ){
48
- $ this ->controller = $ array ["controller " ];
49
- $ this ->action = $ array ["action " ];
50
- $ this ->name = isset ($ array ["name " ])? $ array ["name " ]: '' ;
51
- $ this ->cache = isset ($ array ["cache " ])? $ array ["cache " ]: false ;
52
- $ this ->duration = isset ($ array ["duration " ])? $ array ["duration " ]: false ;
53
- if (isset ($ array ["parameters " ]) && \sizeof ($ array ["parameters " ])> 0 ) {
54
- if (\class_exists ($ this ->controller )){
55
- if (\method_exists ($ this ->controller , $ this ->action )){
56
- $ method= new \ReflectionMethod ($ this ->controller ,$ this ->action );
57
- $ params= $ method ->getParameters ();
58
- foreach ($ array ["parameters " ] as $ paramIndex ){
59
- if ($ paramIndex ==="* " ){
60
- $ pName= $ this ->getVariadicParam ($ params );
61
- if ($ pName !==false ){
62
- $ this ->parameters []= "... " . $ pName ;
57
+ private function fromArray ($ array ) {
58
+ $ this ->controller = $ array ["controller " ];
59
+ $ this ->action = $ array ["action " ];
60
+ $ this ->name = isset ($ array ["name " ]) ? $ array ["name " ] : '' ;
61
+ $ this ->cache = isset ($ array ["cache " ]) ? $ array ["cache " ] : false ;
62
+ $ this ->duration = isset ($ array ["duration " ]) ? $ array ["duration " ] : false ;
63
+ if (isset ($ array ["parameters " ]) && \sizeof ($ array ["parameters " ]) > 0 ) {
64
+ if (\class_exists ($ this ->controller )) {
65
+ if (\method_exists ($ this ->controller , $ this ->action )) {
66
+ $ method = new \ReflectionMethod ($ this ->controller , $ this ->action );
67
+ $ params = $ method ->getParameters ();
68
+ foreach ($ array ["parameters " ] as $ paramIndex ) {
69
+ if ($ paramIndex === "* " ) {
70
+ $ pName = $ this ->getVariadicParam ($ params );
71
+ if ($ pName !== false ) {
72
+ $ this ->parameters [] = "... " . $ pName ;
63
73
}
64
- }else {
65
- $ index= \intval (\str_replace ("~ " , "" , $ paramIndex ));
66
- if (isset ($ params [$ index ])){
67
- if (\substr ($ paramIndex ,0 , 1 ) ==="~ " )
68
- $ this ->parameters []= $ params [$ index ]->getName ();
74
+ } else {
75
+ $ index = \intval (\str_replace ("~ " , "" , $ paramIndex ));
76
+ if (isset ($ params [$ index ])) {
77
+ if (\substr ($ paramIndex , 0 , 1 ) === "~ " )
78
+ $ this ->parameters [] = $ params [$ index ]->getName ();
69
79
else
70
- $ this ->parameters []= $ params [$ index ]->getName (). "* " ;
80
+ $ this ->parameters [] = $ params [$ index ]->getName () . "* " ;
71
81
}
72
82
}
73
83
}
74
- }else {
75
- $ this ->messages []= "The method <b> " . $ this ->action . "</b> does not exists in the class <b> " . $ this ->controller . "</b>. \n" ;
84
+ } else {
85
+ $ this ->messages [] = "The method <b> " . $ this ->action . "</b> does not exists in the class <b> " . $ this ->controller . "</b>. \n" ;
76
86
}
77
- }else {
78
- $ this ->messages [$ this ->controller ]= "The class <b> " . $ this ->controller . "</b> does not exist. \n" ;
87
+ } else {
88
+ $ this ->messages [$ this ->controller ] = "The class <b> " . $ this ->controller . "</b> does not exist. \n" ;
79
89
}
80
90
}
81
91
}
82
- private function getVariadicParam ($ parameters ){
83
- foreach ($ parameters as $ param ){
84
- if ($ param ->isVariadic ()){
92
+
93
+ private function getVariadicParam ($ parameters ) {
94
+ foreach ($ parameters as $ param ) {
95
+ if ($ param ->isVariadic ()) {
85
96
return $ param ->getName ();
86
97
}
87
98
}
@@ -93,7 +104,7 @@ public function getPath() {
93
104
}
94
105
95
106
public function setPath ($ path ) {
96
- $ this ->path = $ path ;
107
+ $ this ->path = $ path ;
97
108
return $ this ;
98
109
}
99
110
@@ -102,7 +113,7 @@ public function getController() {
102
113
}
103
114
104
115
public function setController ($ controller ) {
105
- $ this ->controller = $ controller ;
116
+ $ this ->controller = $ controller ;
106
117
return $ this ;
107
118
}
108
119
@@ -111,7 +122,7 @@ public function getAction() {
111
122
}
112
123
113
124
public function setAction ($ action ) {
114
- $ this ->action = $ action ;
125
+ $ this ->action = $ action ;
115
126
return $ this ;
116
127
}
117
128
@@ -120,7 +131,7 @@ public function getParameters() {
120
131
}
121
132
122
133
public function setParameters ($ parameters ) {
123
- $ this ->parameters = $ parameters ;
134
+ $ this ->parameters = $ parameters ;
124
135
return $ this ;
125
136
}
126
137
@@ -129,7 +140,7 @@ public function getCache() {
129
140
}
130
141
131
142
public function setCache ($ cache ) {
132
- $ this ->cache = $ cache ;
143
+ $ this ->cache = $ cache ;
133
144
return $ this ;
134
145
}
135
146
@@ -138,7 +149,7 @@ public function getDuration() {
138
149
}
139
150
140
151
public function setDuration ($ duration ) {
141
- $ this ->duration = $ duration ;
152
+ $ this ->duration = $ duration ;
142
153
return $ this ;
143
154
}
144
155
@@ -147,26 +158,32 @@ public function getName() {
147
158
}
148
159
149
160
public function setName ($ name ) {
150
- $ this ->name = $ name ;
161
+ $ this ->name = $ name ;
151
162
return $ this ;
152
163
}
153
164
154
- public function getCompiledParams (){
155
- return " ( " . ((\is_array ($ this ->parameters ))? \implode (", " , $ this ->parameters ): $ this ->parameters ). ") " ;
165
+ public function getCompiledParams () {
166
+ return " ( " . ((\is_array ($ this ->parameters )) ? \implode (", " , $ this ->parameters ) : $ this ->parameters ) . ") " ;
156
167
}
157
168
158
- public static function init ($ array ){
159
- $ result= [];
160
- foreach ($ array as $ k=> $ v ){
161
- if (isset ($ v ['controller ' ])){
162
- $ result []= new Route ($ k , $ v );
163
- }else {
164
- $ routes= self ::mergeRouteArray ($ v );
165
- foreach ($ routes as $ route ){
166
- $ result []= new Route ($ k ,$ route );
169
+ public static function init ($ array ) {
170
+ $ result = [];
171
+ foreach ($ array as $ k => $ v ) {
172
+ if (isset ($ v ['controller ' ])) {
173
+ $ result [] = new Route ($ k , $ v );
174
+ } else {
175
+ $ routes = self ::mergeRouteArray ($ v );
176
+ foreach ($ routes as $ route ) {
177
+ $ result [] = new Route ($ k , $ route );
167
178
}
168
179
}
169
180
}
181
+ \usort ($ result , function ($ left , $ right ) {
182
+ if ($ left ->getController () !== $ right ->getController ()) {
183
+ return $ left ->getController () <=> $ right ->getController ();
184
+ }
185
+ return $ left ->getAction () <=> $ right ->getAction ();
186
+ });
170
187
return $ result ;
171
188
}
172
189
@@ -183,7 +200,7 @@ public function getMethods() {
183
200
}
184
201
185
202
public function setMethods ($ methods ) {
186
- $ this ->methods = $ methods ;
203
+ $ this ->methods = $ methods ;
187
204
return $ this ;
188
205
}
189
206
}
0 commit comments