From f097608e14c379f52545745d769f602a3b710277 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Thu, 30 Jan 2020 10:46:39 +0200 Subject: [PATCH 1/3] Wrap routes in the default config --- src/Repositories/Repository.php | 5 ++++- src/RestifyCustomRoutesProvider.php | 15 +++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index 123b5c1fa..18e86bba4 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -186,8 +186,11 @@ public function __call($method, $parameters) * * @param Router $router * @param array $attributes + * @param bool $wrap Choose the routes defined in the @routes method, should be wrapped in a group with attributes by default. + * If true then all routes will be grouped in a configuration attributes passed by restify, otherwise + * you should take care of that, by adding $router->group($attributes) in the @routes method */ - public static function routes(Router $router, $attributes) + public static function routes(Router $router, $attributes, $wrap = false) { $router->group($attributes, function ($router) { // override for custom routes diff --git a/src/RestifyCustomRoutesProvider.php b/src/RestifyCustomRoutesProvider.php index 269143269..87e2a7020 100644 --- a/src/RestifyCustomRoutesProvider.php +++ b/src/RestifyCustomRoutesProvider.php @@ -29,7 +29,7 @@ protected function registerRoutes() { collect(Restify::$repositories)->each(function ($repository) { $config = [ - 'namespace' => trim(app()->getNamespace(), '\\').'\Http\Controllers', + 'namespace' => trim(app()->getNamespace(), '\\') . '\Http\Controllers', 'as' => '', 'prefix' => Restify::path($repository::uriKey()), 'middleware' => config('restify.middleware', []), @@ -41,13 +41,16 @@ protected function registerRoutes() $parameters = $method->getParameters(); - if (count($parameters) === 2 && $parameters[1] instanceof \ReflectionParameter) { -// $config = array_merge($config, $parameters[1]->getDefaultValue()); + $wrap = []; + + if (count($parameters) === 3 && $parameters[1] instanceof \ReflectionParameter) { + $default = $parameters[1]->isDefaultValueAvailable() ? $parameters[1]->getDefaultValue() : []; + $config = array_merge($config, $default); + + $wrap = ($parameters[2]->isDefaultValueAvailable() && $parameters[2]->getDefaultValueConstantName()) ? $config : []; } - Route::group([], function ($router) use ($repository, $config) { - if ($repository === 'Binaryk\LaravelRestify\Tests\RepositoryWithRoutes') { - } + Route::group($wrap, function ($router) use ($repository, $config) { $repository::routes($router, $config); }); }); From 960ba2684063563321ced641f9b99ab8cf5b8eac Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Thu, 30 Jan 2020 10:48:58 +0200 Subject: [PATCH 2/3] Backward compatibility --- src/RestifyCustomRoutesProvider.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/RestifyCustomRoutesProvider.php b/src/RestifyCustomRoutesProvider.php index 87e2a7020..3eaa6cc78 100644 --- a/src/RestifyCustomRoutesProvider.php +++ b/src/RestifyCustomRoutesProvider.php @@ -43,11 +43,14 @@ protected function registerRoutes() $wrap = []; - if (count($parameters) === 3 && $parameters[1] instanceof \ReflectionParameter) { + if (count($parameters) >= 2 && $parameters[1] instanceof \ReflectionParameter) { $default = $parameters[1]->isDefaultValueAvailable() ? $parameters[1]->getDefaultValue() : []; $config = array_merge($config, $default); - $wrap = ($parameters[2]->isDefaultValueAvailable() && $parameters[2]->getDefaultValueConstantName()) ? $config : []; + } + + if (count($parameters) === 3) { + $wrap = ($parameters[2]->isDefaultValueAvailable() && $parameters[2]->getDefaultValue()) ? $config : []; } Route::group($wrap, function ($router) use ($repository, $config) { From 43a2aa9d1cce93f0d577023a14317148a519f682 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Thu, 30 Jan 2020 10:49:15 +0200 Subject: [PATCH 3/3] Apply fixes from StyleCI (#128) --- src/RestifyCustomRoutesProvider.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/RestifyCustomRoutesProvider.php b/src/RestifyCustomRoutesProvider.php index 3eaa6cc78..e654e2d38 100644 --- a/src/RestifyCustomRoutesProvider.php +++ b/src/RestifyCustomRoutesProvider.php @@ -29,7 +29,7 @@ protected function registerRoutes() { collect(Restify::$repositories)->each(function ($repository) { $config = [ - 'namespace' => trim(app()->getNamespace(), '\\') . '\Http\Controllers', + 'namespace' => trim(app()->getNamespace(), '\\').'\Http\Controllers', 'as' => '', 'prefix' => Restify::path($repository::uriKey()), 'middleware' => config('restify.middleware', []), @@ -46,7 +46,6 @@ protected function registerRoutes() if (count($parameters) >= 2 && $parameters[1] instanceof \ReflectionParameter) { $default = $parameters[1]->isDefaultValueAvailable() ? $parameters[1]->getDefaultValue() : []; $config = array_merge($config, $default); - } if (count($parameters) === 3) {