From 667a14dd3de39a6109951a605708adfb41305631 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 3 Mar 2020 18:41:52 +0200 Subject: [PATCH] 3.x - Wrap routes in the default config (#127) * Wrap routes in the default config * Backward compatibility * Apply fixes from StyleCI (#128) --- src/Repositories/Repository.php | 5 ++++- src/RestifyCustomRoutesProvider.php | 15 ++++++++++----- 2 files changed, 14 insertions(+), 6 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..e654e2d38 100644 --- a/src/RestifyCustomRoutesProvider.php +++ b/src/RestifyCustomRoutesProvider.php @@ -41,13 +41,18 @@ 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) >= 2 && $parameters[1] instanceof \ReflectionParameter) { + $default = $parameters[1]->isDefaultValueAvailable() ? $parameters[1]->getDefaultValue() : []; + $config = array_merge($config, $default); + } + + if (count($parameters) === 3) { + $wrap = ($parameters[2]->isDefaultValueAvailable() && $parameters[2]->getDefaultValue()) ? $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); }); });