Skip to content

Commit f6ff752

Browse files
author
Tyler King
authored
Ability to override internal routes gnikyt#596 (gnikyt#598)
* Ability to override internal routes gnikyt#596 * Prevent multiple include of registerPackageRoutes * Moved registerPackageRoute to helper * StyleCI fixes * Remove bool casting on routes config
1 parent 1c4f9c3 commit f6ff752

File tree

4 files changed

+107
-46
lines changed

4 files changed

+107
-46
lines changed

src/ShopifyApp/helpers.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,16 @@ function parseQueryString(string $qs, string $d = null): array
8484

8585
return $params;
8686
}
87+
88+
/**
89+
* Checks if the route should be registered or not.
90+
*
91+
* @param string $routeName The route name to check.
92+
* @param bool|array $routes The routes which are to be excluded.
93+
*
94+
* @return bool
95+
*/
96+
function registerPackageRoute(string $routeName, $routes): bool
97+
{
98+
return ! (is_array($routes) && in_array($routeName, $routes));
99+
}

src/ShopifyApp/resources/config/shopify-app.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323
*/
2424
'manual_migrations' => (bool) env('SHOPIFY_MANUAL_MIGRATIONS', false),
2525

26+
/*
27+
|--------------------------------------------------------------------------
28+
| Manual routes
29+
|--------------------------------------------------------------------------
30+
|
31+
| This option allows you to ignore the package's built-in routes.
32+
| Use `false` (default) for allowing the built-in routes. Otherwise, you
33+
| can list out which route "names" you would like excluded.
34+
| See `resources/routes.php` for list of route names available.
35+
| Example: `home,billing` would ignore both "home" and "billing" routes.
36+
|
37+
*/
38+
'manual_routes' => env('SHOPIFY_MANUAL_ROUTES', false),
39+
2640
/*
2741
|--------------------------------------------------------------------------
2842
| Namespace

src/ShopifyApp/resources/routes.php

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@
99
|
1010
*/
1111

