Skip to content

Commit 9b947e1

Browse files
committed
Add custom make:model command with remote option
Introduces a custom ModelMakeCommand that adds a --remote option to generate models extending the package base model. Registers the command in the service provider and adds a corresponding stub. Includes a test to verify remote model generation.
1 parent 89db92d commit 9b947e1

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

src/Console/ModelMakeCommand.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Kroderdev\LaravelMicroserviceCore\Console;
4+
5+
use Illuminate\Foundation\Console\ModelMakeCommand as BaseModelMakeCommand;
6+
use Symfony\Component\Console\Attribute\AsCommand;
7+
use Symfony\Component\Console\Input\InputOption;
8+
9+
#[AsCommand(name: 'make:model', description: 'Create a new Eloquent model class')]
10+
class ModelMakeCommand extends BaseModelMakeCommand
11+
{
12+
protected function getOptions()
13+
{
14+
return array_merge(parent::getOptions(), [
15+
['remote', null, InputOption::VALUE_NONE, 'Indicates the model should extend the package base model'],
16+
]);
17+
}
18+
19+
protected function getStub()
20+
{
21+
if ($this->option('remote')) {
22+
return __DIR__.'/stubs/remote-model.stub';
23+
}
24+
25+
return parent::getStub();
26+
}
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
{{ factoryImport }}
6+
use Kroderdev\LaravelMicroserviceCore\Models\Model;
7+
8+
class {{ class }} extends Model
9+
{
10+
{{ factory }}
11+
}

src/Providers/MicroserviceServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,24 @@ public function register(): void
4141
$this->app->bind(ApiGatewayClientInterface::class, fn ($app) => $app->make(ApiGatewayClientFactory::class)->default());
4242
$this->app->scoped(PermissionsClient::class, fn ($app) => new PermissionsClient($app->make(ApiGatewayClientInterface::class)));
4343
$this->app->singleton(AuthServiceClient::class, fn () => new AuthServiceClient());
44+
45+
$this->app->singleton(\Illuminate\Foundation\Console\ModelMakeCommand::class, function ($app) {
46+
return new \Kroderdev\LaravelMicroserviceCore\Console\ModelMakeCommand($app['files']);
47+
});
4448
}
4549

4650
/**
4751
* Bootstrap services.
4852
*/
4953
public function boot(Router $router): void
5054
{
55+
// Commands
56+
if ($this->app->runningInConsole()) {
57+
\Illuminate\Console\Application::starting(function ($artisan) {
58+
$artisan->add(new \Kroderdev\LaravelMicroserviceCore\Console\ModelMakeCommand($this->app['files']));
59+
});
60+
}
61+
5162
// Publish config
5263
$this->publishes([
5364
__DIR__.'/../config/microservice.php' => config_path('microservice.php'),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tests\Commands;
4+
5+
use Illuminate\Filesystem\Filesystem;
6+
use Kroderdev\LaravelMicroserviceCore\Providers\MicroserviceServiceProvider;
7+
use Orchestra\Testbench\TestCase;
8+
9+
class MakeModelCommandTest extends TestCase
10+
{
11+
protected function getPackageProviders($app)
12+
{
13+
return [MicroserviceServiceProvider::class];
14+
}
15+
16+
public function test_generates_remote_model()
17+
{
18+
$this->artisan('make:model', ['name' => 'RemoteUser', '--remote' => true])
19+
->assertExitCode(0);
20+
21+
$path = app_path('Models/RemoteUser.php');
22+
$this->assertFileExists($path);
23+
$contents = (new Filesystem())->get($path);
24+
$this->assertStringContainsString('Kroderdev\\LaravelMicroserviceCore\\Models\\Model', $contents);
25+
}
26+
}

0 commit comments

Comments
 (0)