From 2dc27b9acdb0d9363eb0147908d10c197d144b5b Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 25 May 2020 14:27:59 +0300 Subject: [PATCH 1/3] Adding support for controller input and method in an array with the name of the route --- src/Routing/Router.php | 148 ++++++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 70 deletions(-) diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 108cc95d2..c3469c979 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -92,11 +92,11 @@ class Router /** * Create a new router instance. * - * @param \Dingo\Api\Contract\Routing\Adapter $adapter - * @param \Dingo\Api\Contract\Debug\ExceptionHandler $exception - * @param \Illuminate\Container\Container $container - * @param string $domain - * @param string $prefix + * @param \Dingo\Api\Contract\Routing\Adapter $adapter + * @param \Dingo\Api\Contract\Debug\ExceptionHandler $exception + * @param \Illuminate\Container\Container $container + * @param string $domain + * @param string $prefix * * @return void */ @@ -117,9 +117,9 @@ public function __construct(Adapter $adapter, ExceptionHandler $exception, Conta * This method can be called without the third parameter, however, * the callback should always be the last parameter. * - * @param array|string $version - * @param array|callable $second - * @param callable $third + * @param array|string $version + * @param array|callable $second + * @param callable $third * * @return void */ @@ -139,30 +139,30 @@ public function version($version, $second, $third = null) /** * Create a new route group. * - * @param array $attributes - * @param callable $callback + * @param array $attributes + * @param callable $callback * * @return void */ public function group(array $attributes, $callback) { - if (! isset($attributes['conditionalRequest'])) { + if (!isset($attributes['conditionalRequest'])) { $attributes['conditionalRequest'] = $this->conditionalRequest; } $attributes = $this->mergeLastGroupAttributes($attributes); - if (! isset($attributes['version'])) { + if (!isset($attributes['version'])) { throw new RuntimeException('A version is required for an API group definition.'); } else { $attributes['version'] = (array) $attributes['version']; } - if ((! isset($attributes['prefix']) || empty($attributes['prefix'])) && isset($this->prefix)) { + if ((!isset($attributes['prefix']) || empty($attributes['prefix'])) && isset($this->prefix)) { $attributes['prefix'] = $this->prefix; } - if ((! isset($attributes['domain']) || empty($attributes['domain'])) && isset($this->domain)) { + if ((!isset($attributes['domain']) || empty($attributes['domain'])) && isset($this->domain)) { $attributes['domain'] = $this->domain; } @@ -176,8 +176,8 @@ public function group(array $attributes, $callback) /** * Create a new GET route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -189,8 +189,8 @@ public function get($uri, $action) /** * Create a new POST route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -202,8 +202,8 @@ public function post($uri, $action) /** * Create a new PUT route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -215,8 +215,8 @@ public function put($uri, $action) /** * Create a new PATCH route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -228,8 +228,8 @@ public function patch($uri, $action) /** * Create a new DELETE route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -241,8 +241,8 @@ public function delete($uri, $action) /** * Create a new OPTIONS route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -254,8 +254,8 @@ public function options($uri, $action) /** * Create a new route that responding to all verbs. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -269,9 +269,9 @@ public function any($uri, $action) /** * Create a new route with the given verbs. * - * @param array|string $methods - * @param string $uri - * @param array|string|callable $action + * @param array|string $methods + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -283,7 +283,7 @@ public function match($methods, $uri, $action) /** * Register an array of resources. * - * @param array $resources + * @param array $resources * * @return void */ @@ -303,9 +303,9 @@ public function resources(array $resources) /** * Register a resource controller. * - * @param string $name - * @param string $controller - * @param array $options + * @param string $name + * @param string $controller + * @param array $options * * @return void */ @@ -323,9 +323,9 @@ public function resource($name, $controller, array $options = []) /** * Add a route to the routing adapter. * - * @param string|array $methods - * @param string $uri - * @param string|array|callable $action + * @param string|array $methods + * @param string $uri + * @param string|array|callable $action * * @return mixed */ @@ -341,6 +341,14 @@ public function addRoute($methods, $uri, $action) $action = implode('@', $action); $action = ['uses' => $action, 'controller' => $action]; } + + // For named routes and syntax: + // $api->post('login', ['uses' => [LoginController::class, 'login'], 'as' => 'login'] + if (isset($action['uses']) && isset($action['as'])) { + $route_name = $action['as']; + $action = implode('@', $action['uses']); + $action = ['uses' => $action, 'controller' => $action, 'as' => $route_name]; + } } $action = $this->mergeLastGroupAttributes($action); @@ -349,7 +357,7 @@ public function addRoute($methods, $uri, $action) $uri = $uri === '/' ? $uri : '/'.trim($uri, '/'); - if (! empty($action['prefix'])) { + if (!empty($action['prefix'])) { $uri = '/'.ltrim(rtrim(trim($action['prefix'], '/').'/'.trim($uri, '/'), '/'), '/'); unset($action['prefix']); @@ -363,7 +371,7 @@ public function addRoute($methods, $uri, $action) /** * Add the controller preparation middleware to the beginning of the routes middleware. * - * @param array $action + * @param array $action * * @return array */ @@ -377,7 +385,7 @@ protected function addControllerMiddlewareToRouteAction(array $action) /** * Merge the last groups attributes. * - * @param array $attributes + * @param array $attributes * * @return array */ @@ -393,8 +401,8 @@ protected function mergeLastGroupAttributes(array $attributes) /** * Merge the given group attributes. * - * @param array $new - * @param array $old + * @param array $new + * @param array $old * * @return array */ @@ -432,8 +440,8 @@ protected function mergeGroup(array $new, array $old) /** * Format an array based option in a route action. * - * @param string $option - * @param array $new + * @param string $option + * @param array $new * * @return array */ @@ -447,8 +455,8 @@ protected function formatArrayBasedOption($option, array $new) /** * Format the uses key in a route action. * - * @param array $new - * @param array $old + * @param array $new + * @param array $old * * @return string */ @@ -464,8 +472,8 @@ protected function formatUses(array $new, array $old) /** * Format the namespace for the new group attributes. * - * @param array $new - * @param array $old + * @param array $new + * @param array $old * * @return string */ @@ -483,8 +491,8 @@ protected function formatNamespace(array $new, array $old) /** * Format the prefix for the new group attributes. * - * @param array $new - * @param array $old + * @param array $new + * @param array $old * * @return string */ @@ -500,11 +508,11 @@ protected function formatPrefix($new, $old) /** * Dispatch a request via the adapter. * - * @param \Dingo\Api\Http\Request $request + * @param \Dingo\Api\Http\Request $request * + * @return \Dingo\Api\Http\Response * @throws \Exception * - * @return \Dingo\Api\Http\Response */ public function dispatch(Request $request) { @@ -532,9 +540,9 @@ public function dispatch(Request $request) /** * Prepare a response by transforming and formatting it correctly. * - * @param mixed $response - * @param \Dingo\Api\Http\Request $request - * @param string $format + * @param mixed $response + * @param \Dingo\Api\Http\Request $request + * @param string $format * * @return \Dingo\Api\Http\Response */ @@ -561,7 +569,7 @@ protected function prepareResponse($response, Request $request, $format) } if ($response->isSuccessful() && $this->requestIsConditional()) { - if (! $response->headers->has('ETag')) { + if (!$response->headers->has('ETag')) { $response->setEtag(sha1($response->getContent() ?: '')); } @@ -574,7 +582,7 @@ protected function prepareResponse($response, Request $request, $format) /** * Gather the middleware for the given route. * - * @param mixed $route + * @param mixed $route * * @return array */ @@ -596,7 +604,7 @@ protected function requestIsConditional() /** * Set the conditional request. * - * @param bool $conditionalRequest + * @param bool $conditionalRequest * * @return void */ @@ -624,7 +632,7 @@ public function getCurrentRoute() { if (isset($this->currentRoute)) { return $this->currentRoute; - } elseif (! $this->hasDispatchedRoutes() || ! $route = $this->container['request']->route()) { + } elseif (!$this->hasDispatchedRoutes() || !$route = $this->container['request']->route()) { return; } @@ -644,7 +652,7 @@ public function current() /** * Create a new route instance from an adapter route. * - * @param array|\Illuminate\Routing\Route $route + * @param array|\Illuminate\Routing\Route $route * * @return \Dingo\Api\Routing\Route */ @@ -656,7 +664,7 @@ public function createRoute($route) /** * Set the current route instance. * - * @param \Dingo\Api\Routing\Route $route + * @param \Dingo\Api\Routing\Route $route * * @return void */ @@ -672,7 +680,7 @@ public function setCurrentRoute(Route $route) */ public function hasGroupStack() { - return ! empty($this->groupStack); + return !empty($this->groupStack); } /** @@ -694,7 +702,7 @@ public function getLastGroupPrefix() /** * Get all routes registered on the adapter. * - * @param string $version + * @param string $version * * @return mixed */ @@ -702,7 +710,7 @@ public function getRoutes($version = null) { $routes = $this->adapter->getIterableRoutes($version); - if (! is_null($version)) { + if (!is_null($version)) { $routes = [$version => $routes]; } @@ -734,7 +742,7 @@ public function getAdapterRoutes() /** * Set the raw adapter routes. * - * @param array $routes + * @param array $routes * * @return void */ @@ -778,7 +786,7 @@ public function currentRouteName() /** * Alias for the "currentRouteNamed" method. * - * @param mixed string + * @param mixed string * * @return bool */ @@ -796,7 +804,7 @@ public function is() /** * Determine if the current route matches a given name. * - * @param string $name + * @param string $name * * @return bool */ @@ -812,7 +820,7 @@ public function currentRouteNamed($name) */ public function currentRouteAction() { - if (! $route = $this->current()) { + if (!$route = $this->current()) { return; } @@ -842,7 +850,7 @@ public function uses() /** * Determine if the current route action matches a given action. * - * @param string $action + * @param string $action * * @return bool */ From fae27b72352e2e45c76041b740f28511611e25d3 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 25 May 2020 14:33:18 +0300 Subject: [PATCH 2/3] code style --- src/Routing/Router.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Routing/Router.php b/src/Routing/Router.php index c3469c979..2b597240c 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -146,23 +146,23 @@ public function version($version, $second, $third = null) */ public function group(array $attributes, $callback) { - if (!isset($attributes['conditionalRequest'])) { + if (! isset($attributes['conditionalRequest'])) { $attributes['conditionalRequest'] = $this->conditionalRequest; } $attributes = $this->mergeLastGroupAttributes($attributes); - if (!isset($attributes['version'])) { + if (! isset($attributes['version'])) { throw new RuntimeException('A version is required for an API group definition.'); } else { $attributes['version'] = (array) $attributes['version']; } - if ((!isset($attributes['prefix']) || empty($attributes['prefix'])) && isset($this->prefix)) { + if ((! isset($attributes['prefix']) || empty($attributes['prefix'])) && isset($this->prefix)) { $attributes['prefix'] = $this->prefix; } - if ((!isset($attributes['domain']) || empty($attributes['domain'])) && isset($this->domain)) { + if ((! isset($attributes['domain']) || empty($attributes['domain'])) && isset($this->domain)) { $attributes['domain'] = $this->domain; } @@ -357,7 +357,7 @@ public function addRoute($methods, $uri, $action) $uri = $uri === '/' ? $uri : '/'.trim($uri, '/'); - if (!empty($action['prefix'])) { + if (! empty($action['prefix'])) { $uri = '/'.ltrim(rtrim(trim($action['prefix'], '/').'/'.trim($uri, '/'), '/'), '/'); unset($action['prefix']); @@ -569,7 +569,7 @@ protected function prepareResponse($response, Request $request, $format) } if ($response->isSuccessful() && $this->requestIsConditional()) { - if (!$response->headers->has('ETag')) { + if (! $response->headers->has('ETag')) { $response->setEtag(sha1($response->getContent() ?: '')); } @@ -632,7 +632,7 @@ public function getCurrentRoute() { if (isset($this->currentRoute)) { return $this->currentRoute; - } elseif (!$this->hasDispatchedRoutes() || !$route = $this->container['request']->route()) { + } elseif (! $this->hasDispatchedRoutes() || ! $route = $this->container['request']->route()) { return; } @@ -710,7 +710,7 @@ public function getRoutes($version = null) { $routes = $this->adapter->getIterableRoutes($version); - if (!is_null($version)) { + if (! is_null($version)) { $routes = [$version => $routes]; } @@ -820,7 +820,7 @@ public function currentRouteNamed($name) */ public function currentRouteAction() { - if (!$route = $this->current()) { + if (! $route = $this->current()) { return; } From e313d8a3e1605f7f6efe7944c58f99445e5d0e66 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 25 May 2020 14:34:21 +0300 Subject: [PATCH 3/3] code style 2 --- src/Routing/Router.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 2b597240c..bff7f2bbb 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -510,9 +510,9 @@ protected function formatPrefix($new, $old) * * @param \Dingo\Api\Http\Request $request * - * @return \Dingo\Api\Http\Response * @throws \Exception * + * @return \Dingo\Api\Http\Response */ public function dispatch(Request $request) { @@ -680,7 +680,7 @@ public function setCurrentRoute(Route $route) */ public function hasGroupStack() { - return !empty($this->groupStack); + return ! empty($this->groupStack); } /**