From 67c89cd81ba4d3c732bf06d20a1c7efe732f5b37 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Jan 2021 15:01:57 +0800 Subject: [PATCH 1/6] Add stubs publisher script and --- src/ServiceProvider.php | 4 ++++ 1 file changed, 4 insertions(+) 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'); } } From e3f921a4d0d2415ed6fd695ebd5b1bf8cbe51bac Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Jan 2021 15:02:11 +0800 Subject: [PATCH 2/6] Use published stub file if it exists --- src/Generators/BaseGenerator.php | 6 ++++++ 1 file changed, 6 insertions(+) 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'); } From 22f2bd6c166d3597f1e2ff9be161cf493dfde5b6 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 27 Jan 2021 05:59:03 +0800 Subject: [PATCH 3/6] Add test for published stubs usage --- tests/Generators/ModelFactoryGeneratorTest.php | 42 +++++++++++++++++++++++ tests/stubs/database/factories/model-factory.stub | 25 ++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 tests/stubs/database/factories/model-factory.stub diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php index 377fffd..0d2fa30 100644 --- a/tests/Generators/ModelFactoryGeneratorTest.php +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -39,4 +39,46 @@ class {$this->model_name}Factory extends Factory "; $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; + }, + ]; + } +} From e386b3e78f33a1a6cf5db255e8c02b53a669cd36 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 27 Jan 2021 06:05:14 +0800 Subject: [PATCH 4/6] Add @param on on the controller index method's docblock --- src/stubs/controllers/api.stub | 1 + src/stubs/controllers/full-formrequests.stub | 1 + src/stubs/controllers/full.stub | 1 + src/stubs/controllers/simple.stub | 1 + tests/CommandOptions/FullCrudFormRequestOptionsTest.php | 1 + tests/Generators/Api/ApiControllerGeneratorTest.php | 1 + tests/Generators/FullControllerGeneratorTest.php | 3 +++ tests/Generators/Simple/SimpleControllerGeneratorTest.php | 3 +++ 8 files changed, 12 insertions(+) diff --git a/src/stubs/controllers/api.stub b/src/stubs/controllers/api.stub index 7fce7b5..a94091e 100644 --- a/src/stubs/controllers/api.stub +++ b/src/stubs/controllers/api.stub @@ -11,6 +11,7 @@ class MasterController extends Controller /** * Get a listing of the singleMstr. * + * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function index(Request $request) diff --git a/src/stubs/controllers/full-formrequests.stub b/src/stubs/controllers/full-formrequests.stub index 2efb58e..63262ed 100644 --- a/src/stubs/controllers/full-formrequests.stub +++ b/src/stubs/controllers/full-formrequests.stub @@ -12,6 +12,7 @@ class MasterController extends Controller /** * Display a listing of the singleMstr. * + * @param \Illuminate\Http\Request $request * @return \Illuminate\View\View */ public function index(Request $request) diff --git a/src/stubs/controllers/full.stub b/src/stubs/controllers/full.stub index c108603..c3aaecc 100644 --- a/src/stubs/controllers/full.stub +++ b/src/stubs/controllers/full.stub @@ -10,6 +10,7 @@ class MasterController extends Controller /** * Display a listing of the singleMstr. * + * @param \Illuminate\Http\Request $request * @return \Illuminate\View\View */ public function index(Request $request) diff --git a/src/stubs/controllers/simple.stub b/src/stubs/controllers/simple.stub index f5ff38d..9033bd3 100644 --- a/src/stubs/controllers/simple.stub +++ b/src/stubs/controllers/simple.stub @@ -10,6 +10,7 @@ class MasterController extends Controller /** * Display a listing of the singleMstr. * + * @param \Illuminate\Http\Request $request * @return \Illuminate\View\View */ public function index(Request $request) diff --git a/tests/CommandOptions/FullCrudFormRequestOptionsTest.php b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php index 874f6b3..cbd5d37 100644 --- a/tests/CommandOptions/FullCrudFormRequestOptionsTest.php +++ b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php @@ -57,6 +57,7 @@ class {$this->model_name}Controller extends Controller /** * Display a listing of the {$this->single_model_var_name}. * + * @param \Illuminate\Http\Request \$request * @return \Illuminate\View\View */ public function index(Request \$request) diff --git a/tests/Generators/Api/ApiControllerGeneratorTest.php b/tests/Generators/Api/ApiControllerGeneratorTest.php index 432c4a9..5eedaad 100644 --- a/tests/Generators/Api/ApiControllerGeneratorTest.php +++ b/tests/Generators/Api/ApiControllerGeneratorTest.php @@ -25,6 +25,7 @@ class {$this->model_name}Controller extends Controller /** * Get a listing of the {$this->single_model_var_name}. * + * @param \Illuminate\Http\Request \$request * @return \Illuminate\Http\JsonResponse */ public function index(Request \$request) diff --git a/tests/Generators/FullControllerGeneratorTest.php b/tests/Generators/FullControllerGeneratorTest.php index 29974f5..76143fb 100644 --- a/tests/Generators/FullControllerGeneratorTest.php +++ b/tests/Generators/FullControllerGeneratorTest.php @@ -24,6 +24,7 @@ class {$this->model_name}Controller extends Controller /** * Display a listing of the {$this->single_model_var_name}. * + * @param \Illuminate\Http\Request \$request * @return \Illuminate\View\View */ public function index(Request \$request) @@ -155,6 +156,7 @@ class CategoryController extends Controller /** * Display a listing of the category. * + * @param \Illuminate\Http\Request \$request * @return \Illuminate\View\View */ public function index(Request \$request) @@ -287,6 +289,7 @@ class CategoryController extends Controller /** * Display a listing of the category. * + * @param \Illuminate\Http\Request \$request * @return \Illuminate\View\View */ public function index(Request \$request) diff --git a/tests/Generators/Simple/SimpleControllerGeneratorTest.php b/tests/Generators/Simple/SimpleControllerGeneratorTest.php index 843441c..6fedd5b 100644 --- a/tests/Generators/Simple/SimpleControllerGeneratorTest.php +++ b/tests/Generators/Simple/SimpleControllerGeneratorTest.php @@ -24,6 +24,7 @@ class {$this->model_name}Controller extends Controller /** * Display a listing of the {$this->single_model_var_name}. * + * @param \Illuminate\Http\Request \$request * @return \Illuminate\View\View */ public function index(Request \$request) @@ -128,6 +129,7 @@ class CategoryController extends Controller /** * Display a listing of the category. * + * @param \Illuminate\Http\Request \$request * @return \Illuminate\View\View */ public function index(Request \$request) @@ -233,6 +235,7 @@ class CategoryController extends Controller /** * Display a listing of the category. * + * @param \Illuminate\Http\Request \$request * @return \Illuminate\View\View */ public function index(Request \$request) From 4031b0b0052d08fff699dba01dd346db0ff28c41 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 27 Jan 2021 10:59:34 +0800 Subject: [PATCH 5/6] Update readme.md for for publishing stub files --- readme.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/readme.md b/readme.md index 78232ce..690bba9 100644 --- a/readme.md +++ b/readme.md @@ -254,6 +254,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**. From 2f459ec06147bc6dab65d458892ae1ebd03154e7 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 27 Jan 2021 11:02:17 +0800 Subject: [PATCH 6/6] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 690bba9..6ab722a 100644 --- a/readme.md +++ b/readme.md @@ -230,7 +230,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.