Skip to content

Commit c5d525f

Browse files
authored
Merge pull request #127 from erhanurgun/fix/quickstart-command-duplicate-routes
Fixed duplicate route creation issue in QuickStartCommand
2 parents 86683af + f2372df commit c5d525f

File tree

3 files changed

+127
-20
lines changed

3 files changed

+127
-20
lines changed

src/Console/Commands/QuickStartCommand.php

+43-18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Lomkit\Rest\Console\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Str;
67
use Lomkit\Rest\Console\ResolvesStubPath;
78

89
class QuickStartCommand extends Command
@@ -32,37 +33,41 @@ public function handle()
3233
{
3334
$this->comment('Generating User Resource...');
3435
$this->callSilent('rest:resource', ['name' => 'UserResource']);
35-
copy($this->resolveStubPath('/stubs/user-resource.stub'), app_path('Rest/Resources/UserResource.php'));
36-
37-
if (file_exists(app_path('Models/User.php'))) {
38-
file_put_contents(
39-
app_path('Rest/Resources/UserResource.php'),
40-
str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Rest/Resources/UserResource.php')))
41-
);
42-
}
4336

4437
$this->comment('Generating User Controller...');
4538
$this->callSilent('rest:controller', ['name' => 'UsersController']);
46-
copy($this->resolveStubPath('/stubs/user-controller.stub'), app_path('Rest/Controllers/UsersController.php'));
39+
40+
$this->updateUserModelNamespace();
41+
$this->setAppNamespace();
42+
$this->updateApiRoutes();
43+
44+
$this->info('Laravel Rest Api is ready. Type \'php artisan route:list\' to see your new routes !');
45+
}
46+
47+
/**
48+
* Update the User model namespace in the generated files.
49+
*
50+
* @return void
51+
*/
52+
protected function updateUserModelNamespace()
53+
{
54+
$resource = app_path('Rest/Resources/UserResource.php');
4755

4856
if (file_exists(app_path('Models/User.php'))) {
4957
file_put_contents(
50-
app_path('Rest/Controllers/UsersController.php'),
51-
str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Rest/Controllers/UsersController.php')))
58+
$resource,
59+
str_replace('App\Models\Model::class', 'App\Models\User::class', file_get_contents($resource))
5260
);
5361
}
5462

55-
$this->setAppNamespace();
63+
$controller = app_path('Rest/Controllers/UsersController.php');
5664

57-
if (file_exists(base_path('routes/api.php'))) {
65+
if (file_exists(app_path('Models/User.php'))) {
5866
file_put_contents(
59-
base_path('routes/api.php'),
60-
file_get_contents(base_path('routes/api.php')).
61-
'\Lomkit\Rest\Facades\Rest::resource(\'users\', \App\Rest\Controllers\UsersController::class);'
67+
$controller,
68+
str_replace('App\Rest\Resources\ModelResource::class', 'App\Rest\Resources\UserResource::class', file_get_contents($controller))
6269
);
6370
}
64-
65-
$this->info('Laravel Rest Api is ready. Type \'php artisan route:list\' to see your new routes !');
6671
}
6772

6873
/**
@@ -94,4 +99,24 @@ protected function setAppNamespaceOn($file, $namespace)
9499
file_get_contents($file)
95100
));
96101
}
102+
103+
/**
104+
* Update the api routes file to include the new resource.
105+
*
106+
* @return void
107+
*/
108+
protected function updateApiRoutes()
109+
{
110+
$routesPath = base_path('routes/api.php');
111+
if (!file_exists($routesPath)) {
112+
file_put_contents($routesPath, '<?php');
113+
}
114+
115+
$routeContent = file_get_contents($routesPath);
116+
$newRoute = "\Lomkit\Rest\Facades\Rest::resource('users', \App\Rest\Controllers\UsersController::class);";
117+
118+
if (!Str::contains($routeContent, $newRoute)) {
119+
file_put_contents($routesPath, $routeContent.PHP_EOL.$newRoute);
120+
}
121+
}
97122
}

