Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Route.php
Go to the documentation of this file.
1<?php
3
6
16class Route {
17
18 private $path;
19
20 private $controller;
21
22 private $action;
23
24 private $parameters = [];
25
26 private $cache;
27
28 private $duration;
29
30 private $name;
31
32 private $methods;
33
34 private $id;
35
36 private $messages;
37
38 public function __construct($path = "", $array = []) {
39 if (isset($array['controller'])) {
40 $this->messages = [];
41 $this->path = $path;
42 $this->methods = $array['methods'] ?? '';
43 $this->fromArray($array);
44 $this->id = \uniqid();
45 }
46 }
47
48 private static function mergeRouteArray($routeArrays) {
49 $response = [];
50 foreach ($routeArrays as $method => $route) {
51 $routeName = $route['name'];
52 if (! isset($response[$routeName])) {
53 $response[$routeName] = $route;
54 }
55 $response[$routeName]['methods'][] = $method;
56 }
57 return $response;
58 }
59
60 private function fromArray($array) {
61 $this->controller = $array["controller"];
62 $this->action = $array["action"];
63 $this->name = isset($array["name"]) ? $array["name"] : '';
64 $this->cache = isset($array["cache"]) ? $array["cache"] : false;
65 $this->duration = isset($array["duration"]) ? $array["duration"] : false;
66 if (isset($array["parameters"]) && \sizeof($array["parameters"]) > 0) {
67 if (\class_exists($this->controller)) {
68 if (\method_exists($this->controller, $this->action)) {
69 $method = new \ReflectionMethod($this->controller, $this->action);
70 $params = $method->getParameters();
71 foreach ($array["parameters"] as $paramIndex) {
72 if ($paramIndex === "*") {
73 $pName = $this->getVariadicParam($params);
74 if ($pName !== false) {
75 $this->parameters[] = "..." . $pName;
76 }
77 } else {
78 $index = \intval(\str_replace("~", "", $paramIndex));
79 if (isset($params[$index])) {
80 if (\substr($paramIndex, 0, 1) === "~")
81 $this->parameters[] = $params[$index]->getName();
82 else
83 $this->parameters[] = $params[$index]->getName() . "*";
84 }
85 }
86 }
87 } else {
88 $this->messages[] = "The method <b>" . $this->action . "</b> does not exists in the class <b>" . $this->controller . "</b>.\n";
89 }
90 } else {
91 $this->messages[$this->controller] = "The class <b>" . $this->controller . "</b> does not exist.\n";
92 }
93 }
94 }
95
96 private function getVariadicParam($parameters) {
97 foreach ($parameters as $param) {
98 if ($param->isVariadic()) {
99 return $param->getName();
100 }
101 }
102 return false;
103 }
104
105 public function getPath() {
106 return $this->path;
107 }
108
109 public function setPath($path) {
110 $this->path = $path;
111 return $this;
112 }
113
114 public function getController() {
115 return $this->controller;
116 }
117
118 public function setController($controller) {
119 $this->controller = $controller;
120 return $this;
121 }
122
123 public function getAction() {
124 return $this->action;
125 }
126
127 public function setAction($action) {
128 $this->action = $action;
129 return $this;
130 }
131
132 public function getParameters() {
133 return $this->parameters;
134 }
135
136 public function setParameters($parameters) {
137 $this->parameters = $parameters;
138 return $this;
139 }
140
141 public function getCache() {
142 return $this->cache;
143 }
144
145 public function setCache($cache) {
146 $this->cache = $cache;
147 return $this;
148 }
149
150 public function getDuration() {
151 return $this->duration;
152 }
153
154 public function setDuration($duration) {
155 $this->duration = $duration;
156 return $this;
157 }
158
159 public function getName() {
160 return $this->name;
161 }
162
163 public function setName($name) {
164 $this->name = $name;
165 return $this;
166 }
167
168 public function getCompiledParams() {
169 return " (" . ((\is_array($this->parameters)) ? \implode(", ", $this->parameters) : $this->parameters) . ")";
170 }
171
172 public static function init(array $array,string $domain=''): array {
173 $result = [];
174 $ns=Startup::getNS();
175 foreach ($array as $k => $v) {
176 if (isset($v['controller'])) {
177 if(UString::startswith($v['controller'],$ns)) {
178 $result[] = new Route($k, $v);
179 }
180 } else {
181 $routes = self::mergeRouteArray($v);
182 foreach ($routes as $route) {
183 if(UString::startswith($route['controller'],$ns)) {
184 $result[] = new Route($k, $route);
185 }
186 }
187 }
188 }
189 \usort($result, function ($left, $right) {
190 if ($left->getController() !== $right->getController()) {
191 return $left->getController() <=> $right->getController();
192 }
193 return $left->getAction() <=> $right->getAction();
194 });
195 return $result;
196 }
197
198 public function getId() {
199 return $this->id;
200 }
201
202 public function getMessages() {
203 return $this->messages;
204 }
205
206 public function getMethods() {
207 return $this->methods;
208 }
209
210 public function setMethods($methods) {
211 $this->methods = $methods;
212 return $this;
213 }
214}
Starts the framework.
Definition Startup.php:19
static init(array $array, string $domain='')
Definition Route.php:172
static mergeRouteArray($routeArrays)
Definition Route.php:48
__construct($path="", $array=[])
Definition Route.php:38
String utilities.
Definition UString.php:15