Browse Source

Merge pull request #31 from nafiesl/1x_publish_stub_files

[1.x] Stub Customization for Laravel 5.8.x to 7.x
tags/1.6.0 1.6.0
Nafies Luthfi 5 years ago
committed by GitHub
parent
commit
ae4879f7b7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      readme.md
  2. 6
      src/Generators/BaseGenerator.php
  3. 4
      src/ServiceProvider.php
  4. 42
      tests/Generators/ModelFactoryGeneratorTest.php
  5. 25
      tests/stubs/database/factories/model-factory.stub

14
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 [
<br>
## 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.
<br>
## Attention
- The package will creates the **Model** class file, the command will stop if the **Model already exists**.

6
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');
}

4
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');
}
}

42
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 = "<?php
declare(strict_types = 1);
namespace Database\Factories;
use App\Models\User;
use {$this->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'));
}
}

25
tests/stubs/database/factories/model-factory.stub

@ -0,0 +1,25 @@
<?php
declare(strict_types = 1);
namespace Database\Factories;
use App\Models\User;
use fullMstr;
use Illuminate\Database\Eloquent\Factories\Factory;
class MasterFactory extends Factory
{
protected $model = Master::class;
public function definition()
{
return [
'title' => $this->faker->word,
'description' => $this->faker->sentence,
'creator_id' => function () {
return User::factory()->create()->id;
},
];
}
}
Loading…
Cancel
Save