Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
RouterModifierTrait.php
Go to the documentation of this file.
1<?php
2
4
7
17
29 public static function addRoute($path, $controller, $action = 'index', $methods = null, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0): void {
30 self::addRouteToRoutes ( self::$routes, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority );
31 }
32
33 public static function addRouteToRoutes(&$routesArray, $path, $controller, $action = 'index', $methods = null, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0, $callback = null): void {
34 if (\class_exists ( $controller )) {
35 $method = new \ReflectionMethod ( $controller, $action );
36 $path = ControllerParser::parseMethodPath ( $method, $path );
37 self::_addRoute ( $method, $routesArray, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority, $callback );
38 }
39 }
40
41 private static function _addRoute(\ReflectionMethod $method, &$routesArray, $path, $controller, $action = 'index', $methods = null, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0, $callback = null): void {
42 $result = [ ];
43 ControllerParser::parseRouteArray ( $result, $controller, [ 'path' => $path,'methods' => $methods,'name' => $name,'cache' => $cache,'duration' => $duration,'requirements' => $requirements,'priority' => $priority,'callback' => $callback ], $method, $action );
44 if ($priority <= 0) {
45 foreach ( $result as $k => $v ) {
46 $routesArray [$k] = $v;
47 }
48 } else {
49 $count = \count ( $routesArray );
50 $newArray = [ ];
51 foreach ( $routesArray as $k => $v ) {
52 if ($priority < $count --) {
53 $newArray [$k] = $v;
54 } else {
55 break;
56 }
57 }
58 $routesArray = \array_diff_key ( $routesArray, $newArray );
59 foreach ( $result as $k => $v ) {
60 $newArray [$k] = $v;
61 }
62 foreach ( $routesArray as $k => $v ) {
63 $newArray [$k] = $v;
64 }
65 $routesArray = $newArray;
66 }
67 }
68
69 public static function addCallableRoute($path, $callable, $methods = null, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0): void {
70 if ($callable instanceof \Closure) {
71 $reflectionFunction = new \ReflectionFunction ( $callable );
72 $path = ControllerParser::parseMethodPath ( $reflectionFunction, $path );
73 self::_addCallableRoute ( $reflectionFunction, self::$routes, $path, $callable, $methods, $name, $cache, $duration, $requirements, $priority );
74 }
75 }
76
77 public static function get($path, $callable, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0): void {
78 self::addCallableRoute ( $path, $callable, [ 'get' ], $name, $cache, $duration, $requirements, $priority );
79 }
80
81 public static function post($path, $callable, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0): void {
82 self::addCallableRoute ( $path, $callable, [ 'post' ], $name, $cache, $duration, $requirements, $priority );
83 }
84
85 public static function delete($path, $callable, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0): void {
86 self::addCallableRoute ( $path, $callable, [ 'delete' ], $name, $cache, $duration, $requirements, $priority );
87 }
88
89 public static function put($path, $callable, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0): void {
90 self::addCallableRoute ( $path, $callable, [ 'put' ], $name, $cache, $duration, $requirements, $priority );
91 }
92
93 public static function patch($path, $callable, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0): void {
94 self::addCallableRoute ( $path, $callable, [ 'patch' ], $name, $cache, $duration, $requirements, $priority );
95 }
96
97 public static function options($path, $callable, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0): void {
98 self::addCallableRoute ( $path, $callable, [ 'options' ], $name, $cache, $duration, $requirements, $priority );
99 }
100
101 private static function _addCallableRoute(\ReflectionFunction $reflectionFunction, &$routesArray, $path, $callable, $methods = null, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0): void {
102 $result = [ ];
103 CallableParser::parseRouteArray ( $result, $callable, [ 'path' => $path,'methods' => $methods,'name' => $name,'cache' => $cache,'duration' => $duration,'requirements' => $requirements,'priority' => $priority ], $reflectionFunction );
104 foreach ( $result as $k => $v ) {
105 $routesArray [$k] = $v;
106 }
107 }
108
109 public static function addRoutesToRoutes(&$routesArray, $paths, $controller, $action = 'index', $methods = null, $name = '', $cache = false, $duration = null, $requirements = [], $priority = 0): void {
110 if (\class_exists ( $controller )) {
111 $method = new \ReflectionMethod ( $controller, $action );
112 foreach ( $paths as $path ) {
113 self::_addRoute ( $method, $routesArray, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority );
114 }
115 }
116 }
117}
118
Ubiquity\cache\parser$CallableParser This class is part of Ubiquity.
Scans a controller to detect routes defined by annotations or attributes.
Ubiquity\controllers\traits$RouterModifierTrait This class is part of Ubiquity.
static _addRoute(\ReflectionMethod $method, &$routesArray, $path, $controller, $action='index', $methods=null, $name='', $cache=false, $duration=null, $requirements=[], $priority=0, $callback=null)
static patch($path, $callable, $name='', $cache=false, $duration=null, $requirements=[], $priority=0)
static addRouteToRoutes(&$routesArray, $path, $controller, $action='index', $methods=null, $name='', $cache=false, $duration=null, $requirements=[], $priority=0, $callback=null)
static options($path, $callable, $name='', $cache=false, $duration=null, $requirements=[], $priority=0)
static post($path, $callable, $name='', $cache=false, $duration=null, $requirements=[], $priority=0)
static addRoutesToRoutes(&$routesArray, $paths, $controller, $action='index', $methods=null, $name='', $cache=false, $duration=null, $requirements=[], $priority=0)
static addRoute($path, $controller, $action='index', $methods=null, $name='', $cache=false, $duration=null, $requirements=[], $priority=0)
static _addCallableRoute(\ReflectionFunction $reflectionFunction, &$routesArray, $path, $callable, $methods=null, $name='', $cache=false, $duration=null, $requirements=[], $priority=0)
static addCallableRoute($path, $callable, $methods=null, $name='', $cache=false, $duration=null, $requirements=[], $priority=0)
static put($path, $callable, $name='', $cache=false, $duration=null, $requirements=[], $priority=0)