src/Console/Commands/ResourceCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ protected function getPath($name)
7070
*/
7171
protected function buildClass($name)
7272
{
73-
$model = $this->option('model') ?? 'App\\Models\\Model';
73+
$model = $this->option('model');
7474

7575
$modelNamespace = $this->getModelNamespace();
7676

7777
if (is_null($model)) {
78-
$resource = $modelNamespace.str_replace('/', '\\', $this->argument('name'));
78+
$model = $modelNamespace.'Model';
7979
} elseif (!Str::startsWith($model, [
8080
$modelNamespace, '\\',
8181
])) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Feature\Commands;
4+
5+
use Illuminate\Support\Facades\File;
6+
use Lomkit\Rest\Tests\Feature\TestCase;
7+
8+
class QuickStartCommandTest extends TestCase
9+
{
10+
protected function setUp(): void
11+
{
12+
parent::setUp();
13+
14+
// Ensure api.php exists for tests
15+
if (!File::exists(base_path('routes/api.php'))) {
16+
File::put(base_path('routes/api.php'), '<?php');
17+
}
18+
}
19+
20+
protected function tearDown(): void
21+
{
22+
$this->cleanUp();
23+
parent::tearDown();
24+
}
25+
26+
protected function cleanUp(): void
27+
{
28+
File::deleteDirectory(app_path('Rest'));
29+
File::deleteDirectory(app_path('Models'));
30+
File::delete(base_path('routes/api.php'));
31+
}
32+
33+
public function test_quick_start_command_creates_necessary_files()
34+
{
35+
$this->artisan('rest:quick-start')->assertExitCode(0);
36+
37+
$this->assertFileExists(app_path('Rest/Resources/UserResource.php'));
38+
$this->assertFileExists(app_path('Rest/Controllers/UsersController.php'));
39+
}
40+
41+
public function test_quick_start_command_updates_api_routes()
42+
{
43+
$this->artisan('rest:quick-start')->assertExitCode(0);
44+
45+
$routeContent = File::get(base_path('routes/api.php'));
46+
$this->assertStringContainsString(
47+
"\Lomkit\Rest\Facades\Rest::resource('users', \App\Rest\Controllers\UsersController::class);",
48+
$routeContent
49+
);
50+
}
51+
52+
public function test_quick_start_command_does_not_duplicate_routes()
53+
{
54+
$this->artisan('rest:quick-start')->assertExitCode(0);
55+
$this->artisan('rest:quick-start')->assertExitCode(0);
56+
57+
$routeContent = File::get(base_path('routes/api.php'));
58+
$count = substr_count($routeContent, "\Lomkit\Rest\Facades\Rest::resource('users', \App\Rest\Controllers\UsersController::class);");
59+
$this->assertEquals(1, $count, 'The route should only appear once in the file.');
60+
}
61+
62+
public function test_quick_start_command_updates_user_model_namespace()
63+
{
64+
// Simulate the existence of App\Models\User
65+
File::makeDirectory(app_path('Models'), 0755, true);
66+
File::put(app_path('Models/User.php'), '<?php namespace App\Models; class User {}');
67+
68+
$this->artisan('rest:quick-start')->assertExitCode(0);
69+
70+
$this->assertFileExists(app_path('Rest/Resources/UserResource.php'));
71+
$this->assertFileExists(app_path('Rest/Controllers/UsersController.php'));
72+
73+
$resourceContent = File::get(app_path('Rest/Resources/UserResource.php'));
74+
$controllerContent = File::get(app_path('Rest/Controllers/UsersController.php'));
75+
76+
$this->assertStringContainsString('\App\Models\User::class', $resourceContent);
77+
78+
$this->assertStringContainsString('\App\Rest\Resources\UserResource::class', $controllerContent);
79+
80+
$this->assertStringContainsString('public static $model = \App\Models\User::class;', $resourceContent);
81+
}
82+
}

0 commit comments

Comments
 (0)