Skip to content

Commit

Permalink
Enable API routes if necessary before generating code (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodevs authored Jan 4, 2025
1 parent d73aabd commit 3a67fdf
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Generators/RouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function output(Tree $tree): array

$paths = [];

if (isset($routes['api'])) {
$this->setupApiRouter();
}

foreach (array_filter($routes) as $type => $definitions) {
$path = 'routes/' . $type . '.php';
$this->filesystem->append($path, $definitions . PHP_EOL);
Expand Down Expand Up @@ -93,4 +97,38 @@ protected function buildRouteLine($className, $slug, $method): string

return sprintf("Route::get('%s/%s', [%s, '%s']);", $slug, Str::kebab($method), $className, $method);
}

protected function setupApiRouter(): void
{
$this->createApiRoutesFileIfMissing();
$this->configureApiRoutesInAppBootstrap();
}

protected function createApiRoutesFileIfMissing(): void
{
$apiPath = 'routes/api.php';
if (!$this->filesystem->exists($apiPath)) {
$this->filesystem->put($apiPath, $this->filesystem->stub('routes.api.stub'));
}
}

protected function configureApiRoutesInAppBootstrap(): void
{
$appBootstrapPath = 'bootstrap/app.php';
$content = $this->filesystem->get($appBootstrapPath);

if (str_contains($content, '// api: ')) {
$this->filesystem->replaceInFile(
'// api: ',
'api: ',
$appBootstrapPath,
);
} elseif (str_contains($content, 'web: __DIR__.\'/../routes/web.php\',')) {
$this->filesystem->replaceInFile(
'web: __DIR__.\'/../routes/web.php\',',
'web: __DIR__.\'/../routes/web.php\',' . PHP_EOL . ' api: __DIR__.\'/../routes/api.php\',',
$appBootstrapPath,
);
}
}
}
3 changes: 3 additions & 0 deletions stubs/routes.api.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

use Illuminate\Support\Facades\Route;
78 changes: 78 additions & 0 deletions tests/Feature/Generators/RouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,84 @@ public function output_generates_routes_for_mixed_resources(): void
$this->assertEquals(['updated' => ['routes/api.php', 'routes/web.php']], $this->subject->output($tree));
}

#[Test]
public function output_creates_api_routes_file_when_missing(): void
{
$this->filesystem->expects('exists')
->with('routes/api.php')
->andReturn(false);

$this->filesystem->expects('stub')
->with('routes.api.stub')
->andReturn($this->stub('routes.api.stub'));

$this->filesystem->expects('put')
->with('routes/api.php', $this->stub('routes.api.stub'));

$this->filesystem->expects('append')
->with('routes/api.php', $this->fixture('routes/api-routes.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
}

#[Test]
public function output_does_not_create_api_routes_file_when_it_exists(): void
{
$this->filesystem->shouldReceive('exists')
->with('routes/api.php')
->andReturn(true);

$this->filesystem->expects('put')
->with('routes/api.php', $this->anything())
->never();

$tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
}

#[Test]
public function output_adds_api_route_line_to_bootstrap_if_missing(): void
{
$this->filesystem->expects('get')
->with('bootstrap/app.php')
->andReturn("web: __DIR__.'/../routes/web.php',");

$this->filesystem->shouldReceive('replaceInFile')
->with(
'web: __DIR__.\'/../routes/web.php\',',
'web: __DIR__.\'/../routes/web.php\',' . PHP_EOL . ' api: __DIR__.\'/../routes/api.php\',',
'bootstrap/app.php'
)
->once();

$tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
}

#[Test]
public function output_uncomments_api_route_line_in_bootstrap_if_commented(): void
{
$this->filesystem->expects('get')
->with('bootstrap/app.php')
->andReturn("// api: \nweb: __DIR__.'/../routes/web.php',");

$this->filesystem->shouldReceive('replaceInFile')
->with('// api: ', 'api: ', 'bootstrap/app.php')
->once();

$tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
}

public static function controllerTreeDataProvider(): array
{
return [
Expand Down

0 comments on commit 3a67fdf

Please sign in to comment.