Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
RouterAdminTrait.php
Go to the documentation of this file.
1<?php
2
4
6
17
18 abstract public static function slashPath($path): string;
19
26 public static function getRouteInfoByControllerAction($controller, $action) {
27 foreach ( self::$routes as $routePath => $routeDetails ) {
28 if (! isset ( $routeDetails ['controller'] )) {
29 $routeDetails = \current ( $routeDetails );
30 }
31 if(\is_string($routeDetails ['controller'])){
32 if ($controller === \str_replace('\\\\','\\',$routeDetails ['controller']) && $action === $routeDetails ['action']) {
33 $routeDetails ['path'] = $routePath;
34 return $routeDetails;
35 }
36 }
37 }
38 return false;
39 }
40
41 public static function getRoutesPathByController($controller): array {
42 $result = [ ];
43 foreach ( self::$routes as $routePath => $routeDetails ) {
44 if (! isset ( $routeDetails ['controller'] )) {
45 foreach ( $routeDetails as $method => $routeInfo ) {
46 if ($routeInfo ['controller'] === $controller) {
47 $result [] = [ 'method' => $method,'url' => self::getRoutePathInfos ( $controller, $routePath, $routeInfo ) ];
48 }
49 }
50 } else {
51 if ($routeDetails ['controller'] === $controller) {
52 $result [] = [ 'method' => '*','url' => self::getRoutePathInfos ( $controller, $routePath, $routeDetails ) ];
53 }
54 }
55 }
56 return $result;
57 }
58
59 public static function getRoutePathInfos($controller, $routePath, $routeInfo) {
60 $method = new \ReflectionMethod ( $controller, $routeInfo ['action'] );
61 $parameters = $method->getParameters ();
62 $routeParams = $routeInfo ['parameters'];
63 $pattern = "@\‍(.*?\‍)@";
64 $params = [ ];
65 foreach ( $routeParams as $param ) {
66 if ($param === '*') {
67 $params [] = $parameters [\count ( $params )]->getName ();
68 } else {
69 $index = ( int ) \filter_var ( $param, FILTER_SANITIZE_NUMBER_INT );
70 $params [] = $parameters [$index]->getName ();
71 }
72 }
73 $path = $routePath;
74 foreach ( $params as $param ) {
75 $path = \preg_replace ( $pattern, '{' . $param . '}', $path, 1 );
76 }
77 return $path;
78 }
79
85 public static function getRouteInfo($path) {
86 $path = self::slashPath ( $path );
87 foreach ( self::$routes as $routePath => $routeDetails ) {
88 if (\preg_match ( "@^{$routePath}\$@s", $path, $matches ) || \stripslashes ( $routePath ) == $path) {
89 if (! isset ( $routeDetails ['controller'] )) {
90 return \current ( $routeDetails );
91 } else
92 return $routeDetails;
93 }
94 }
95 return false;
96 }
97
98 public static function getAnnotations($controllerName, $actionName): array {
99 $result = [ ];
100 foreach ( self::$routes as $routePath => $routeDetails ) {
101 if (! isset ( $routeDetails ['controller'] )) {
102 $routeDetails = \current ( $routeDetails );
103 }
104 if ($routeDetails ['controller'] === $controllerName && $routeDetails ['action'] === $actionName)
105 $result [$routePath] = $routeDetails;
106 }
107 return $result;
108 }
109
110 public static function filterRoutes($path) {
111 $path = self::slashPath ( $path );
112 $result = [ ];
113 foreach ( self::$routes as $routePath => $routeDetails ) {
114 if (\preg_match ( "@^{$routePath}.*?$@s", $path, $matches )) {
115 $result [$routePath] = $routeDetails;
116 }
117 }
118 return $result;
119 }
120 public static function getRouteInfoByDefaultRouting(string $url){
121 $ns = Startup::getNS();
122 $url=\trim($url,'/');
123 $u = \explode("/", $url);
124 $controller = $ns . $u[0];
125 $action = $u[1]??'index';
126 return self::getRouteInfoByControllerAction($controller,$action);
127 }
128}
129
Starts the framework.
Definition Startup.php:19
static getRouteInfoByControllerAction($controller, $action)
static getRoutePathInfos($controller, $routePath, $routeInfo)
static getAnnotations($controllerName, $actionName)