Skip to content

Commit 7c9a32e

Browse files
authored
Add support for route generation using FQCN (#323)
1 parent 45503e8 commit 7c9a32e

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

config/blueprint.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,18 @@
102102

103103
'use_guarded' => false,
104104

105+
/*
106+
|--------------------------------------------------------------------------
107+
| Generate FQCN Routes
108+
|--------------------------------------------------------------------------
109+
|
110+
| By default, Blueprint follows the Laravel convention of the controller
111+
| namespace matches the namespace set within the RouteServiceProvider.
112+
| However, you may configure Blueprint to generate routes using a
113+
| "tuple syntax" in cases where you may not use this property
114+
| or wish to improve static analysis.
115+
|
116+
*/
117+
'generate_fqcn_route' => false,
118+
105119
];

src/Generators/RouteGenerator.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,19 @@ protected function buildRoutes(Controller $controller)
5252
$routes = '';
5353
$methods = array_keys($controller->methods());
5454

55-
$className = str_replace('App\Http\Controllers\\', '', $controller->fullyQualifiedClassName());
55+
$useTuples = config('blueprint.generate_fqcn_route');
56+
57+
$className = $useTuples
58+
? $controller->fullyQualifiedClassName() . '::class'
59+
: '\'' . str_replace('App\Http\Controllers\\', '', $controller->fullyQualifiedClassName()) . '\'';
60+
5661
$slug = Str::kebab($controller->prefix());
5762

5863
$resource_methods = array_intersect($methods, Controller::$resourceMethods);
5964
if (count($resource_methods)) {
6065
$routes .= $controller->isApiResource()
61-
? sprintf("Route::apiResource('%s', '%s')", $slug, $className)
62-
: sprintf("Route::resource('%s', '%s')", $slug, $className);
66+
? sprintf("Route::apiResource('%s', %s)", $slug, $className)
67+
: sprintf("Route::resource('%s', %s)", $slug, $className);
6368

6469
$missing_methods = $controller->isApiResource()
6570
? array_diff(Controller::$apiResourceMethods, $resource_methods)
@@ -78,7 +83,14 @@ protected function buildRoutes(Controller $controller)
7883

7984
$methods = array_diff($methods, Controller::$resourceMethods);
8085
foreach ($methods as $method) {
81-
$routes .= sprintf("Route::get('%s/%s', '%s@%s');", $slug, Str::kebab($method), $className, $method);
86+
if ($useTuples) {
87+
$action = "[{$className}, '{$method}']";
88+
} else {
89+
$classNameNoQuotes = trim($className, '\'');
90+
$action = "'{$classNameNoQuotes}@{$method}'";
91+
}
92+
93+
$routes .= sprintf("Route::get('%s/%s', %s);", $slug, Str::kebab($method), $action);
8294
$routes .= PHP_EOL;
8395
}
8496

tests/Feature/Generator/RouteGeneratorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ public function output_generates_routes_for_mixed_resources()
8888
$this->assertEquals(['updated' => ['routes/api.php', 'routes/web.php']], $this->subject->output($tree));
8989
}
9090

91+
/**
92+
* @test
93+
*/
94+
public function output_generates_routes_using_tuples()
95+
{
96+
config(['blueprint.generate_fqcn_route' => true]);
97+
98+
$this->files->expects('append')
99+
->with('routes/web.php', $this->fixture('routes/routes-tuples.php'));
100+
101+
$tokens = $this->blueprint->parse($this->fixture('drafts/routes-tuples.yaml'));
102+
$tree = $this->blueprint->analyze($tokens);
103+
104+
$this->subject->output($tree);
105+
}
106+
91107
public function controllerTreeDataProvider()
92108
{
93109
return [
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
models:
2+
Foo:
3+
name: string
4+
5+
controllers:
6+
Foo:
7+
resource
8+
Some:
9+
whatever:
10+
redirect: some.index
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
Route::resource('foo', App\Http\Controllers\FooController::class);
4+
5+
Route::get('some/whatever', [App\Http\Controllers\SomeController::class, 'whatever']);

0 commit comments

Comments
 (0)