Skip to content

Commit 2c40000

Browse files
wip
- Added the ability to disable route discovery at controller level
1 parent 0512dc3 commit 2c40000

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Orion\Concerns;
4+
5+
trait DisableRouteDiscovery
6+
{
7+
/**
8+
* @var bool $routeDiscoveryDisabled
9+
*/
10+
protected $routeDiscoveryDisabled = true;
11+
}

src/Concerns/HandlesRouteDiscovery.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ trait HandlesRouteDiscovery
2727
protected static $routeMiddleware = [];
2828
protected static $withoutRouteMiddleware = [];
2929

30+
/**
31+
* Determine whether route auto discovery is enabled.
32+
*
33+
* @return bool
34+
*/
35+
public function routeDiscoveryEnabled(): bool
36+
{
37+
return !property_exists($this, 'routeDiscoveryDisabled');
38+
}
39+
3040
public static function registerRoutes(): void
3141
{
3242
if (static::isRelationController()) {
@@ -59,7 +69,7 @@ protected static function registerRelationRoutes(): void
5969
$controller = static::class;
6070
$instance = app($controller);
6171

62-
$model = isset($instance->model) ? $instance->model : null;
72+
$model = $instance->model ?? null;
6373
$relation = isset($instance->relation) ? $instance->relation : null;
6474
$type = isset($instance->resourceType) ? $instance->resourceType : static::detectRelationType($model, $relation);
6575

@@ -74,7 +84,6 @@ protected static function registerRelationRoutes(): void
7484
->prefix(static::getRoutePrefix())
7585
->name(static::getRouteNamePrefix() . '.')
7686
->group(function () use ($type, $parentSlug, $relation, $controller, $instance) {
77-
$route = null;
7887

7988
switch ($type) {
8089
case 'hasOne':

src/Http/Controllers/BaseController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Support\Facades\Auth;
1616
use Orion\Concerns\BuildsResponses;
1717
use Orion\Concerns\HandlesAuthorization;
18+
use Orion\Concerns\HandlesRouteDiscovery;
1819
use Orion\Concerns\HandlesTransactions;
1920
use Orion\Concerns\InteractsWithBatchResources;
2021
use Orion\Concerns\InteractsWithHooks;
@@ -39,6 +40,7 @@ abstract class BaseController extends \Illuminate\Routing\Controller
3940
InteractsWithSoftDeletes,
4041
InteractsWithBatchResources,
4142
BuildsResponses,
43+
HandlesRouteDiscovery,
4244
HandlesTransactions;
4345

4446
/**

src/OrionServiceProvider.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\ServiceProvider;
7+
use Illuminate\Support\Str;
78
use Orion\Commands\BuildSpecsCommand;
89
use Orion\Contracts\ComponentsResolver;
910
use Orion\Contracts\KeyResolver;
@@ -12,6 +13,8 @@
1213
use Orion\Contracts\QueryBuilder;
1314
use Orion\Contracts\RelationsResolver;
1415
use Orion\Contracts\SearchBuilder;
16+
use Orion\Http\Controllers\Controller;
17+
use Orion\Http\Controllers\RelationController;
1518
use Orion\Http\Middleware\EnforceExpectsJson;
1619
use Orion\Specs\ResourcesCacheStore;
1720

@@ -70,28 +73,46 @@ public function boot()
7073
protected function discoverAndRegisterControllers(): void
7174
{
7275
$paths = config('orion.route_discovery.paths', []);
73-
$baseNamespace = trim(config('orion.namespaces.controllers', 'App\\Http\\Controllers\\'), '\\');
7476

7577
foreach ($paths as $path) {
7678
if (! is_dir($path)) {
7779
continue;
7880
}
7981

80-
foreach (File::allFiles($path) as $file) {
81-
$relativePath = str($file->getRelativePathname())
82-
->replace(DIRECTORY_SEPARATOR, '\\')
83-
->replace('.php', '')
84-
->value();
82+
$namespace = $this->pathToNamespace($path);
8583

86-
$class = "$baseNamespace\\$relativePath";
84+
foreach (File::allFiles($path) as $file) {
85+
$class = $namespace . '\\' . str_replace(
86+
['/', '.php'],
87+
['\\', ''],
88+
$file->getRelativePathname()
89+
);
8790

8891
if (
8992
class_exists($class) &&
90-
method_exists($class, 'registerRoutes')
93+
(
94+
is_subclass_of($class, Controller::class) ||
95+
is_subclass_of($class, RelationController::class)
96+
)
9197
) {
98+
$instance = app($class);
99+
100+
if (method_exists($instance, 'routeDiscoveryEnabled') && !$instance->routeDiscoveryEnabled()) {
101+
continue;
102+
}
103+
92104
$class::registerRoutes();
93105
}
94106
}
95107
}
96108
}
109+
110+
protected function pathToNamespace(string $path): string
111+
{
112+
return Str::of($path)
113+
->after(base_path('app'))
114+
->replace('/', '\\')
115+
->prepend('App')
116+
->__toString();
117+
}
97118
}

0 commit comments

Comments
 (0)