12-
Route::group(['prefix' => config('shopify-app.prefix'), 'middleware' => ['web']], function () {
12+
use Illuminate\Support\Facades\Config;
13+
use Illuminate\Support\Facades\Route;
14+
use function Osiset\ShopifyApp\registerPackageRoute;
15+
16+
// Check if manual routes override is to be use
17+
$manualRoutes = Config::get('shopify-app.manual_routes');
18+
if ($manualRoutes) {
19+
// Get a list of route names to exclude
20+
$manualRoutes = explode(',', $manualRoutes);
21+
}
22+
23+
Route::group(['prefix' => config('shopify-app.prefix'), 'middleware' => ['web']], function () use ($manualRoutes) {
1324
/*
1425
|--------------------------------------------------------------------------
1526
| Home Route
@@ -20,12 +31,14 @@
2031
|
2132
*/
2233

23-
Route::get(
24-
'/',
25-
'Osiset\ShopifyApp\Http\Controllers\HomeController@index'
26-
)
27-
->middleware(['auth.shopify', 'billable'])
28-
->name('home');
34+
if (registerPackageRoute('home', $manualRoutes)) {
35+
Route::get(
36+
'/',
37+
'Osiset\ShopifyApp\Http\Controllers\HomeController@index'
38+
)
39+
->middleware(['auth.shopify', 'billable'])
40+
->name('home');
41+
}
2942

3043
/*
3144
|--------------------------------------------------------------------------
@@ -36,12 +49,14 @@
3649
|
3750
*/
3851

39-
Route::match(
40-
['get', 'post'],
41-
'/authenticate',
42-
'Osiset\ShopifyApp\Http\Controllers\AuthController@authenticate'
43-
)
44-
->name('authenticate');
52+
if (registerPackageRoute('authenticate', $manualRoutes)) {
53+
Route::match(
54+
['get', 'post'],
55+
'/authenticate',
56+
'Osiset\ShopifyApp\Http\Controllers\AuthController@authenticate'
57+
)
58+
->name('authenticate');
59+
}
4560

4661
/*
4762
|--------------------------------------------------------------------------
@@ -52,11 +67,13 @@
5267
|
5368
*/
5469

55-
Route::get(
56-
'/authenticate/oauth',
57-
'Osiset\ShopifyApp\Http\Controllers\AuthController@oauth'
58-
)
59-
->name('authenticate.oauth');
70+
if (registerPackageRoute('authenticate.oauth', $manualRoutes)) {
71+
Route::get(
72+
'/authenticate/oauth',
73+
'Osiset\ShopifyApp\Http\Controllers\AuthController@oauth'
74+
)
75+
->name('authenticate.oauth');
76+
}
6077

6178
/*
6279
|--------------------------------------------------------------------------
@@ -67,13 +84,15 @@
6784
|
6885
*/
6986

70-
Route::get(
71-
'/billing/{plan?}',
72-
'Osiset\ShopifyApp\Http\Controllers\BillingController@index'
73-
)
74-
->middleware(['auth.shopify'])
75-
->where('plan', '^([0-9]+|)$')
76-
->name('billing');
87+
if (registerPackageRoute('billing', $manualRoutes)) {
88+
Route::get(
89+
'/billing/{plan?}',
90+
'Osiset\ShopifyApp\Http\Controllers\BillingController@index'
91+
)
92+
->middleware(['auth.shopify'])
93+
->where('plan', '^([0-9]+|)$')
94+
->name('billing');
95+
}
7796

7897
/*
7998
|--------------------------------------------------------------------------
@@ -84,13 +103,15 @@
84103
|
85104
*/
86105

87-
Route::get(
88-
'/billing/process/{plan?}',
89-
'Osiset\ShopifyApp\Http\Controllers\BillingController@process'
90-
)
91-
->middleware(['auth.shopify'])
92-
->where('plan', '^([0-9]+|)$')
93-
->name('billing.process');
106+
if (registerPackageRoute('billing.process', $manualRoutes)) {
107+
Route::get(
108+
'/billing/process/{plan?}',
109+
'Osiset\ShopifyApp\Http\Controllers\BillingController@process'
110+
)
111+
->middleware(['auth.shopify'])
112+
->where('plan', '^([0-9]+|)$')
113+
->name('billing.process');
114+
}
94115

95116
/*
96117
|--------------------------------------------------------------------------
@@ -101,16 +122,18 @@
101122
|
102123
*/
103124

104-
Route::match(
105-
['get', 'post'],
106-
'/billing/usage-charge',
107-
'Osiset\ShopifyApp\Http\Controllers\BillingController@usageCharge'
108-
)
109-
->middleware(['auth.shopify'])
110-
->name('billing.usage_charge');
125+
if (registerPackageRoute('billing.usage_charge', $manualRoutes)) {
126+
Route::match(
127+
['get', 'post'],
128+
'/billing/usage-charge',
129+
'Osiset\ShopifyApp\Http\Controllers\BillingController@usageCharge'
130+
)
131+
->middleware(['auth.shopify'])
132+
->name('billing.usage_charge');
133+
}
111134
});
112135

113-
Route::group(['middleware' => ['api']], function () {
136+
Route::group(['middleware' => ['api']], function () use ($manualRoutes) {
114137
/*
115138
|--------------------------------------------------------------------------
116139
| Webhook Handler
@@ -120,10 +143,12 @@
120143
|
121144
*/
122145

123-
Route::post(
124-
'/webhook/{type}',
125-
'Osiset\ShopifyApp\Http\Controllers\WebhookController@handle'
126-
)
127-
->middleware('auth.webhook')
128-
->name('webhook');
146+
if (registerPackageRoute('webhook', $manualRoutes)) {
147+
Route::post(
148+
'/webhook/{type}',
149+
'Osiset\ShopifyApp\Http\Controllers\WebhookController@handle'
150+
)
151+
->middleware('auth.webhook')
152+
->name('webhook');
153+
}
129154
});

tests/HelpersTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,13 @@ public function testHmacCreator(): void
3232
createHmac(['data' => $data, 'buildQuery' => true], $secret)
3333
);
3434
}
35+
36+
public function testRegisterPackageRoutes(): void
37+
{
38+
// Routes to exclude
39+
$routes = explode(',', 'home,billing');
40+
41+
$this->assertTrue(registerPackageRoute('authenticate', $routes));
42+
$this->assertFalse(registerPackageRoute('home', $routes));
43+
}
3544
}

0 commit comments

Comments
 (0)