@@ -48,7 +48,7 @@ |
||
| 48 | 48 | * Shutdown the application. |
| 49 | 49 | * |
| 50 | 50 | * @param \Syscodes\Components\Contracts\Console\Input\Input $input |
| 51 | - * @param int $status |
|
| 51 | + * @param int $status |
|
| 52 | 52 | * |
| 53 | 53 | * @return void |
| 54 | 54 | */ |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | * |
| 59 | 59 | * @return bool |
| 60 | 60 | */ |
| 61 | - public function hasParameterOption(string|array $values, bool $params = false): bool; |
|
| 61 | + public function hasParameterOption(string | array $values, bool $params = false): bool; |
|
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * Gets the value of a unprocessed option (not parsed). |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * |
| 70 | 70 | * @return mixed |
| 71 | 71 | */ |
| 72 | - public function getParameterOption(string|array $values, $default = false, bool $params = false): mixed; |
|
| 72 | + public function getParameterOption(string | array $values, $default = false, bool $params = false): mixed; |
|
| 73 | 73 | |
| 74 | 74 | /** |
| 75 | 75 | * Get the input interactive. |
@@ -49,28 +49,28 @@ |
||
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | - * Parse the given URI. |
|
| 53 | - * |
|
| 54 | - * @param string $uri |
|
| 55 | - * |
|
| 56 | - * @return static |
|
| 57 | - */ |
|
| 58 | - public static function parse(string $uri): static |
|
| 59 | - { |
|
| 60 | - preg_match_all('~\{([\w\:]+?)\??\}~', $uri, $matches); |
|
| 52 | + * Parse the given URI. |
|
| 53 | + * |
|
| 54 | + * @param string $uri |
|
| 55 | + * |
|
| 56 | + * @return static |
|
| 57 | + */ |
|
| 58 | + public static function parse(string $uri): static |
|
| 59 | + { |
|
| 60 | + preg_match_all('~\{([\w\:]+?)\??\}~', $uri, $matches); |
|
| 61 | 61 | |
| 62 | - foreach ($matches[0] as $match) { |
|
| 63 | - if ( ! Str::contains($match, ':')) { |
|
| 64 | - continue; |
|
| 65 | - } |
|
| 62 | + foreach ($matches[0] as $match) { |
|
| 63 | + if ( ! Str::contains($match, ':')) { |
|
| 64 | + continue; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $segments = explode(':', trim($match, '{}?')); |
|
| 67 | + $segments = explode(':', trim($match, '{}?')); |
|
| 68 | 68 | |
| 69 | - $uri = Str::contains($match, '?') |
|
| 69 | + $uri = Str::contains($match, '?') |
|
| 70 | 70 | ? str_replace($match, '{'.$segments[0].'?}', $uri) |
| 71 | 71 | : str_replace($match, '{'.$segments[0].'}', $uri); |
| 72 | - } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - return new static($uri); |
|
| 75 | - } |
|
| 74 | + return new static($uri); |
|
| 75 | + } |
|
| 76 | 76 | } |
| 77 | 77 | \ No newline at end of file |
@@ -339,7 +339,7 @@ |
||
| 339 | 339 | */ |
| 340 | 340 | protected function getNestedResourceUri(array $segments): string |
| 341 | 341 | { |
| 342 | - return implode('/', array_map(function ($segment) { |
|
| 342 | + return implode('/', array_map(function($segment) { |
|
| 343 | 343 | return $segment.'/{'.$this->getResourceWilcard($segment).'}'; |
| 344 | 344 | }, $segments)); |
| 345 | 345 | } |
@@ -108,7 +108,7 @@ |
||
| 108 | 108 | */ |
| 109 | 109 | protected function getInParameters($class, array $parameters): bool |
| 110 | 110 | { |
| 111 | - return ! is_null(Arr::first($parameters, function ($value) use ($class) { |
|
| 111 | + return ! is_null(Arr::first($parameters, function($value) use ($class) { |
|
| 112 | 112 | return $value instanceof $class; |
| 113 | 113 | })); |
| 114 | 114 | } |
@@ -27,139 +27,139 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | trait RouteMap |
| 29 | 29 | { |
| 30 | - /** |
|
| 31 | - * All of the verbs supported by the router. |
|
| 32 | - * |
|
| 33 | - * @var array $verbs |
|
| 34 | - */ |
|
| 35 | - public static $verbs = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']; |
|
| 30 | + /** |
|
| 31 | + * All of the verbs supported by the router. |
|
| 32 | + * |
|
| 33 | + * @var array $verbs |
|
| 34 | + */ |
|
| 35 | + public static $verbs = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Add a route to the underlying route collection. |
|
| 39 | - * |
|
| 40 | - * @param string $method |
|
| 41 | - * @param string $route |
|
| 42 | - * @param mixed $action |
|
| 43 | - * |
|
| 44 | - * @return \Syscodes\Components\Routing\Route |
|
| 45 | - */ |
|
| 46 | - abstract public function addRoute($method, $route, $action); |
|
| 37 | + /** |
|
| 38 | + * Add a route to the underlying route collection. |
|
| 39 | + * |
|
| 40 | + * @param string $method |
|
| 41 | + * @param string $route |
|
| 42 | + * @param mixed $action |
|
| 43 | + * |
|
| 44 | + * @return \Syscodes\Components\Routing\Route |
|
| 45 | + */ |
|
| 46 | + abstract public function addRoute($method, $route, $action); |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Add a route for all posible methods. |
|
| 50 | - * |
|
| 51 | - * @param string $route |
|
| 52 | - * @param \Closure|array|string|null $action |
|
| 53 | - * |
|
| 54 | - * @return \Syscodes\Components\Routing\Route |
|
| 55 | - */ |
|
| 56 | - public function any($route, $action = null) |
|
| 57 | - { |
|
| 58 | - return $this->addRoute(self::$verbs, $route, $action); |
|
| 59 | - } |
|
| 48 | + /** |
|
| 49 | + * Add a route for all posible methods. |
|
| 50 | + * |
|
| 51 | + * @param string $route |
|
| 52 | + * @param \Closure|array|string|null $action |
|
| 53 | + * |
|
| 54 | + * @return \Syscodes\Components\Routing\Route |
|
| 55 | + */ |
|
| 56 | + public function any($route, $action = null) |
|
| 57 | + { |
|
| 58 | + return $this->addRoute(self::$verbs, $route, $action); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Add a route with delete method. |
|
| 63 | - * |
|
| 64 | - * @param string $route |
|
| 65 | - * @param \Closure|array|string|null $action |
|
| 66 | - * |
|
| 67 | - * @return \Syscodes\Components\Routing\Route |
|
| 68 | - */ |
|
| 69 | - public function delete($route, $action = null) |
|
| 70 | - { |
|
| 71 | - return $this->addRoute('DELETE', $route, $action); |
|
| 72 | - } |
|
| 61 | + /** |
|
| 62 | + * Add a route with delete method. |
|
| 63 | + * |
|
| 64 | + * @param string $route |
|
| 65 | + * @param \Closure|array|string|null $action |
|
| 66 | + * |
|
| 67 | + * @return \Syscodes\Components\Routing\Route |
|
| 68 | + */ |
|
| 69 | + public function delete($route, $action = null) |
|
| 70 | + { |
|
| 71 | + return $this->addRoute('DELETE', $route, $action); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Add a route with get method. |
|
| 76 | - * |
|
| 77 | - * @param string $route |
|
| 78 | - * @param \Closure|array|string|null $action |
|
| 79 | - * |
|
| 80 | - * @return \Syscodes\Components\Routing\Route |
|
| 81 | - */ |
|
| 82 | - public function get($route, $action = null) |
|
| 83 | - { |
|
| 84 | - return $this->addRoute(['GET', 'HEAD'], $route, $action); |
|
| 85 | - } |
|
| 74 | + /** |
|
| 75 | + * Add a route with get method. |
|
| 76 | + * |
|
| 77 | + * @param string $route |
|
| 78 | + * @param \Closure|array|string|null $action |
|
| 79 | + * |
|
| 80 | + * @return \Syscodes\Components\Routing\Route |
|
| 81 | + */ |
|
| 82 | + public function get($route, $action = null) |
|
| 83 | + { |
|
| 84 | + return $this->addRoute(['GET', 'HEAD'], $route, $action); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Add a route with head method. |
|
| 89 | - * |
|
| 90 | - * @param string $route |
|
| 91 | - * @param \Closure|array|string|null $action |
|
| 92 | - * |
|
| 93 | - * @return \Syscodes\Components\Routing\Route |
|
| 94 | - */ |
|
| 95 | - public function head($route, $action = null) |
|
| 96 | - { |
|
| 97 | - return $this->addRoute('HEAD', $route, $action); |
|
| 98 | - } |
|
| 87 | + /** |
|
| 88 | + * Add a route with head method. |
|
| 89 | + * |
|
| 90 | + * @param string $route |
|
| 91 | + * @param \Closure|array|string|null $action |
|
| 92 | + * |
|
| 93 | + * @return \Syscodes\Components\Routing\Route |
|
| 94 | + */ |
|
| 95 | + public function head($route, $action = null) |
|
| 96 | + { |
|
| 97 | + return $this->addRoute('HEAD', $route, $action); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Register a new route with the given methods. |
|
| 102 | - * |
|
| 103 | - * @param array|string $methods |
|
| 104 | - * @param string $route |
|
| 105 | - * @param \Closure|array|string|null $action |
|
| 106 | - * |
|
| 107 | - * @return \Syscodes\Components\Routing\Route |
|
| 108 | - */ |
|
| 109 | - public function match($methods, $route, $action = null) |
|
| 110 | - { |
|
| 111 | - return $this->addRoute(array_map('strtoupper', (array) $methods), $route, $action); |
|
| 112 | - } |
|
| 100 | + /** |
|
| 101 | + * Register a new route with the given methods. |
|
| 102 | + * |
|
| 103 | + * @param array|string $methods |
|
| 104 | + * @param string $route |
|
| 105 | + * @param \Closure|array|string|null $action |
|
| 106 | + * |
|
| 107 | + * @return \Syscodes\Components\Routing\Route |
|
| 108 | + */ |
|
| 109 | + public function match($methods, $route, $action = null) |
|
| 110 | + { |
|
| 111 | + return $this->addRoute(array_map('strtoupper', (array) $methods), $route, $action); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Add a route with options method |
|
| 116 | - * |
|
| 117 | - * @param string $route |
|
| 118 | - * @param \Closure|array|string|null $action |
|
| 119 | - * |
|
| 120 | - * @return \Syscodes\Components\Routing\Route |
|
| 121 | - */ |
|
| 122 | - public function options($route, $action = null) |
|
| 123 | - { |
|
| 124 | - return $this->addRoute('OPTIONS', $route, $action); |
|
| 125 | - } |
|
| 114 | + /** |
|
| 115 | + * Add a route with options method |
|
| 116 | + * |
|
| 117 | + * @param string $route |
|
| 118 | + * @param \Closure|array|string|null $action |
|
| 119 | + * |
|
| 120 | + * @return \Syscodes\Components\Routing\Route |
|
| 121 | + */ |
|
| 122 | + public function options($route, $action = null) |
|
| 123 | + { |
|
| 124 | + return $this->addRoute('OPTIONS', $route, $action); |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Add a route with patch method. |
|
| 129 | - * |
|
| 130 | - * @param string $route |
|
| 131 | - * @param \Closure|array|string|null $action |
|
| 132 | - * |
|
| 133 | - * @return \Syscodes\Components\Routing\Route |
|
| 134 | - */ |
|
| 135 | - public function patch($route, $action = null) |
|
| 136 | - { |
|
| 137 | - return $this->addRoute('PATCH', $route, $action); |
|
| 138 | - } |
|
| 127 | + /** |
|
| 128 | + * Add a route with patch method. |
|
| 129 | + * |
|
| 130 | + * @param string $route |
|
| 131 | + * @param \Closure|array|string|null $action |
|
| 132 | + * |
|
| 133 | + * @return \Syscodes\Components\Routing\Route |
|
| 134 | + */ |
|
| 135 | + public function patch($route, $action = null) |
|
| 136 | + { |
|
| 137 | + return $this->addRoute('PATCH', $route, $action); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * Add a route with post method. |
|
| 142 | - * |
|
| 143 | - * @param string $route |
|
| 144 | - * @param \Closure|array|string|null $action |
|
| 145 | - * |
|
| 146 | - * @return \Syscodes\Components\Routing\Route |
|
| 147 | - */ |
|
| 148 | - public function post($route, $action = null) |
|
| 149 | - { |
|
| 150 | - return $this->addRoute('POST', $route, $action); |
|
| 151 | - } |
|
| 140 | + /** |
|
| 141 | + * Add a route with post method. |
|
| 142 | + * |
|
| 143 | + * @param string $route |
|
| 144 | + * @param \Closure|array|string|null $action |
|
| 145 | + * |
|
| 146 | + * @return \Syscodes\Components\Routing\Route |
|
| 147 | + */ |
|
| 148 | + public function post($route, $action = null) |
|
| 149 | + { |
|
| 150 | + return $this->addRoute('POST', $route, $action); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Add a route with put method. |
|
| 155 | - * |
|
| 156 | - * @param string $route |
|
| 157 | - * @param \Closure|array|string|null $action |
|
| 158 | - * |
|
| 159 | - * @return \Syscodes\Components\Routing\Route |
|
| 160 | - */ |
|
| 161 | - public function put($route, $action = null) |
|
| 162 | - { |
|
| 163 | - return $this->addRoute('PUT', $route, $action); |
|
| 164 | - } |
|
| 153 | + /** |
|
| 154 | + * Add a route with put method. |
|
| 155 | + * |
|
| 156 | + * @param string $route |
|
| 157 | + * @param \Closure|array|string|null $action |
|
| 158 | + * |
|
| 159 | + * @return \Syscodes\Components\Routing\Route |
|
| 160 | + */ |
|
| 161 | + public function put($route, $action = null) |
|
| 162 | + { |
|
| 163 | + return $this->addRoute('PUT', $route, $action); |
|
| 164 | + } |
|
| 165 | 165 | } |
| 166 | 166 | \ No newline at end of file |
@@ -854,7 +854,7 @@ |
||
| 854 | 854 | * |
| 855 | 855 | * @return array|string |
| 856 | 856 | */ |
| 857 | - public function getMethod(): array|string |
|
| 857 | + public function getMethod(): array | string |
|
| 858 | 858 | { |
| 859 | 859 | return $this->method; |
| 860 | 860 | } |
@@ -43,857 +43,857 @@ |
||
| 43 | 43 | */ |
| 44 | 44 | class Route |
| 45 | 45 | { |
| 46 | - use Concerns\RouteCondition, |
|
| 47 | - Concerns\RouteDependencyResolver; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * The validators used by the routes. |
|
| 51 | - * |
|
| 52 | - * @var array $validators |
|
| 53 | - */ |
|
| 54 | - public static $validators; |
|
| 46 | + use Concerns\RouteCondition, |
|
| 47 | + Concerns\RouteDependencyResolver; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * The validators used by the routes. |
|
| 51 | + * |
|
| 52 | + * @var array $validators |
|
| 53 | + */ |
|
| 54 | + public static $validators; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Action that the route will use when called. |
|
| 58 | - * |
|
| 59 | - * @var \Closure|string|array $action |
|
| 60 | - */ |
|
| 61 | - public $action; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * The compiled version of the route. |
|
| 65 | - * |
|
| 66 | - * @var \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute|string $compiled |
|
| 67 | - */ |
|
| 68 | - public $compiled; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * The computed gathered middleware. |
|
| 72 | - * |
|
| 73 | - * @var array|null $computedMiddleware |
|
| 74 | - */ |
|
| 75 | - public $computedMiddleware; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * The container instance used by the route. |
|
| 79 | - * |
|
| 80 | - * @var \Syscodes\Components\Container\Container $container |
|
| 81 | - */ |
|
| 82 | - protected $container; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * The controller instance. |
|
| 86 | - * |
|
| 87 | - * @var string $controller |
|
| 88 | - */ |
|
| 89 | - public $controller; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * The default values for the route. |
|
| 93 | - * |
|
| 94 | - * @var array $defaults |
|
| 95 | - */ |
|
| 96 | - public $defaults = []; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Indicates whether the route is a fallback route. |
|
| 100 | - * |
|
| 101 | - * @var bool $fallback |
|
| 102 | - */ |
|
| 103 | - protected $fallback = false; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Variable of HTTP method. |
|
| 107 | - * |
|
| 108 | - * @var array|string $method |
|
| 109 | - */ |
|
| 110 | - public $method; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * The array of matched parameters. |
|
| 114 | - * |
|
| 115 | - * @var array $parameters |
|
| 116 | - */ |
|
| 117 | - public $parameters = []; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * The parameter names for the route. |
|
| 121 | - * |
|
| 122 | - * @var string|null $parameterNames |
|
| 123 | - */ |
|
| 124 | - public $parameterNames; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * The URI pattern the route responds to. |
|
| 128 | - * |
|
| 129 | - * @var string $uri |
|
| 130 | - */ |
|
| 131 | - public $uri; |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Contains the arguments of the current route. |
|
| 135 | - * |
|
| 136 | - * @var array $where |
|
| 137 | - */ |
|
| 138 | - public $wheres = []; |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Constructor. Initialize route. |
|
| 142 | - * |
|
| 143 | - * @param array|string $method |
|
| 144 | - * @param string $uri |
|
| 145 | - * @param \Closure|array $action |
|
| 146 | - * |
|
| 147 | - * @return void |
|
| 148 | - */ |
|
| 149 | - public function __construct($method, $uri, $action) |
|
| 150 | - { |
|
| 151 | - $this->uri = $uri; |
|
| 152 | - |
|
| 153 | - // Set the method |
|
| 154 | - $this->method = $this->parseMethod($method); |
|
| 155 | - |
|
| 156 | - // Set the action |
|
| 157 | - $this->action = Arr::except($this->parseAction($action), ['prefix']); |
|
| 56 | + /** |
|
| 57 | + * Action that the route will use when called. |
|
| 58 | + * |
|
| 59 | + * @var \Closure|string|array $action |
|
| 60 | + */ |
|
| 61 | + public $action; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * The compiled version of the route. |
|
| 65 | + * |
|
| 66 | + * @var \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute|string $compiled |
|
| 67 | + */ |
|
| 68 | + public $compiled; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * The computed gathered middleware. |
|
| 72 | + * |
|
| 73 | + * @var array|null $computedMiddleware |
|
| 74 | + */ |
|
| 75 | + public $computedMiddleware; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * The container instance used by the route. |
|
| 79 | + * |
|
| 80 | + * @var \Syscodes\Components\Container\Container $container |
|
| 81 | + */ |
|
| 82 | + protected $container; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * The controller instance. |
|
| 86 | + * |
|
| 87 | + * @var string $controller |
|
| 88 | + */ |
|
| 89 | + public $controller; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * The default values for the route. |
|
| 93 | + * |
|
| 94 | + * @var array $defaults |
|
| 95 | + */ |
|
| 96 | + public $defaults = []; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Indicates whether the route is a fallback route. |
|
| 100 | + * |
|
| 101 | + * @var bool $fallback |
|
| 102 | + */ |
|
| 103 | + protected $fallback = false; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Variable of HTTP method. |
|
| 107 | + * |
|
| 108 | + * @var array|string $method |
|
| 109 | + */ |
|
| 110 | + public $method; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * The array of matched parameters. |
|
| 114 | + * |
|
| 115 | + * @var array $parameters |
|
| 116 | + */ |
|
| 117 | + public $parameters = []; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * The parameter names for the route. |
|
| 121 | + * |
|
| 122 | + * @var string|null $parameterNames |
|
| 123 | + */ |
|
| 124 | + public $parameterNames; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * The URI pattern the route responds to. |
|
| 128 | + * |
|
| 129 | + * @var string $uri |
|
| 130 | + */ |
|
| 131 | + public $uri; |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Contains the arguments of the current route. |
|
| 135 | + * |
|
| 136 | + * @var array $where |
|
| 137 | + */ |
|
| 138 | + public $wheres = []; |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Constructor. Initialize route. |
|
| 142 | + * |
|
| 143 | + * @param array|string $method |
|
| 144 | + * @param string $uri |
|
| 145 | + * @param \Closure|array $action |
|
| 146 | + * |
|
| 147 | + * @return void |
|
| 148 | + */ |
|
| 149 | + public function __construct($method, $uri, $action) |
|
| 150 | + { |
|
| 151 | + $this->uri = $uri; |
|
| 152 | + |
|
| 153 | + // Set the method |
|
| 154 | + $this->method = $this->parseMethod($method); |
|
| 155 | + |
|
| 156 | + // Set the action |
|
| 157 | + $this->action = Arr::except($this->parseAction($action), ['prefix']); |
|
| 158 | 158 | |
| 159 | - if (in_array('GET', $this->method) && ! in_array('HEAD', $this->method)) { |
|
| 160 | - $this->method[] = 'HEAD'; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $this->prefix(is_array($action) ? Arr::get($action, 'prefix') : ''); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Get the controller instance for the route. |
|
| 168 | - * |
|
| 169 | - * @return mixed |
|
| 170 | - */ |
|
| 171 | - public function getController() |
|
| 172 | - { |
|
| 173 | - if ( ! $this->controller) { |
|
| 174 | - $class = $this->parseControllerCallback()[0]; |
|
| 159 | + if (in_array('GET', $this->method) && ! in_array('HEAD', $this->method)) { |
|
| 160 | + $this->method[] = 'HEAD'; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $this->prefix(is_array($action) ? Arr::get($action, 'prefix') : ''); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Get the controller instance for the route. |
|
| 168 | + * |
|
| 169 | + * @return mixed |
|
| 170 | + */ |
|
| 171 | + public function getController() |
|
| 172 | + { |
|
| 173 | + if ( ! $this->controller) { |
|
| 174 | + $class = $this->parseControllerCallback()[0]; |
|
| 175 | 175 | |
| 176 | - $this->controller = $this->container->make(ltrim($class, '\\')); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $this->controller; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Get the controller method used for the route. |
|
| 184 | - * |
|
| 185 | - * @return string |
|
| 186 | - */ |
|
| 187 | - public function getControllerMethod(): string |
|
| 188 | - { |
|
| 189 | - return $this->parseControllerCallback()[1]; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Get or set the domain for the route. |
|
| 194 | - * |
|
| 195 | - * @param string|null $domain |
|
| 196 | - * |
|
| 197 | - * @return mixed |
|
| 198 | - */ |
|
| 199 | - public function domain($domain = null) |
|
| 200 | - { |
|
| 201 | - if (is_null($domain)) { |
|
| 202 | - return $this->getDomain(); |
|
| 203 | - } |
|
| 176 | + $this->controller = $this->container->make(ltrim($class, '\\')); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return $this->controller; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Get the controller method used for the route. |
|
| 184 | + * |
|
| 185 | + * @return string |
|
| 186 | + */ |
|
| 187 | + public function getControllerMethod(): string |
|
| 188 | + { |
|
| 189 | + return $this->parseControllerCallback()[1]; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Get or set the domain for the route. |
|
| 194 | + * |
|
| 195 | + * @param string|null $domain |
|
| 196 | + * |
|
| 197 | + * @return mixed |
|
| 198 | + */ |
|
| 199 | + public function domain($domain = null) |
|
| 200 | + { |
|
| 201 | + if (is_null($domain)) { |
|
| 202 | + return $this->getDomain(); |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - $parsed = RouteUri::parse($domain); |
|
| 206 | - |
|
| 207 | - $this->action['domain'] = $parsed->uri; |
|
| 208 | - |
|
| 209 | - return $this; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Get the domain defined for the route. |
|
| 214 | - * |
|
| 215 | - * @return string|null |
|
| 216 | - */ |
|
| 217 | - public function getDomain() |
|
| 218 | - { |
|
| 219 | - return isset($this->action['domain']) |
|
| 220 | - ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
| 221 | - : null; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Parse the controller. |
|
| 226 | - * |
|
| 227 | - * @return array |
|
| 228 | - */ |
|
| 229 | - public function parseControllerCallback(): array |
|
| 230 | - { |
|
| 231 | - return Str::parseCallback($this->action['uses']); |
|
| 232 | - } |
|
| 205 | + $parsed = RouteUri::parse($domain); |
|
| 206 | + |
|
| 207 | + $this->action['domain'] = $parsed->uri; |
|
| 208 | + |
|
| 209 | + return $this; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Get the domain defined for the route. |
|
| 214 | + * |
|
| 215 | + * @return string|null |
|
| 216 | + */ |
|
| 217 | + public function getDomain() |
|
| 218 | + { |
|
| 219 | + return isset($this->action['domain']) |
|
| 220 | + ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
| 221 | + : null; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Parse the controller. |
|
| 226 | + * |
|
| 227 | + * @return array |
|
| 228 | + */ |
|
| 229 | + public function parseControllerCallback(): array |
|
| 230 | + { |
|
| 231 | + return Str::parseCallback($this->action['uses']); |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * Checks whether the route's action is a controller. |
|
| 236 | - * |
|
| 237 | - * @return bool |
|
| 238 | - */ |
|
| 239 | - public function isControllerAction(): bool |
|
| 240 | - { |
|
| 241 | - return is_string($this->action['uses']); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Get the dispatcher for the route's controller. |
|
| 246 | - * |
|
| 247 | - * @return \Syscodes\Components\Routing\ControllerDispatcher |
|
| 248 | - */ |
|
| 249 | - private function controllerDispatcher(): ControllerDispatcher |
|
| 250 | - { |
|
| 251 | - return new ControllerDispatcher($this->container); |
|
| 252 | - } |
|
| 234 | + /** |
|
| 235 | + * Checks whether the route's action is a controller. |
|
| 236 | + * |
|
| 237 | + * @return bool |
|
| 238 | + */ |
|
| 239 | + public function isControllerAction(): bool |
|
| 240 | + { |
|
| 241 | + return is_string($this->action['uses']); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Get the dispatcher for the route's controller. |
|
| 246 | + * |
|
| 247 | + * @return \Syscodes\Components\Routing\ControllerDispatcher |
|
| 248 | + */ |
|
| 249 | + private function controllerDispatcher(): ControllerDispatcher |
|
| 250 | + { |
|
| 251 | + return new ControllerDispatcher($this->container); |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * Run the route action and return the response. |
|
| 256 | - * |
|
| 257 | - * @return mixed |
|
| 258 | - */ |
|
| 259 | - public function runResolver() |
|
| 260 | - { |
|
| 261 | - $this->container = $this->container ?: new Container; |
|
| 262 | - |
|
| 263 | - try { |
|
| 264 | - if ($this->isControllerAction()) { |
|
| 265 | - return $this->runResolverController(); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - return $this->runResolverCallable(); |
|
| 269 | - } catch (HttpResponseException $e) { |
|
| 270 | - return $e->getResponse(); |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Run the route action and return the response. |
|
| 276 | - * |
|
| 277 | - * @return mixed |
|
| 278 | - */ |
|
| 279 | - protected function runResolverCallable() |
|
| 280 | - { |
|
| 281 | - $callable = $this->action['uses']; |
|
| 282 | - |
|
| 283 | - return $callable(...array_values($this->resolveMethodDependencies( |
|
| 284 | - $this->parametersWithouNulls(), new ReflectionFunction($callable) |
|
| 285 | - ))); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Run the route action and return the response. |
|
| 290 | - * |
|
| 291 | - * @return mixed |
|
| 292 | - */ |
|
| 293 | - protected function runResolverController() |
|
| 294 | - { |
|
| 295 | - return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Determine if the route matches a given request. |
|
| 300 | - * |
|
| 301 | - * @param \Syscodes\Components\Http\Request $request |
|
| 302 | - * @param bool $method |
|
| 303 | - * |
|
| 304 | - * @return bool |
|
| 305 | - */ |
|
| 306 | - public function matches(Request $request, bool $method = true): bool |
|
| 307 | - { |
|
| 308 | - $this->compileRoute(); |
|
| 309 | - |
|
| 310 | - foreach (self::getValidators() as $validator) { |
|
| 311 | - if ($method && $validator instanceof MethodValidator) { |
|
| 312 | - continue; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - if ( ! $validator->matches($this, $request)) { |
|
| 316 | - return false; |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - return true; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Get the route validators for the instance. |
|
| 325 | - * |
|
| 326 | - * @return array |
|
| 327 | - */ |
|
| 328 | - public function getValidators(): array |
|
| 329 | - { |
|
| 330 | - if (isset(static::$validators)) { |
|
| 331 | - return static::$validators; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - return static::$validators = [ |
|
| 335 | - new HostValidator, new MethodValidator, |
|
| 336 | - new SchemeValidator, new UriValidator |
|
| 337 | - ]; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Parse the route action. |
|
| 342 | - * |
|
| 343 | - * @param \callable|array|null $action |
|
| 344 | - * |
|
| 345 | - * @return array |
|
| 346 | - * |
|
| 347 | - * @throws \InvalidArgumentException |
|
| 348 | - */ |
|
| 349 | - protected function parseAction($action): array |
|
| 350 | - { |
|
| 351 | - if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
| 352 | - throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - return RouteAction::parse($this->uri, $action); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Set the method of the current route. |
|
| 360 | - * |
|
| 361 | - * @param array $method |
|
| 362 | - * |
|
| 363 | - * @return array |
|
| 364 | - * |
|
| 365 | - * @throws \InvalidArgumentException |
|
| 366 | - */ |
|
| 367 | - public function parseMethod($method): array |
|
| 368 | - { |
|
| 369 | - if ($method === null || empty($method)) { |
|
| 370 | - throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - foreach ((array) $method as $httpMethod) { |
|
| 374 | - if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
| 375 | - throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - return array_map('strtoupper', (array) $method); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Set the URI that the route responds to. |
|
| 384 | - * |
|
| 385 | - * @param string|null $uri |
|
| 386 | - * |
|
| 387 | - * @return static |
|
| 388 | - * |
|
| 389 | - * @throws \InvalidArgumentException |
|
| 390 | - */ |
|
| 391 | - public function setUri($uri): static |
|
| 392 | - { |
|
| 393 | - if ($uri === null) { |
|
| 394 | - throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - $this->uri = $this->parseUri($uri); |
|
| 398 | - |
|
| 399 | - return $this; |
|
| 400 | - } |
|
| 254 | + /** |
|
| 255 | + * Run the route action and return the response. |
|
| 256 | + * |
|
| 257 | + * @return mixed |
|
| 258 | + */ |
|
| 259 | + public function runResolver() |
|
| 260 | + { |
|
| 261 | + $this->container = $this->container ?: new Container; |
|
| 262 | + |
|
| 263 | + try { |
|
| 264 | + if ($this->isControllerAction()) { |
|
| 265 | + return $this->runResolverController(); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + return $this->runResolverCallable(); |
|
| 269 | + } catch (HttpResponseException $e) { |
|
| 270 | + return $e->getResponse(); |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Run the route action and return the response. |
|
| 276 | + * |
|
| 277 | + * @return mixed |
|
| 278 | + */ |
|
| 279 | + protected function runResolverCallable() |
|
| 280 | + { |
|
| 281 | + $callable = $this->action['uses']; |
|
| 282 | + |
|
| 283 | + return $callable(...array_values($this->resolveMethodDependencies( |
|
| 284 | + $this->parametersWithouNulls(), new ReflectionFunction($callable) |
|
| 285 | + ))); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Run the route action and return the response. |
|
| 290 | + * |
|
| 291 | + * @return mixed |
|
| 292 | + */ |
|
| 293 | + protected function runResolverController() |
|
| 294 | + { |
|
| 295 | + return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Determine if the route matches a given request. |
|
| 300 | + * |
|
| 301 | + * @param \Syscodes\Components\Http\Request $request |
|
| 302 | + * @param bool $method |
|
| 303 | + * |
|
| 304 | + * @return bool |
|
| 305 | + */ |
|
| 306 | + public function matches(Request $request, bool $method = true): bool |
|
| 307 | + { |
|
| 308 | + $this->compileRoute(); |
|
| 309 | + |
|
| 310 | + foreach (self::getValidators() as $validator) { |
|
| 311 | + if ($method && $validator instanceof MethodValidator) { |
|
| 312 | + continue; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + if ( ! $validator->matches($this, $request)) { |
|
| 316 | + return false; |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + return true; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Get the route validators for the instance. |
|
| 325 | + * |
|
| 326 | + * @return array |
|
| 327 | + */ |
|
| 328 | + public function getValidators(): array |
|
| 329 | + { |
|
| 330 | + if (isset(static::$validators)) { |
|
| 331 | + return static::$validators; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + return static::$validators = [ |
|
| 335 | + new HostValidator, new MethodValidator, |
|
| 336 | + new SchemeValidator, new UriValidator |
|
| 337 | + ]; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Parse the route action. |
|
| 342 | + * |
|
| 343 | + * @param \callable|array|null $action |
|
| 344 | + * |
|
| 345 | + * @return array |
|
| 346 | + * |
|
| 347 | + * @throws \InvalidArgumentException |
|
| 348 | + */ |
|
| 349 | + protected function parseAction($action): array |
|
| 350 | + { |
|
| 351 | + if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
| 352 | + throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + return RouteAction::parse($this->uri, $action); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Set the method of the current route. |
|
| 360 | + * |
|
| 361 | + * @param array $method |
|
| 362 | + * |
|
| 363 | + * @return array |
|
| 364 | + * |
|
| 365 | + * @throws \InvalidArgumentException |
|
| 366 | + */ |
|
| 367 | + public function parseMethod($method): array |
|
| 368 | + { |
|
| 369 | + if ($method === null || empty($method)) { |
|
| 370 | + throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + foreach ((array) $method as $httpMethod) { |
|
| 374 | + if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
| 375 | + throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + return array_map('strtoupper', (array) $method); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Set the URI that the route responds to. |
|
| 384 | + * |
|
| 385 | + * @param string|null $uri |
|
| 386 | + * |
|
| 387 | + * @return static |
|
| 388 | + * |
|
| 389 | + * @throws \InvalidArgumentException |
|
| 390 | + */ |
|
| 391 | + public function setUri($uri): static |
|
| 392 | + { |
|
| 393 | + if ($uri === null) { |
|
| 394 | + throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + $this->uri = $this->parseUri($uri); |
|
| 398 | + |
|
| 399 | + return $this; |
|
| 400 | + } |
|
| 401 | 401 | |
| 402 | - /** |
|
| 403 | - * Parse the route URI and normalize. |
|
| 404 | - * |
|
| 405 | - * @param string $uri |
|
| 406 | - * |
|
| 407 | - * @return string |
|
| 408 | - */ |
|
| 409 | - protected function parseUri($uri): string |
|
| 410 | - { |
|
| 411 | - return take(RouteUri::parse($uri), fn ($uri) => $uri)->uri; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Add a prefix to the route URI. |
|
| 416 | - * |
|
| 417 | - * @param string $prefix |
|
| 418 | - * |
|
| 419 | - * @return static |
|
| 420 | - */ |
|
| 421 | - public function prefix($prefix): static |
|
| 422 | - { |
|
| 423 | - if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
| 424 | - $this->action['prefix'] = $newPrefix; |
|
| 425 | - } |
|
| 402 | + /** |
|
| 403 | + * Parse the route URI and normalize. |
|
| 404 | + * |
|
| 405 | + * @param string $uri |
|
| 406 | + * |
|
| 407 | + * @return string |
|
| 408 | + */ |
|
| 409 | + protected function parseUri($uri): string |
|
| 410 | + { |
|
| 411 | + return take(RouteUri::parse($uri), fn ($uri) => $uri)->uri; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Add a prefix to the route URI. |
|
| 416 | + * |
|
| 417 | + * @param string $prefix |
|
| 418 | + * |
|
| 419 | + * @return static |
|
| 420 | + */ |
|
| 421 | + public function prefix($prefix): static |
|
| 422 | + { |
|
| 423 | + if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
| 424 | + $this->action['prefix'] = $newPrefix; |
|
| 425 | + } |
|
| 426 | 426 | |
| 427 | - $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
| 427 | + $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
| 428 | 428 | |
| 429 | - return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Set the action array for the route. |
|
| 434 | - * |
|
| 435 | - * @param array $action |
|
| 436 | - * |
|
| 437 | - * @return mixed |
|
| 438 | - */ |
|
| 439 | - public function setAction(array $action) |
|
| 440 | - { |
|
| 441 | - $this->action = $action; |
|
| 442 | - |
|
| 443 | - if (isset($this->action['domain'])) { |
|
| 444 | - $this->domain($this->action['domain']); |
|
| 445 | - } |
|
| 429 | + return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Set the action array for the route. |
|
| 434 | + * |
|
| 435 | + * @param array $action |
|
| 436 | + * |
|
| 437 | + * @return mixed |
|
| 438 | + */ |
|
| 439 | + public function setAction(array $action) |
|
| 440 | + { |
|
| 441 | + $this->action = $action; |
|
| 442 | + |
|
| 443 | + if (isset($this->action['domain'])) { |
|
| 444 | + $this->domain($this->action['domain']); |
|
| 445 | + } |
|
| 446 | 446 | |
| 447 | - return $this; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * Set the name. |
|
| 452 | - * |
|
| 453 | - * @param string $name |
|
| 454 | - * |
|
| 455 | - * @return static |
|
| 456 | - */ |
|
| 457 | - public function name($name): static |
|
| 458 | - { |
|
| 459 | - $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
| 460 | - |
|
| 461 | - return $this; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Determine whether the route's name matches the given patterns. |
|
| 466 | - * |
|
| 467 | - * @param mixed ...$patterns |
|
| 468 | - * |
|
| 469 | - * @return bool |
|
| 470 | - */ |
|
| 471 | - public function named(...$patterns): bool |
|
| 472 | - { |
|
| 473 | - if (is_null($routeName = $this->getName())) { |
|
| 474 | - return false; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - foreach ($patterns as $pattern) { |
|
| 478 | - if (Str::is($pattern, $routeName)) { |
|
| 479 | - return true; |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - return false; |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * Set a default value for the route. |
|
| 488 | - * |
|
| 489 | - * @param string $key |
|
| 490 | - * @param mixed $value |
|
| 491 | - * |
|
| 492 | - * @return static |
|
| 493 | - */ |
|
| 494 | - public function defaults($key, $value): static |
|
| 495 | - { |
|
| 496 | - $this->defaults[$key] = $value; |
|
| 497 | - |
|
| 498 | - return $this; |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - /** |
|
| 502 | - * Set a default values for the route. |
|
| 503 | - * |
|
| 504 | - * @param array $defaults |
|
| 505 | - * |
|
| 506 | - * @return static |
|
| 507 | - */ |
|
| 508 | - public function setDefaults(array $defaults): static |
|
| 509 | - { |
|
| 510 | - $this->defaults = $defaults; |
|
| 511 | - |
|
| 512 | - return $this; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * Set the flag of fallback mode on the route. |
|
| 517 | - * |
|
| 518 | - * @return static |
|
| 519 | - */ |
|
| 520 | - public function fallback(): static |
|
| 521 | - { |
|
| 522 | - $this->fallback = true; |
|
| 523 | - |
|
| 524 | - return $this; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * Set the facllback value. |
|
| 529 | - * |
|
| 530 | - * @param bool $fallback |
|
| 531 | - * |
|
| 532 | - * @return static |
|
| 533 | - */ |
|
| 534 | - public function setFallback(bool $fallback): static |
|
| 535 | - { |
|
| 536 | - $this->fallback = $fallback; |
|
| 537 | - |
|
| 538 | - return $this; |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - /** |
|
| 542 | - * Set the where. |
|
| 543 | - * |
|
| 544 | - * @param array|string $name |
|
| 545 | - * @param string|null $expression |
|
| 546 | - * |
|
| 547 | - * @return static |
|
| 548 | - */ |
|
| 549 | - public function where($name, string $expression = null): static |
|
| 550 | - { |
|
| 551 | - $wheres = is_array($name) ? $name : [$name => $expression]; |
|
| 447 | + return $this; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * Set the name. |
|
| 452 | + * |
|
| 453 | + * @param string $name |
|
| 454 | + * |
|
| 455 | + * @return static |
|
| 456 | + */ |
|
| 457 | + public function name($name): static |
|
| 458 | + { |
|
| 459 | + $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
| 460 | + |
|
| 461 | + return $this; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Determine whether the route's name matches the given patterns. |
|
| 466 | + * |
|
| 467 | + * @param mixed ...$patterns |
|
| 468 | + * |
|
| 469 | + * @return bool |
|
| 470 | + */ |
|
| 471 | + public function named(...$patterns): bool |
|
| 472 | + { |
|
| 473 | + if (is_null($routeName = $this->getName())) { |
|
| 474 | + return false; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + foreach ($patterns as $pattern) { |
|
| 478 | + if (Str::is($pattern, $routeName)) { |
|
| 479 | + return true; |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + return false; |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * Set a default value for the route. |
|
| 488 | + * |
|
| 489 | + * @param string $key |
|
| 490 | + * @param mixed $value |
|
| 491 | + * |
|
| 492 | + * @return static |
|
| 493 | + */ |
|
| 494 | + public function defaults($key, $value): static |
|
| 495 | + { |
|
| 496 | + $this->defaults[$key] = $value; |
|
| 497 | + |
|
| 498 | + return $this; |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + /** |
|
| 502 | + * Set a default values for the route. |
|
| 503 | + * |
|
| 504 | + * @param array $defaults |
|
| 505 | + * |
|
| 506 | + * @return static |
|
| 507 | + */ |
|
| 508 | + public function setDefaults(array $defaults): static |
|
| 509 | + { |
|
| 510 | + $this->defaults = $defaults; |
|
| 511 | + |
|
| 512 | + return $this; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * Set the flag of fallback mode on the route. |
|
| 517 | + * |
|
| 518 | + * @return static |
|
| 519 | + */ |
|
| 520 | + public function fallback(): static |
|
| 521 | + { |
|
| 522 | + $this->fallback = true; |
|
| 523 | + |
|
| 524 | + return $this; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * Set the facllback value. |
|
| 529 | + * |
|
| 530 | + * @param bool $fallback |
|
| 531 | + * |
|
| 532 | + * @return static |
|
| 533 | + */ |
|
| 534 | + public function setFallback(bool $fallback): static |
|
| 535 | + { |
|
| 536 | + $this->fallback = $fallback; |
|
| 537 | + |
|
| 538 | + return $this; |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + /** |
|
| 542 | + * Set the where. |
|
| 543 | + * |
|
| 544 | + * @param array|string $name |
|
| 545 | + * @param string|null $expression |
|
| 546 | + * |
|
| 547 | + * @return static |
|
| 548 | + */ |
|
| 549 | + public function where($name, string $expression = null): static |
|
| 550 | + { |
|
| 551 | + $wheres = is_array($name) ? $name : [$name => $expression]; |
|
| 552 | 552 | |
| 553 | - foreach ($wheres as $name => $expression) { |
|
| 554 | - $this->wheres[$name] = $expression; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - return $this; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * Set the where when have a variable assign. |
|
| 562 | - * |
|
| 563 | - * @param string $key |
|
| 564 | - * |
|
| 565 | - * @return string|null |
|
| 566 | - */ |
|
| 567 | - public function setPattern(string $key): ?string |
|
| 568 | - { |
|
| 569 | - return $this->wheres[$key] ?? null; |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * Bind the route to a given request for execution. |
|
| 574 | - * |
|
| 575 | - * @param \Syscodes\Components\Http\Request $request |
|
| 576 | - * |
|
| 577 | - * @return static |
|
| 578 | - */ |
|
| 579 | - public function bind(Request $request): static |
|
| 580 | - { |
|
| 581 | - $this->compileRoute(); |
|
| 553 | + foreach ($wheres as $name => $expression) { |
|
| 554 | + $this->wheres[$name] = $expression; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + return $this; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * Set the where when have a variable assign. |
|
| 562 | + * |
|
| 563 | + * @param string $key |
|
| 564 | + * |
|
| 565 | + * @return string|null |
|
| 566 | + */ |
|
| 567 | + public function setPattern(string $key): ?string |
|
| 568 | + { |
|
| 569 | + return $this->wheres[$key] ?? null; |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * Bind the route to a given request for execution. |
|
| 574 | + * |
|
| 575 | + * @param \Syscodes\Components\Http\Request $request |
|
| 576 | + * |
|
| 577 | + * @return static |
|
| 578 | + */ |
|
| 579 | + public function bind(Request $request): static |
|
| 580 | + { |
|
| 581 | + $this->compileRoute(); |
|
| 582 | 582 | |
| 583 | - $this->parameters = (new RouteParameter($this))->parameters($request); |
|
| 584 | - |
|
| 585 | - return $this; |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - /** |
|
| 589 | - * Compile into a Route Compile instance. |
|
| 590 | - * |
|
| 591 | - * @return \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler |
|
| 592 | - */ |
|
| 593 | - protected function compileRoute() |
|
| 594 | - { |
|
| 595 | - if ( ! $this->compiled) { |
|
| 596 | - $this->compiled = $this->toBundleRoute()->compile(); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - return $this->compiled; |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * Get all of the parameter names for the route. |
|
| 604 | - * |
|
| 605 | - * @return array |
|
| 606 | - */ |
|
| 607 | - public function parameterNames(): array |
|
| 608 | - { |
|
| 609 | - if (isset($this->parameterNames)) { |
|
| 610 | - return $this->parameterNames; |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - return $this->parameterNames = $this->compileParameterNames(); |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * Get the parameter names for the route. |
|
| 618 | - * |
|
| 619 | - * @return array |
|
| 620 | - */ |
|
| 621 | - protected function compileParameterNames(): array |
|
| 622 | - { |
|
| 623 | - preg_match_all('~\{(.*?)\}~', $this->getDomain().$this->getUri(), $matches); |
|
| 624 | - |
|
| 625 | - return array_map(fn ($match) => trim($match, '?'), $matches[1]); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * Get a given parameter from the route. |
|
| 630 | - * |
|
| 631 | - * @param string $name |
|
| 632 | - * @param mixed $default |
|
| 633 | - * |
|
| 634 | - * @return mixed |
|
| 635 | - */ |
|
| 636 | - public function parameter($name, $default = null) |
|
| 637 | - { |
|
| 638 | - return Arr::get($this->parameters(), $name, $default); |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - /** |
|
| 642 | - * Set a parameter to the given value. |
|
| 643 | - * |
|
| 644 | - * @param string $name |
|
| 645 | - * @param mixed $value |
|
| 646 | - * |
|
| 647 | - * @return void |
|
| 648 | - */ |
|
| 649 | - public function setParameter($name, $value): void |
|
| 650 | - { |
|
| 651 | - $this->parameters(); |
|
| 652 | - |
|
| 653 | - $this->parameters[$name] = $value; |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - /** |
|
| 657 | - * Get the key / value list of parameters without null values. |
|
| 658 | - * |
|
| 659 | - * @return array |
|
| 660 | - */ |
|
| 661 | - public function parametersWithouNulls(): array |
|
| 662 | - { |
|
| 663 | - return array_filter($this->parameters(), fn ($parameter) => ! is_null($parameter)); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - /** |
|
| 667 | - * Get the key / value list of parameters for the route. |
|
| 668 | - * |
|
| 669 | - * @return array |
|
| 670 | - */ |
|
| 671 | - public function parameters(): array |
|
| 672 | - { |
|
| 673 | - if (isset($this->parameters)) { |
|
| 674 | - return $this->parameters; |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - throw new LogicException('The route is not bound'); |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - /** |
|
| 681 | - * Get all middleware, including the ones from the controller. |
|
| 682 | - * |
|
| 683 | - * @return array |
|
| 684 | - */ |
|
| 685 | - public function gatherMiddleware(): array |
|
| 686 | - { |
|
| 687 | - if ( ! is_null($this->computedMiddleware)) { |
|
| 688 | - return $this->computedMiddleware; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - $this->computedMiddleware = []; |
|
| 692 | - |
|
| 693 | - return $this->computedMiddleware = Router::uniqueMiddleware(array_merge( |
|
| 694 | - $this->middleware(), |
|
| 695 | - $this->controllerMiddleware() |
|
| 696 | - )); |
|
| 697 | - } |
|
| 698 | - |
|
| 699 | - /** |
|
| 700 | - * Get or set the middlewares attached to the route. |
|
| 701 | - * |
|
| 702 | - * @param array|string|null $middleware |
|
| 703 | - * |
|
| 704 | - * @return array|static |
|
| 705 | - */ |
|
| 706 | - public function middleware($middleware = null) |
|
| 707 | - { |
|
| 708 | - if (is_null($middleware)) { |
|
| 709 | - return $this->getMiddleware(); |
|
| 710 | - } |
|
| 711 | - |
|
| 712 | - if (is_string($middleware)) { |
|
| 713 | - $middleware = func_get_args(); |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - foreach ($middleware as $index => $value) { |
|
| 717 | - $middleware[$index] = (string) $value; |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - $this->action['middleware'] = array_merge( |
|
| 721 | - $this->getMiddleware(), |
|
| 722 | - $middleware |
|
| 723 | - ); |
|
| 724 | - |
|
| 725 | - return $this; |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - /** |
|
| 729 | - * Get the middlewares attached to the route. |
|
| 730 | - * |
|
| 731 | - * @return array |
|
| 732 | - */ |
|
| 733 | - protected function getMiddleware(): array |
|
| 734 | - { |
|
| 735 | - return (array) ($this->action['middleware'] ?? []); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - /** |
|
| 739 | - * Get the middleware for the route's controller. |
|
| 740 | - * |
|
| 741 | - * @return array |
|
| 742 | - */ |
|
| 743 | - public function controllerMiddleware(): array |
|
| 744 | - { |
|
| 745 | - if ( ! $this->isControllerAction()) { |
|
| 746 | - return []; |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - return $this->controllerDispatcher()->getMiddleware( |
|
| 750 | - $this->getController(), |
|
| 751 | - $this->getControllerMethod() |
|
| 752 | - ); |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - /** |
|
| 756 | - * Determine if the route only responds to HTTP requests. |
|
| 757 | - * |
|
| 758 | - * @return bool |
|
| 759 | - */ |
|
| 760 | - public function httpOnly(): bool |
|
| 761 | - { |
|
| 762 | - return in_array('http', $this->action, true); |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * Determine if the route only responds to HTTPS requests. |
|
| 767 | - * |
|
| 768 | - * @return bool |
|
| 769 | - */ |
|
| 770 | - public function httpsOnly(): bool |
|
| 771 | - { |
|
| 772 | - return $this->secure(); |
|
| 773 | - } |
|
| 774 | - |
|
| 775 | - /** |
|
| 776 | - * Determine if the route only responds to HTTPS requests. |
|
| 777 | - * |
|
| 778 | - * @return bool |
|
| 779 | - */ |
|
| 780 | - public function secure(): bool |
|
| 781 | - { |
|
| 782 | - return in_array('https', $this->action, true); |
|
| 783 | - } |
|
| 583 | + $this->parameters = (new RouteParameter($this))->parameters($request); |
|
| 584 | + |
|
| 585 | + return $this; |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + /** |
|
| 589 | + * Compile into a Route Compile instance. |
|
| 590 | + * |
|
| 591 | + * @return \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler |
|
| 592 | + */ |
|
| 593 | + protected function compileRoute() |
|
| 594 | + { |
|
| 595 | + if ( ! $this->compiled) { |
|
| 596 | + $this->compiled = $this->toBundleRoute()->compile(); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + return $this->compiled; |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * Get all of the parameter names for the route. |
|
| 604 | + * |
|
| 605 | + * @return array |
|
| 606 | + */ |
|
| 607 | + public function parameterNames(): array |
|
| 608 | + { |
|
| 609 | + if (isset($this->parameterNames)) { |
|
| 610 | + return $this->parameterNames; |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + return $this->parameterNames = $this->compileParameterNames(); |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + /** |
|
| 617 | + * Get the parameter names for the route. |
|
| 618 | + * |
|
| 619 | + * @return array |
|
| 620 | + */ |
|
| 621 | + protected function compileParameterNames(): array |
|
| 622 | + { |
|
| 623 | + preg_match_all('~\{(.*?)\}~', $this->getDomain().$this->getUri(), $matches); |
|
| 624 | + |
|
| 625 | + return array_map(fn ($match) => trim($match, '?'), $matches[1]); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * Get a given parameter from the route. |
|
| 630 | + * |
|
| 631 | + * @param string $name |
|
| 632 | + * @param mixed $default |
|
| 633 | + * |
|
| 634 | + * @return mixed |
|
| 635 | + */ |
|
| 636 | + public function parameter($name, $default = null) |
|
| 637 | + { |
|
| 638 | + return Arr::get($this->parameters(), $name, $default); |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + /** |
|
| 642 | + * Set a parameter to the given value. |
|
| 643 | + * |
|
| 644 | + * @param string $name |
|
| 645 | + * @param mixed $value |
|
| 646 | + * |
|
| 647 | + * @return void |
|
| 648 | + */ |
|
| 649 | + public function setParameter($name, $value): void |
|
| 650 | + { |
|
| 651 | + $this->parameters(); |
|
| 652 | + |
|
| 653 | + $this->parameters[$name] = $value; |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * Get the key / value list of parameters without null values. |
|
| 658 | + * |
|
| 659 | + * @return array |
|
| 660 | + */ |
|
| 661 | + public function parametersWithouNulls(): array |
|
| 662 | + { |
|
| 663 | + return array_filter($this->parameters(), fn ($parameter) => ! is_null($parameter)); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + /** |
|
| 667 | + * Get the key / value list of parameters for the route. |
|
| 668 | + * |
|
| 669 | + * @return array |
|
| 670 | + */ |
|
| 671 | + public function parameters(): array |
|
| 672 | + { |
|
| 673 | + if (isset($this->parameters)) { |
|
| 674 | + return $this->parameters; |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + throw new LogicException('The route is not bound'); |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + /** |
|
| 681 | + * Get all middleware, including the ones from the controller. |
|
| 682 | + * |
|
| 683 | + * @return array |
|
| 684 | + */ |
|
| 685 | + public function gatherMiddleware(): array |
|
| 686 | + { |
|
| 687 | + if ( ! is_null($this->computedMiddleware)) { |
|
| 688 | + return $this->computedMiddleware; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + $this->computedMiddleware = []; |
|
| 692 | + |
|
| 693 | + return $this->computedMiddleware = Router::uniqueMiddleware(array_merge( |
|
| 694 | + $this->middleware(), |
|
| 695 | + $this->controllerMiddleware() |
|
| 696 | + )); |
|
| 697 | + } |
|
| 698 | + |
|
| 699 | + /** |
|
| 700 | + * Get or set the middlewares attached to the route. |
|
| 701 | + * |
|
| 702 | + * @param array|string|null $middleware |
|
| 703 | + * |
|
| 704 | + * @return array|static |
|
| 705 | + */ |
|
| 706 | + public function middleware($middleware = null) |
|
| 707 | + { |
|
| 708 | + if (is_null($middleware)) { |
|
| 709 | + return $this->getMiddleware(); |
|
| 710 | + } |
|
| 711 | + |
|
| 712 | + if (is_string($middleware)) { |
|
| 713 | + $middleware = func_get_args(); |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + foreach ($middleware as $index => $value) { |
|
| 717 | + $middleware[$index] = (string) $value; |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + $this->action['middleware'] = array_merge( |
|
| 721 | + $this->getMiddleware(), |
|
| 722 | + $middleware |
|
| 723 | + ); |
|
| 724 | + |
|
| 725 | + return $this; |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + /** |
|
| 729 | + * Get the middlewares attached to the route. |
|
| 730 | + * |
|
| 731 | + * @return array |
|
| 732 | + */ |
|
| 733 | + protected function getMiddleware(): array |
|
| 734 | + { |
|
| 735 | + return (array) ($this->action['middleware'] ?? []); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + /** |
|
| 739 | + * Get the middleware for the route's controller. |
|
| 740 | + * |
|
| 741 | + * @return array |
|
| 742 | + */ |
|
| 743 | + public function controllerMiddleware(): array |
|
| 744 | + { |
|
| 745 | + if ( ! $this->isControllerAction()) { |
|
| 746 | + return []; |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + return $this->controllerDispatcher()->getMiddleware( |
|
| 750 | + $this->getController(), |
|
| 751 | + $this->getControllerMethod() |
|
| 752 | + ); |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + /** |
|
| 756 | + * Determine if the route only responds to HTTP requests. |
|
| 757 | + * |
|
| 758 | + * @return bool |
|
| 759 | + */ |
|
| 760 | + public function httpOnly(): bool |
|
| 761 | + { |
|
| 762 | + return in_array('http', $this->action, true); |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * Determine if the route only responds to HTTPS requests. |
|
| 767 | + * |
|
| 768 | + * @return bool |
|
| 769 | + */ |
|
| 770 | + public function httpsOnly(): bool |
|
| 771 | + { |
|
| 772 | + return $this->secure(); |
|
| 773 | + } |
|
| 774 | + |
|
| 775 | + /** |
|
| 776 | + * Determine if the route only responds to HTTPS requests. |
|
| 777 | + * |
|
| 778 | + * @return bool |
|
| 779 | + */ |
|
| 780 | + public function secure(): bool |
|
| 781 | + { |
|
| 782 | + return in_array('https', $this->action, true); |
|
| 783 | + } |
|
| 784 | 784 | |
| 785 | - /** |
|
| 786 | - * Convert the route to a bundle route. |
|
| 787 | - * |
|
| 788 | - * @return \Syscodes\Bundles\ApplicationBundle\Routing\Route |
|
| 789 | - */ |
|
| 790 | - public function toBundleRoute() |
|
| 791 | - { |
|
| 792 | - return new BundleRoute( |
|
| 793 | - preg_replace('~\{(\w+?)\?\}~', '{$1}', $this->getUri()), |
|
| 794 | - $this->getOptionalParameterNames(), |
|
| 795 | - $this->getPatterns(), |
|
| 796 | - $this->getDomain() ?: '' |
|
| 797 | - ); |
|
| 798 | - } |
|
| 785 | + /** |
|
| 786 | + * Convert the route to a bundle route. |
|
| 787 | + * |
|
| 788 | + * @return \Syscodes\Bundles\ApplicationBundle\Routing\Route |
|
| 789 | + */ |
|
| 790 | + public function toBundleRoute() |
|
| 791 | + { |
|
| 792 | + return new BundleRoute( |
|
| 793 | + preg_replace('~\{(\w+?)\?\}~', '{$1}', $this->getUri()), |
|
| 794 | + $this->getOptionalParameterNames(), |
|
| 795 | + $this->getPatterns(), |
|
| 796 | + $this->getDomain() ?: '' |
|
| 797 | + ); |
|
| 798 | + } |
|
| 799 | 799 | |
| 800 | - /** |
|
| 801 | - * Get the optional parameter names for the route. |
|
| 802 | - * |
|
| 803 | - * @return array |
|
| 804 | - */ |
|
| 805 | - protected function getOptionalParameterNames(): array |
|
| 806 | - { |
|
| 807 | - preg_match_all('~\{(\w+?)\?\}~', $this->getUri(), $matches); |
|
| 800 | + /** |
|
| 801 | + * Get the optional parameter names for the route. |
|
| 802 | + * |
|
| 803 | + * @return array |
|
| 804 | + */ |
|
| 805 | + protected function getOptionalParameterNames(): array |
|
| 806 | + { |
|
| 807 | + preg_match_all('~\{(\w+?)\?\}~', $this->getUri(), $matches); |
|
| 808 | 808 | |
| 809 | - return isset($matches[1]) ? array_fill_keys($matches[1], null) : []; |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - /** |
|
| 813 | - * Get the action of the current route. |
|
| 814 | - * |
|
| 815 | - * @return \Closure|string|array |
|
| 816 | - */ |
|
| 817 | - public function getAction() |
|
| 818 | - { |
|
| 819 | - return $this->action; |
|
| 820 | - } |
|
| 809 | + return isset($matches[1]) ? array_fill_keys($matches[1], null) : []; |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + /** |
|
| 813 | + * Get the action of the current route. |
|
| 814 | + * |
|
| 815 | + * @return \Closure|string|array |
|
| 816 | + */ |
|
| 817 | + public function getAction() |
|
| 818 | + { |
|
| 819 | + return $this->action; |
|
| 820 | + } |
|
| 821 | 821 | |
| 822 | - /** |
|
| 823 | - * Get the compiled version of the route. |
|
| 824 | - * |
|
| 825 | - * @return \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute |
|
| 826 | - */ |
|
| 827 | - public function getCompiled() |
|
| 828 | - { |
|
| 829 | - return $this->compiled; |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - /** |
|
| 833 | - * Get the URI associated with the route. |
|
| 834 | - * |
|
| 835 | - * @return string |
|
| 836 | - */ |
|
| 837 | - public function getUri(): string |
|
| 838 | - { |
|
| 839 | - return $this->uri; |
|
| 840 | - } |
|
| 841 | - |
|
| 842 | - /** |
|
| 843 | - * Get the patterns of the current route. |
|
| 844 | - * |
|
| 845 | - * @return array |
|
| 846 | - */ |
|
| 847 | - public function getPatterns(): array |
|
| 848 | - { |
|
| 849 | - return $this->wheres; |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - /** |
|
| 853 | - * Get the request method of the current route. |
|
| 854 | - * |
|
| 855 | - * @return array|string |
|
| 856 | - */ |
|
| 857 | - public function getMethod(): array|string |
|
| 858 | - { |
|
| 859 | - return $this->method; |
|
| 860 | - } |
|
| 861 | - |
|
| 862 | - /** |
|
| 863 | - * Get the url of the current route. |
|
| 864 | - * |
|
| 865 | - * @return string|null |
|
| 866 | - */ |
|
| 867 | - public function getName(): ?string |
|
| 868 | - { |
|
| 869 | - return $this->action['as'] ?? null; |
|
| 870 | - } |
|
| 871 | - |
|
| 872 | - /** |
|
| 873 | - * Set the container instance on the route. |
|
| 874 | - * |
|
| 875 | - * @param \Syscodes\Components\Container\Container $container |
|
| 876 | - * |
|
| 877 | - * @return static |
|
| 878 | - */ |
|
| 879 | - public function setContainer(Container $container): static |
|
| 880 | - { |
|
| 881 | - $this->container = $container; |
|
| 882 | - |
|
| 883 | - return $this; |
|
| 884 | - } |
|
| 885 | - |
|
| 886 | - /** |
|
| 887 | - * Magic method. |
|
| 888 | - * |
|
| 889 | - * Dynamically access route parameters. |
|
| 890 | - * |
|
| 891 | - * @param string $key |
|
| 892 | - * |
|
| 893 | - * @return mixed |
|
| 894 | - */ |
|
| 895 | - public function __get($key) |
|
| 896 | - { |
|
| 897 | - return $this->parameter($key); |
|
| 898 | - } |
|
| 822 | + /** |
|
| 823 | + * Get the compiled version of the route. |
|
| 824 | + * |
|
| 825 | + * @return \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute |
|
| 826 | + */ |
|
| 827 | + public function getCompiled() |
|
| 828 | + { |
|
| 829 | + return $this->compiled; |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + /** |
|
| 833 | + * Get the URI associated with the route. |
|
| 834 | + * |
|
| 835 | + * @return string |
|
| 836 | + */ |
|
| 837 | + public function getUri(): string |
|
| 838 | + { |
|
| 839 | + return $this->uri; |
|
| 840 | + } |
|
| 841 | + |
|
| 842 | + /** |
|
| 843 | + * Get the patterns of the current route. |
|
| 844 | + * |
|
| 845 | + * @return array |
|
| 846 | + */ |
|
| 847 | + public function getPatterns(): array |
|
| 848 | + { |
|
| 849 | + return $this->wheres; |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + /** |
|
| 853 | + * Get the request method of the current route. |
|
| 854 | + * |
|
| 855 | + * @return array|string |
|
| 856 | + */ |
|
| 857 | + public function getMethod(): array|string |
|
| 858 | + { |
|
| 859 | + return $this->method; |
|
| 860 | + } |
|
| 861 | + |
|
| 862 | + /** |
|
| 863 | + * Get the url of the current route. |
|
| 864 | + * |
|
| 865 | + * @return string|null |
|
| 866 | + */ |
|
| 867 | + public function getName(): ?string |
|
| 868 | + { |
|
| 869 | + return $this->action['as'] ?? null; |
|
| 870 | + } |
|
| 871 | + |
|
| 872 | + /** |
|
| 873 | + * Set the container instance on the route. |
|
| 874 | + * |
|
| 875 | + * @param \Syscodes\Components\Container\Container $container |
|
| 876 | + * |
|
| 877 | + * @return static |
|
| 878 | + */ |
|
| 879 | + public function setContainer(Container $container): static |
|
| 880 | + { |
|
| 881 | + $this->container = $container; |
|
| 882 | + |
|
| 883 | + return $this; |
|
| 884 | + } |
|
| 885 | + |
|
| 886 | + /** |
|
| 887 | + * Magic method. |
|
| 888 | + * |
|
| 889 | + * Dynamically access route parameters. |
|
| 890 | + * |
|
| 891 | + * @param string $key |
|
| 892 | + * |
|
| 893 | + * @return mixed |
|
| 894 | + */ |
|
| 895 | + public function __get($key) |
|
| 896 | + { |
|
| 897 | + return $this->parameter($key); |
|
| 898 | + } |
|
| 899 | 899 | } |
| 900 | 900 | \ No newline at end of file |
@@ -37,171 +37,171 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | class RouteResolver |
| 39 | 39 | { |
| 40 | - /** |
|
| 41 | - * The container instance used by the router. |
|
| 42 | - * |
|
| 43 | - * @var \Syscodes\Components\Contracts\Container\Container $container |
|
| 44 | - */ |
|
| 45 | - protected $container; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The currently dispatched route instance. |
|
| 49 | - * |
|
| 50 | - * @var \Syscodes\Components\Routing\Route|null |
|
| 51 | - */ |
|
| 52 | - protected $current; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * The Route instance. |
|
| 56 | - * |
|
| 57 | - * @var \Syscodes\Components\Routing\Router|null |
|
| 58 | - */ |
|
| 59 | - protected $router; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * The route collection instance. |
|
| 63 | - * |
|
| 64 | - * @var \Syscodes\Components\Routing\Collections\RouteCollection $routes |
|
| 65 | - */ |
|
| 66 | - protected $routes; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Constructor. Create a new RouteResolver instance. |
|
| 70 | - * |
|
| 71 | - * @param \Syscodes\Components\Routing\Router $router |
|
| 72 | - * @param \Syscodes\Components\Routing\Collections\RouteCollection $routes |
|
| 73 | - * @param \Syscodes\Components\Contracts\Container\Container|null $container |
|
| 74 | - * |
|
| 75 | - * @return void |
|
| 76 | - */ |
|
| 77 | - public function __construct(Router $router, RouteCollection $routes, Container $container = null) |
|
| 78 | - { |
|
| 79 | - $this->router = $router; |
|
| 80 | - $this->routes = $routes; |
|
| 81 | - $this->container = $container ?: new Container; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Resolve the given route and call the method that belongs to the route. |
|
| 86 | - * |
|
| 87 | - * @param \Syscodes\Components\Http\Request $request |
|
| 88 | - * |
|
| 89 | - * @return \Syscodes\Components\Http\Response |
|
| 90 | - */ |
|
| 91 | - public function resolve(Request $request): Response |
|
| 92 | - { |
|
| 93 | - return $this->dispatchToRoute($request); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Dispatch the request to a route and return the response. |
|
| 98 | - * |
|
| 99 | - * @param \Syscodes\Components\Http\Request $request |
|
| 100 | - * |
|
| 101 | - * @return \Syscodes\Components\Http\Response |
|
| 102 | - */ |
|
| 103 | - protected function dispatchToRoute(Request $request): Response |
|
| 104 | - { |
|
| 105 | - return $this->runRoute($request, $this->findRoute($request)); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Find the route matching a given request. |
|
| 110 | - * |
|
| 111 | - * @param \Syscodes\Components\Http\Request $request |
|
| 112 | - * |
|
| 113 | - * @return \Syscodes\Components\Routing\Route |
|
| 114 | - */ |
|
| 115 | - protected function findRoute($request): Route |
|
| 116 | - { |
|
| 117 | - // Get all register routes with the same request method |
|
| 118 | - $this->current = $route = $this->routes->match($request); |
|
| 119 | - |
|
| 120 | - $this->container->instance(Route::class, $route); |
|
| 121 | - |
|
| 122 | - return $route; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Return the response for the given route. |
|
| 127 | - * |
|
| 128 | - * @param \Syscodes\Components\Http\Request $request |
|
| 129 | - * @param \Syscodes\Components\Routing\Route $route |
|
| 130 | - * |
|
| 131 | - * @return \Syscodes\Components\Http\Response |
|
| 132 | - */ |
|
| 133 | - protected function runRoute(Request $request, Route $route): Response |
|
| 134 | - { |
|
| 135 | - $request->setRouteResolver(fn () => $route); |
|
| 136 | - |
|
| 137 | - return $this->callResponse($request, |
|
| 138 | - $this->runRouteStack($route, $request) |
|
| 139 | - ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Run the given route through a stack response instance. |
|
| 144 | - * |
|
| 145 | - * @param \Syscodes\Components\Routing\Route $route |
|
| 146 | - * @param \Syscodes\Components\Http\Request $request |
|
| 147 | - * |
|
| 148 | - * @return mixed |
|
| 149 | - */ |
|
| 150 | - protected function runRouteStack(Route $route, Request $request) |
|
| 151 | - { |
|
| 152 | - $skipMiddleware = $this->container->bound('middleware.disable') && |
|
| 153 | - ($this->container->make('middleware.disable') === true); |
|
| 40 | + /** |
|
| 41 | + * The container instance used by the router. |
|
| 42 | + * |
|
| 43 | + * @var \Syscodes\Components\Contracts\Container\Container $container |
|
| 44 | + */ |
|
| 45 | + protected $container; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The currently dispatched route instance. |
|
| 49 | + * |
|
| 50 | + * @var \Syscodes\Components\Routing\Route|null |
|
| 51 | + */ |
|
| 52 | + protected $current; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * The Route instance. |
|
| 56 | + * |
|
| 57 | + * @var \Syscodes\Components\Routing\Router|null |
|
| 58 | + */ |
|
| 59 | + protected $router; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * The route collection instance. |
|
| 63 | + * |
|
| 64 | + * @var \Syscodes\Components\Routing\Collections\RouteCollection $routes |
|
| 65 | + */ |
|
| 66 | + protected $routes; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Constructor. Create a new RouteResolver instance. |
|
| 70 | + * |
|
| 71 | + * @param \Syscodes\Components\Routing\Router $router |
|
| 72 | + * @param \Syscodes\Components\Routing\Collections\RouteCollection $routes |
|
| 73 | + * @param \Syscodes\Components\Contracts\Container\Container|null $container |
|
| 74 | + * |
|
| 75 | + * @return void |
|
| 76 | + */ |
|
| 77 | + public function __construct(Router $router, RouteCollection $routes, Container $container = null) |
|
| 78 | + { |
|
| 79 | + $this->router = $router; |
|
| 80 | + $this->routes = $routes; |
|
| 81 | + $this->container = $container ?: new Container; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Resolve the given route and call the method that belongs to the route. |
|
| 86 | + * |
|
| 87 | + * @param \Syscodes\Components\Http\Request $request |
|
| 88 | + * |
|
| 89 | + * @return \Syscodes\Components\Http\Response |
|
| 90 | + */ |
|
| 91 | + public function resolve(Request $request): Response |
|
| 92 | + { |
|
| 93 | + return $this->dispatchToRoute($request); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Dispatch the request to a route and return the response. |
|
| 98 | + * |
|
| 99 | + * @param \Syscodes\Components\Http\Request $request |
|
| 100 | + * |
|
| 101 | + * @return \Syscodes\Components\Http\Response |
|
| 102 | + */ |
|
| 103 | + protected function dispatchToRoute(Request $request): Response |
|
| 104 | + { |
|
| 105 | + return $this->runRoute($request, $this->findRoute($request)); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Find the route matching a given request. |
|
| 110 | + * |
|
| 111 | + * @param \Syscodes\Components\Http\Request $request |
|
| 112 | + * |
|
| 113 | + * @return \Syscodes\Components\Routing\Route |
|
| 114 | + */ |
|
| 115 | + protected function findRoute($request): Route |
|
| 116 | + { |
|
| 117 | + // Get all register routes with the same request method |
|
| 118 | + $this->current = $route = $this->routes->match($request); |
|
| 119 | + |
|
| 120 | + $this->container->instance(Route::class, $route); |
|
| 121 | + |
|
| 122 | + return $route; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Return the response for the given route. |
|
| 127 | + * |
|
| 128 | + * @param \Syscodes\Components\Http\Request $request |
|
| 129 | + * @param \Syscodes\Components\Routing\Route $route |
|
| 130 | + * |
|
| 131 | + * @return \Syscodes\Components\Http\Response |
|
| 132 | + */ |
|
| 133 | + protected function runRoute(Request $request, Route $route): Response |
|
| 134 | + { |
|
| 135 | + $request->setRouteResolver(fn () => $route); |
|
| 136 | + |
|
| 137 | + return $this->callResponse($request, |
|
| 138 | + $this->runRouteStack($route, $request) |
|
| 139 | + ); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Run the given route through a stack response instance. |
|
| 144 | + * |
|
| 145 | + * @param \Syscodes\Components\Routing\Route $route |
|
| 146 | + * @param \Syscodes\Components\Http\Request $request |
|
| 147 | + * |
|
| 148 | + * @return mixed |
|
| 149 | + */ |
|
| 150 | + protected function runRouteStack(Route $route, Request $request) |
|
| 151 | + { |
|
| 152 | + $skipMiddleware = $this->container->bound('middleware.disable') && |
|
| 153 | + ($this->container->make('middleware.disable') === true); |
|
| 154 | 154 | |
| 155 | 155 | |
| 156 | - $middleware = $skipMiddleware ? [] : $this->router->gatherRouteMiddleware($route); |
|
| 157 | - |
|
| 158 | - return (new Pipeline($this->container)) |
|
| 159 | - ->send($request) |
|
| 160 | - ->through($middleware) |
|
| 161 | - ->then(fn ($request) => $this->callResponse($request, $route->runResolver())); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Create a response instance from the given value. |
|
| 166 | - * |
|
| 167 | - * @param \Syscodes\Components\Http\Request $request |
|
| 168 | - * @param mixed $response |
|
| 169 | - * |
|
| 170 | - * @return \Syscodes\Components\Http\Response |
|
| 171 | - */ |
|
| 172 | - public function callResponse($request, $response): Response |
|
| 173 | - { |
|
| 174 | - return static::toResponse($request, $response); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Static version of callResponse. |
|
| 179 | - * |
|
| 180 | - * @param \Syscodes\Components\Http\Request $request |
|
| 181 | - * @param mixed $response |
|
| 182 | - * |
|
| 183 | - * @return \Syscodes\Components\Http\Response |
|
| 184 | - */ |
|
| 185 | - public static function toResponse($request, $response): Response |
|
| 186 | - { |
|
| 187 | - if ( ! $response instanceof Response && |
|
| 188 | - ($response instanceof Jsonserializable || |
|
| 189 | - is_array($response))) { |
|
| 190 | - $response = new JsonResponse($response); |
|
| 191 | - } elseif ( ! $response instanceof Response) { |
|
| 192 | - $response = new Response($response, 200, ['Content-Type' => 'text/html']); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - return $response->prepare($request); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Get the currently dispatched route instance. |
|
| 200 | - * |
|
| 201 | - * @return \Syscodes\Components\Routing\Route|null |
|
| 202 | - */ |
|
| 203 | - public function current() |
|
| 204 | - { |
|
| 205 | - return $this->current; |
|
| 206 | - } |
|
| 156 | + $middleware = $skipMiddleware ? [] : $this->router->gatherRouteMiddleware($route); |
|
| 157 | + |
|
| 158 | + return (new Pipeline($this->container)) |
|
| 159 | + ->send($request) |
|
| 160 | + ->through($middleware) |
|
| 161 | + ->then(fn ($request) => $this->callResponse($request, $route->runResolver())); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Create a response instance from the given value. |
|
| 166 | + * |
|
| 167 | + * @param \Syscodes\Components\Http\Request $request |
|
| 168 | + * @param mixed $response |
|
| 169 | + * |
|
| 170 | + * @return \Syscodes\Components\Http\Response |
|
| 171 | + */ |
|
| 172 | + public function callResponse($request, $response): Response |
|
| 173 | + { |
|
| 174 | + return static::toResponse($request, $response); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Static version of callResponse. |
|
| 179 | + * |
|
| 180 | + * @param \Syscodes\Components\Http\Request $request |
|
| 181 | + * @param mixed $response |
|
| 182 | + * |
|
| 183 | + * @return \Syscodes\Components\Http\Response |
|
| 184 | + */ |
|
| 185 | + public static function toResponse($request, $response): Response |
|
| 186 | + { |
|
| 187 | + if ( ! $response instanceof Response && |
|
| 188 | + ($response instanceof Jsonserializable || |
|
| 189 | + is_array($response))) { |
|
| 190 | + $response = new JsonResponse($response); |
|
| 191 | + } elseif ( ! $response instanceof Response) { |
|
| 192 | + $response = new Response($response, 200, ['Content-Type' => 'text/html']); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + return $response->prepare($request); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Get the currently dispatched route instance. |
|
| 200 | + * |
|
| 201 | + * @return \Syscodes\Components\Routing\Route|null |
|
| 202 | + */ |
|
| 203 | + public function current() |
|
| 204 | + { |
|
| 205 | + return $this->current; |
|
| 206 | + } |
|
| 207 | 207 | } |
| 208 | 208 | \ No newline at end of file |
@@ -29,105 +29,105 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class RouteGroup |
| 31 | 31 | { |
| 32 | - /** |
|
| 33 | - * Merge the given group attributes. |
|
| 34 | - * |
|
| 35 | - * @param array $new |
|
| 36 | - * @param array $old |
|
| 37 | - * @param bool $existsPrefix |
|
| 38 | - * |
|
| 39 | - * @return array |
|
| 40 | - */ |
|
| 41 | - public static function mergeGroup($new, $old, $existsPrefix = true): array |
|
| 42 | - { |
|
| 43 | - if (isset($new['domain'])) { |
|
| 44 | - unset($old['domain']); |
|
| 45 | - } |
|
| 32 | + /** |
|
| 33 | + * Merge the given group attributes. |
|
| 34 | + * |
|
| 35 | + * @param array $new |
|
| 36 | + * @param array $old |
|
| 37 | + * @param bool $existsPrefix |
|
| 38 | + * |
|
| 39 | + * @return array |
|
| 40 | + */ |
|
| 41 | + public static function mergeGroup($new, $old, $existsPrefix = true): array |
|
| 42 | + { |
|
| 43 | + if (isset($new['domain'])) { |
|
| 44 | + unset($old['domain']); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - if (isset($new['controller'])) { |
|
| 48 | - unset($old['controller']); |
|
| 49 | - } |
|
| 47 | + if (isset($new['controller'])) { |
|
| 48 | + unset($old['controller']); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - $new = array_merge(static::formatUseAs($new, $old), [ |
|
| 52 | - 'namespace' => static::formatUseNamespace($new, $old), |
|
| 53 | - 'prefix' => static::formatUsePrefix($new, $old, $existsPrefix), |
|
| 54 | - 'where' => static::formatUseWhere($new, $old) |
|
| 55 | - ]); |
|
| 51 | + $new = array_merge(static::formatUseAs($new, $old), [ |
|
| 52 | + 'namespace' => static::formatUseNamespace($new, $old), |
|
| 53 | + 'prefix' => static::formatUsePrefix($new, $old, $existsPrefix), |
|
| 54 | + 'where' => static::formatUseWhere($new, $old) |
|
| 55 | + ]); |
|
| 56 | 56 | |
| 57 | - return array_merge_recursive( |
|
| 58 | - Arr::except($old, array('namespace', 'prefix', 'where', 'as')), $new |
|
| 59 | - ); |
|
| 60 | - } |
|
| 57 | + return array_merge_recursive( |
|
| 58 | + Arr::except($old, array('namespace', 'prefix', 'where', 'as')), $new |
|
| 59 | + ); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Format the uses namespace for the new group attributes. |
|
| 64 | - * |
|
| 65 | - * @param array $new |
|
| 66 | - * @param array $old |
|
| 67 | - * |
|
| 68 | - * @return string|null |
|
| 69 | - */ |
|
| 70 | - protected static function formatUseNamespace($new, $old): ?string |
|
| 71 | - { |
|
| 72 | - if (isset($new['namespace'])) { |
|
| 73 | - return isset($old['namespace']) |
|
| 74 | - ? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\') |
|
| 75 | - : trim($new['namespace'], '\\'); |
|
| 76 | - } |
|
| 62 | + /** |
|
| 63 | + * Format the uses namespace for the new group attributes. |
|
| 64 | + * |
|
| 65 | + * @param array $new |
|
| 66 | + * @param array $old |
|
| 67 | + * |
|
| 68 | + * @return string|null |
|
| 69 | + */ |
|
| 70 | + protected static function formatUseNamespace($new, $old): ?string |
|
| 71 | + { |
|
| 72 | + if (isset($new['namespace'])) { |
|
| 73 | + return isset($old['namespace']) |
|
| 74 | + ? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\') |
|
| 75 | + : trim($new['namespace'], '\\'); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - return $old['namespace'] ?? null; |
|
| 79 | - } |
|
| 78 | + return $old['namespace'] ?? null; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Format the prefix for the new group attributes. |
|
| 83 | - * |
|
| 84 | - * @param array $new |
|
| 85 | - * @param array $old |
|
| 86 | - * @param bool $existPrefix |
|
| 87 | - * |
|
| 88 | - * @return string|null |
|
| 89 | - */ |
|
| 90 | - protected static function formatUsePrefix($new, $old, bool $existsPrefix = true): ?string |
|
| 91 | - { |
|
| 92 | - $old = $old['prefix'] ?? null; |
|
| 81 | + /** |
|
| 82 | + * Format the prefix for the new group attributes. |
|
| 83 | + * |
|
| 84 | + * @param array $new |
|
| 85 | + * @param array $old |
|
| 86 | + * @param bool $existPrefix |
|
| 87 | + * |
|
| 88 | + * @return string|null |
|
| 89 | + */ |
|
| 90 | + protected static function formatUsePrefix($new, $old, bool $existsPrefix = true): ?string |
|
| 91 | + { |
|
| 92 | + $old = $old['prefix'] ?? null; |
|
| 93 | 93 | |
| 94 | - if ($existsPrefix) { |
|
| 95 | - return trim(isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old, '/'); |
|
| 96 | - } else { |
|
| 97 | - return trim(isset($new['prefix']) ? trim($new['prefix'], '/').'/'.trim($old, '/') : $old, '/'); |
|
| 98 | - } |
|
| 99 | - } |
|
| 94 | + if ($existsPrefix) { |
|
| 95 | + return trim(isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old, '/'); |
|
| 96 | + } else { |
|
| 97 | + return trim(isset($new['prefix']) ? trim($new['prefix'], '/').'/'.trim($old, '/') : $old, '/'); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Format the "wheres" for the new group attributes. |
|
| 103 | - * |
|
| 104 | - * @param array $new |
|
| 105 | - * @param array $old |
|
| 106 | - * |
|
| 107 | - * @return array |
|
| 108 | - */ |
|
| 109 | - protected static function formatUseWhere($new, $old): array |
|
| 110 | - { |
|
| 111 | - return array_merge( |
|
| 112 | - $old['where'] ?? [], |
|
| 113 | - $new['where'] ?? [] |
|
| 114 | - ); |
|
| 115 | - } |
|
| 101 | + /** |
|
| 102 | + * Format the "wheres" for the new group attributes. |
|
| 103 | + * |
|
| 104 | + * @param array $new |
|
| 105 | + * @param array $old |
|
| 106 | + * |
|
| 107 | + * @return array |
|
| 108 | + */ |
|
| 109 | + protected static function formatUseWhere($new, $old): array |
|
| 110 | + { |
|
| 111 | + return array_merge( |
|
| 112 | + $old['where'] ?? [], |
|
| 113 | + $new['where'] ?? [] |
|
| 114 | + ); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Format the "as" clause of the new group attributes. |
|
| 119 | - * |
|
| 120 | - * @param array $new |
|
| 121 | - * @param array $old |
|
| 122 | - * |
|
| 123 | - * @return array |
|
| 124 | - */ |
|
| 125 | - protected static function formatUseAs($new, $old): array |
|
| 126 | - { |
|
| 127 | - if (isset($old['as'])) { |
|
| 128 | - $new['as'] = $old['as'].($new['as'] ?? ''); |
|
| 129 | - } |
|
| 117 | + /** |
|
| 118 | + * Format the "as" clause of the new group attributes. |
|
| 119 | + * |
|
| 120 | + * @param array $new |
|
| 121 | + * @param array $old |
|
| 122 | + * |
|
| 123 | + * @return array |
|
| 124 | + */ |
|
| 125 | + protected static function formatUseAs($new, $old): array |
|
| 126 | + { |
|
| 127 | + if (isset($old['as'])) { |
|
| 128 | + $new['as'] = $old['as'].($new['as'] ?? ''); |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - return $new; |
|
| 132 | - } |
|
| 131 | + return $new; |
|
| 132 | + } |
|
| 133 | 133 | } |
| 134 | 134 | \ No newline at end of file |