diff --git a/readme.md b/readme.md
index 2c2f449..45c5b0d 100644
--- a/readme.md
+++ b/readme.md
@@ -218,7 +218,7 @@ The generated functional tests will give you examples of how to adapt this code
You can configure your own by publishing the config file:
```bash
-$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider"
+$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider" --tag=config
```
That will generate `config/simple-crud.php` file.
@@ -242,6 +242,18 @@ return [
+## Publishing Stub Files
+
+Stub files is the templates which we use to generate the code for each model classes and files. We can customize the stub files as we needed by publishing them to our project directory.
+
+```bash
+$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider" --tag=stubs
+```
+
+That will generate stub files on `stubs/simple-crud` directory. Now we can change some stub files based on our project needs.
+
+
+
## Attention
- The package will creates the **Model** class file, the command will stop if the **Model already exists**.
diff --git a/src/Generators/BaseGenerator.php b/src/Generators/BaseGenerator.php
index e532ffd..b38591b 100644
--- a/src/Generators/BaseGenerator.php
+++ b/src/Generators/BaseGenerator.php
@@ -116,6 +116,12 @@ abstract class BaseGenerator implements GeneratorContract
*/
protected function getStubFileContent(string $stubName)
{
+ $publishedStubPath = base_path('stubs/simple-crud/'.$stubName.'.stub');
+
+ if (is_file($publishedStubPath)) {
+ return $this->files->get($publishedStubPath);
+ }
+
return $this->files->get(__DIR__.'/../stubs/'.$stubName.'.stub');
}
diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php
index 9b78cc8..c304afc 100644
--- a/src/ServiceProvider.php
+++ b/src/ServiceProvider.php
@@ -27,5 +27,9 @@ class ServiceProvider extends BaseServiceProvider
$this->publishes([
__DIR__.'/config.php' => config_path('simple-crud.php'),
], 'config');
+
+ $this->publishes([
+ __DIR__.'/stubs' => base_path('stubs/simple-crud'),
+ ], 'stubs');
}
}
diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php
index d93b37b..0633396 100644
--- a/tests/Generators/ModelFactoryGeneratorTest.php
+++ b/tests/Generators/ModelFactoryGeneratorTest.php
@@ -32,4 +32,46 @@ use Faker\Generator as Faker;
";
$this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath));
}
+
+ /** @test */
+ public function it_creates_model_factory_file_content_from_published_stub()
+ {
+ app('files')->makeDirectory(base_path('stubs/simple-crud/database/factories'), 0777, true, true);
+ app('files')->copy(
+ __DIR__.'/../stubs/database/factories/model-factory.stub',
+ base_path('stubs/simple-crud/database/factories/model-factory.stub')
+ );
+ $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
+
+ $modelFactoryPath = database_path('factories/'.$this->model_name.'Factory.php');
+ $this->assertFileExists($modelFactoryPath);
+ $modelFactoryContent = "full_model_name};
+use Illuminate\Database\Eloquent\Factories\Factory;
+
+class {$this->model_name}Factory extends Factory
+{
+ protected \$model = {$this->model_name}::class;
+
+ public function definition()
+ {
+ return [
+ 'title' => \$this->faker->word,
+ 'description' => \$this->faker->sentence,
+ 'creator_id' => function () {
+ return User::factory()->create()->id;
+ },
+ ];
+ }
+}
+";
+ $this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath));
+ $this->removeFileOrDir(base_path('stubs'));
+ }
}
diff --git a/tests/stubs/database/factories/model-factory.stub b/tests/stubs/database/factories/model-factory.stub
new file mode 100644
index 0000000..23c4374
--- /dev/null
+++ b/tests/stubs/database/factories/model-factory.stub
@@ -0,0 +1,25 @@
+ $this->faker->word,
+ 'description' => $this->faker->sentence,
+ 'creator_id' => function () {
+ return User::factory()->create()->id;
+ },
+ ];
+ }
+}