Skip to content

Commit

Permalink
Hotfix exclude api register routes (gnikyt#619)
Browse files Browse the repository at this point in the history
* Fixes manual routes handling for api routes

* Adds more robust function for manual routes, updates test
  • Loading branch information
honzah1 authored Nov 20, 2020
1 parent e9b0e96 commit b5a1b46
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/ShopifyApp/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,24 @@ function base64url_decode($data)
/**
* Checks if the route should be registered or not.
*
* @param string $routeName The route name to check.
* @param bool|array $routes The routes which are to be excluded.
* @param string $routeToCheck The route name to check.
* @param bool|array $routesToExclude The routes which are to be excluded.
*
* @return bool
*/
function registerPackageRoute(string $routeName, $routes): bool
function registerPackageRoute(string $routeToCheck, $routesToExclude): bool
{
return ! (is_array($routes) && in_array($routeName, $routes));
if ($routesToExclude === false) {
return true;
}

if ($routesToExclude === true) {
throw new \LogicException('Excluded routes can be false, or an array');
}

if (is_array($routesToExclude) === false) {
throw new \LogicException('Excluded routes must be an array');
}

return in_array($routeToCheck, $routesToExclude, true) === false;
}
2 changes: 1 addition & 1 deletion src/ShopifyApp/resources/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$manualRoutes = explode(',', $manualRoutes);
}

Route::group(['middleware' => ['api']], function ($manualRoutes) {
Route::group(['middleware' => ['api']], function () use ($manualRoutes) {
/*
|--------------------------------------------------------------------------
| API Routes
Expand Down
5 changes: 5 additions & 0 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ public function testRegisterPackageRoutes(): void
// Routes to exclude
$routes = explode(',', 'home,billing');

$this->assertTrue(registerPackageRoute('authenticate', false));
$this->assertTrue(registerPackageRoute('authenticate', []));
$this->assertTrue(registerPackageRoute('authenticate', $routes));
$this->assertFalse(registerPackageRoute('home', $routes));

$this->expectErrorMessage('Excluded routes must be an array');
registerPackageRoute('home', \stdClass::class);
}

public function testRouteNames(): void
Expand Down

0 comments on commit b5a1b46

Please sign in to comment.