Skip to content

Commit 25de67d

Browse files
committed
sort routes by controller in router part
1 parent 2803a1c commit 25de67d

File tree

1 file changed

+81
-64
lines changed

1 file changed

+81
-64
lines changed
Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,98 @@
11
<?php
2-
32
namespace Ubiquity\controllers\admin\popo;
43

54
/**
65
* Ubiquity\controllers\admin\popo$Route
76
* This class is part of Ubiquity
7+
*
88
* @author jcheron <[email protected]>
99
* @version 1.0.0
1010
* @package ubiquity.dev
1111
*
1212
*/
1313
class Route {
14+
1415
private $path;
16+
1517
private $controller;
18+
1619
private $action;
17-
private $parameters=[];
20+
21+
private $parameters = [];
22+
1823
private $cache;
24+
1925
private $duration;
26+
2027
private $name;
28+
2129
private $methods;
30+
2231
private $id;
32+
2333
private $messages;
2434

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'] ?? '';
3040
$this->fromArray($array);
31-
$this->id=\uniqid();
41+
$this->id = \uniqid();
3242
}
3343
}
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;
4151
}
42-
$response[$routeName]['methods'][]=$method;
52+
$response[$routeName]['methods'][] = $method;
4353
}
4454
return $response;
4555
}
4656

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;
6373
}
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();
6979
else
70-
$this->parameters[]=$params[$index]->getName()."*";
80+
$this->parameters[] = $params[$index]->getName() . "*";
7181
}
7282
}
7383
}
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";
7686
}
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";
7989
}
8090
}
8191
}
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()) {
8596
return $param->getName();
8697
}
8798
}
@@ -93,7 +104,7 @@ public function getPath() {
93104
}
94105

95106
public function setPath($path) {
96-
$this->path=$path;
107+
$this->path = $path;
97108
return $this;
98109
}
99110

@@ -102,7 +113,7 @@ public function getController() {
102113
}
103114

104115
public function setController($controller) {
105-
$this->controller=$controller;
116+
$this->controller = $controller;
106117
return $this;
107118
}
108119

@@ -111,7 +122,7 @@ public function getAction() {
111122
}
112123

113124
public function setAction($action) {
114-
$this->action=$action;
125+
$this->action = $action;
115126
return $this;
116127
}
117128

@@ -120,7 +131,7 @@ public function getParameters() {
120131
}
121132

122133
public function setParameters($parameters) {
123-
$this->parameters=$parameters;
134+
$this->parameters = $parameters;
124135
return $this;
125136
}
126137

@@ -129,7 +140,7 @@ public function getCache() {
129140
}
130141

131142
public function setCache($cache) {
132-
$this->cache=$cache;
143+
$this->cache = $cache;
133144
return $this;
134145
}
135146

@@ -138,7 +149,7 @@ public function getDuration() {
138149
}
139150

140151
public function setDuration($duration) {
141-
$this->duration=$duration;
152+
$this->duration = $duration;
142153
return $this;
143154
}
144155

@@ -147,26 +158,32 @@ public function getName() {
147158
}
148159

149160
public function setName($name) {
150-
$this->name=$name;
161+
$this->name = $name;
151162
return $this;
152163
}
153164

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) . ")";
156167
}
157168

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);
167178
}
168179
}
169180
}
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+
});
170187
return $result;
171188
}
172189

@@ -183,7 +200,7 @@ public function getMethods() {
183200
}
184201

185202
public function setMethods($methods) {
186-
$this->methods=$methods;
203+
$this->methods = $methods;
187204
return $this;
188205
}
189206
}

0 commit comments

Comments
 (0)