Skip to content

Commit 3a67fdf

Browse files
authored
Enable API routes if necessary before generating code (#728)
1 parent d73aabd commit 3a67fdf

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

src/Generators/RouteGenerator.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public function output(Tree $tree): array
2929

3030
$paths = [];
3131

32+
if (isset($routes['api'])) {
33+
$this->setupApiRouter();
34+
}
35+
3236
foreach (array_filter($routes) as $type => $definitions) {
3337
$path = 'routes/' . $type . '.php';
3438
$this->filesystem->append($path, $definitions . PHP_EOL);
@@ -93,4 +97,38 @@ protected function buildRouteLine($className, $slug, $method): string
9397

9498
return sprintf("Route::get('%s/%s', [%s, '%s']);", $slug, Str::kebab($method), $className, $method);
9599
}
100+
101+
protected function setupApiRouter(): void
102+
{
103+
$this->createApiRoutesFileIfMissing();
104+
$this->configureApiRoutesInAppBootstrap();
105+
}
106+
107+
protected function createApiRoutesFileIfMissing(): void
108+
{
109+
$apiPath = 'routes/api.php';
110+
if (!$this->filesystem->exists($apiPath)) {
111+
$this->filesystem->put($apiPath, $this->filesystem->stub('routes.api.stub'));
112+
}
113+
}
114+
115+
protected function configureApiRoutesInAppBootstrap(): void
116+
{
117+
$appBootstrapPath = 'bootstrap/app.php';
118+
$content = $this->filesystem->get($appBootstrapPath);
119+
120+
if (str_contains($content, '// api: ')) {
121+
$this->filesystem->replaceInFile(
122+
'// api: ',
123+
'api: ',
124+
$appBootstrapPath,
125+
);
126+
} elseif (str_contains($content, 'web: __DIR__.\'/../routes/web.php\',')) {
127+
$this->filesystem->replaceInFile(
128+
'web: __DIR__.\'/../routes/web.php\',',
129+
'web: __DIR__.\'/../routes/web.php\',' . PHP_EOL . ' api: __DIR__.\'/../routes/api.php\',',
130+
$appBootstrapPath,
131+
);
132+
}
133+
}
96134
}

stubs/routes.api.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Route;

tests/Feature/Generators/RouteGeneratorTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,84 @@ public function output_generates_routes_for_mixed_resources(): void
109109
$this->assertEquals(['updated' => ['routes/api.php', 'routes/web.php']], $this->subject->output($tree));
110110
}
111111

112+
#[Test]
113+
public function output_creates_api_routes_file_when_missing(): void
114+
{
115+
$this->filesystem->expects('exists')
116+
->with('routes/api.php')
117+
->andReturn(false);
118+
119+
$this->filesystem->expects('stub')
120+
->with('routes.api.stub')
121+
->andReturn($this->stub('routes.api.stub'));
122+
123+
$this->filesystem->expects('put')
124+
->with('routes/api.php', $this->stub('routes.api.stub'));
125+
126+
$this->filesystem->expects('append')
127+
->with('routes/api.php', $this->fixture('routes/api-routes.php'));
128+
129+
$tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml'));
130+
$tree = $this->blueprint->analyze($tokens);
131+
132+
$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
133+
}
134+
135+
#[Test]
136+
public function output_does_not_create_api_routes_file_when_it_exists(): void
137+
{
138+
$this->filesystem->shouldReceive('exists')
139+
->with('routes/api.php')
140+
->andReturn(true);
141+
142+
$this->filesystem->expects('put')
143+
->with('routes/api.php', $this->anything())
144+
->never();
145+
146+
$tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml'));
147+
$tree = $this->blueprint->analyze($tokens);
148+
149+
$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
150+
}
151+
152+
#[Test]
153+
public function output_adds_api_route_line_to_bootstrap_if_missing(): void
154+
{
155+
$this->filesystem->expects('get')
156+
->with('bootstrap/app.php')
157+
->andReturn("web: __DIR__.'/../routes/web.php',");
158+
159+
$this->filesystem->shouldReceive('replaceInFile')
160+
->with(
161+
'web: __DIR__.\'/../routes/web.php\',',
162+
'web: __DIR__.\'/../routes/web.php\',' . PHP_EOL . ' api: __DIR__.\'/../routes/api.php\',',
163+
'bootstrap/app.php'
164+
)
165+
->once();
166+
167+
$tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml'));
168+
$tree = $this->blueprint->analyze($tokens);
169+
170+
$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
171+
}
172+
173+
#[Test]
174+
public function output_uncomments_api_route_line_in_bootstrap_if_commented(): void
175+
{
176+
$this->filesystem->expects('get')
177+
->with('bootstrap/app.php')
178+
->andReturn("// api: \nweb: __DIR__.'/../routes/web.php',");
179+
180+
$this->filesystem->shouldReceive('replaceInFile')
181+
->with('// api: ', 'api: ', 'bootstrap/app.php')
182+
->once();
183+
184+
$tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml'));
185+
$tree = $this->blueprint->analyze($tokens);
186+
187+
$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
188+
}
189+
112190
public static function controllerTreeDataProvider(): array
113191
{
114192
return [

0 commit comments

Comments
 (0)