Skip to content

Commit 1c3ae40

Browse files
Merge pull request #4 from mehedijaman/main
Feature: Artisan Factory Create Command
2 parents b814578 + 797f190 commit 1c3ae40

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

src/Console/MakeFactoryCommand.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Modular\Modular\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Str;
8+
use Modular\Modular\Console\InstallerTraits\ModuleExists;
9+
10+
class MakeFactoryCommand extends Command
11+
{
12+
use ModuleExists;
13+
14+
protected $signature = 'modular:make-factory {moduleName} {resourceName}';
15+
16+
protected $description = 'Create a new factory class for a Module';
17+
18+
protected string $moduleName;
19+
20+
protected string $resourceName;
21+
22+
public function handle(): ?int
23+
{
24+
$this->moduleName = Str::studly($this->argument('moduleName'));
25+
$this->resourceName = Str::studly($this->argument('resourceName'));
26+
27+
if (! $this->moduleExists()) {
28+
return self::FAILURE;
29+
}
30+
31+
$this->comment('Module '.$this->moduleName.' found, creating Factory...');
32+
$this->createModuleFactory();
33+
34+
return self::SUCCESS;
35+
}
36+
37+
private function createModuleFactory(): void
38+
{
39+
(new Filesystem)->ensureDirectoryExists(base_path("modules/{$this->moduleName}/Database/Factories"));
40+
41+
$stub = file_get_contents(__DIR__.'/../../stubs/module-stub/modules/Database/Factories/ModelFactory.stub');
42+
43+
$stub = str_replace('{{ ModuleName }}', $this->moduleName, $stub);
44+
$stub = str_replace('{{ ResourceName }}', $this->resourceName, $stub);
45+
$stub = str_replace('{{ resourceName }}', Str::camel($this->resourceName), $stub);
46+
47+
$path = base_path("modules/{$this->moduleName}/Database/Factories/{$this->resourceName}Factory.php");
48+
49+
file_put_contents($path, $stub);
50+
}
51+
}

src/Console/MakeModuleCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function handle(): ?int
3535
$this->call('modular:make-route', $params);
3636

3737
$this->call('modular:make-migration', ['moduleName' => $this->moduleName, 'migrationName' => "create{$this->moduleName}s_table"]);
38+
$this->call('modular:make-factory', $params);
3839

3940
$this->call('modular:make-page', $params);
4041
$this->call('modular:make-test', $params);
@@ -82,6 +83,8 @@ private function createModuleDirectoryStructure(): bool
8283
(new Filesystem)->makeDirectory("{$modulePath}/routes");
8384
(new Filesystem)->makeDirectory("{$modulePath}/Tests");
8485
(new Filesystem)->makeDirectory("{$modulePath}/Database/Migrations", 0755, true);
86+
(new Filesystem)->makeDirectory("{$modulePath}/Database/Factories", 0755, true);
87+
(new Filesystem)->makeDirectory("{$modulePath}/Database/Seeders", 0755, true);
8588

8689
return true;
8790
}

src/ModularServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Modular\Modular\Console\MakeComponentCommand;
88
use Modular\Modular\Console\MakeComposableCommand;
99
use Modular\Modular\Console\MakeControllerCommand;
10+
use Modular\Modular\Console\MakeFactoryCommand;
1011
use Modular\Modular\Console\MakeMigrationCommand;
1112
use Modular\Modular\Console\MakeModelCommand;
1213
use Modular\Modular\Console\MakeModuleCommand;
@@ -47,6 +48,7 @@ public function configurePackage(Package $package): void
4748
->hasCommand(MakeTestCommand::class)
4849
->hasCommand(MakeMigrationCommand::class)
4950
->hasCommand(PublishLaravelTranslationsCommand::class)
50-
->hasCommand(PublishSiteFilesCommand::class);
51+
->hasCommand(PublishSiteFilesCommand::class)
52+
->hasCommand(MakeFactoryCommand::class);
5153
}
5254
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Modules\{{ ModuleName }}\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Modules\{{ ModuleName }}\Models\{{ ResourceName }};
7+
8+
class {{ ResourceName }}Factory extends Factory
9+
{
10+
protected $model = {{ ResourceName }}::class;
11+
12+
public function definition(): array
13+
{
14+
//$name = $this->faker->unique()->sentence(4);
15+
16+
return [
17+
//'name' => $name,
18+
19+
//'created_at' => $this->faker->dateTimeBetween('-1 year', '-6 month'),
20+
//'updated_at' => $this->faker->dateTimeBetween('-5 month', 'now'),
21+
];
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Illuminate\Filesystem\Filesystem;
4+
5+
beforeEach(function () {
6+
$this->artisan('modular:make-module ModuleName');
7+
});
8+
9+
afterEach(function () {
10+
(new Filesystem)->deleteDirectory(base_path('modules'));
11+
});
12+
13+
it('can run modular:make-factory command', function () {
14+
$this->artisan('modular:make-factory ModuleName FactoryName')->assertSuccessful();
15+
});
16+
17+
it('can generate a factory', function () {
18+
$this->artisan('modular:make-factory ModuleName FactoryName');
19+
20+
$factory = base_path('modules/ModuleName/Database/Factories/FactoryNameFactory.php');
21+
$this->assertTrue(file_exists($factory));
22+
23+
$factoryContent = file_get_contents($factory);
24+
expect($factoryContent)->toContain('namespace Modules\ModuleName\Database\Factories;');
25+
expect($factoryContent)->toContain('class FactoryNameFactory');
26+
});

0 commit comments

Comments
 (0)