├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json └── src ├── Console ├── MakeCommand.php └── stubs │ └── make.stub └── FactoryGeneratorServiceProvider.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## v1.0.2 (2017-03-25) 4 | 5 | ## Changed 6 | - Simplify the code who generate a new model factory 7 | 8 | ## v1.0.1 (2017-03-24) 9 | 10 | ### Removed 11 | - Removed unused "require-dev" in composer.json 12 | 13 | ## v1.0.0 (2017-03-24) 14 | 15 | ### Added 16 | - Initial release 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Roni Yusuf Manalu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel 5 Model Factory Generator 2 | 3 | This package offers a lazy way to create a new model factory files, since Laravel (< 5.5) have no Artisan command to generate it. 4 | 5 | ## Installation 6 | First, install this package via the Composer package manager: 7 | ``` 8 | composer require rymanalu/factory-generator 9 | ``` 10 | 11 | Next, you should add the `FactoryGeneratorServiceProvider` to the `providers` array of your `config/app.php` configuration file: 12 | ```php 13 | Rymanalu\FactoryGenerator\FactoryGeneratorServiceProvider::class, 14 | ``` 15 | Now, you should be able to generate a new model factory file by executing `php artisan make:factory` command. 16 | 17 | ## Usage 18 | `php artisan make:factory` accept one argument: the model class name with the namespace. Make sure the model is already exists before executing this command. 19 | 20 | Example: 21 | 22 | ``` 23 | php artisan make:factory "App\Post" 24 | ``` 25 | The command will generate a file named `PostFactory.php` in `/path/to/your-laravel-project/database/factories` directory: 26 | 27 | ```php 28 | define(Post::class, function (Faker $faker) { 34 | // 35 | }); 36 | ``` 37 | This command also using the fillable array of the model and pair all of fillable values to `$faker->word` as default (you can change it to the proper [Faker](https://github.com/fzaninotto/Faker) Formatters or other value later) in the generated model factory. 38 | 39 | For example, if the `App\Post` has fillable array like this: 40 | ```php 41 | define(Post::class, function (Faker $faker) { 67 | return [ 68 | 'text' => $faker->word, 69 | ]; 70 | }); 71 | ``` 72 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rymanalu/factory-generator", 3 | "description": "Laravel 5 Model Factory Generator.", 4 | "keywords": ["laravel", "model", "factory", "model factory", "generator"], 5 | "type": "library", 6 | "require": { 7 | "php": ">=5.5.9", 8 | "illuminate/console": "^5.1", 9 | "illuminate/support": "^5.1" 10 | }, 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Roni Yusuf Manalu", 15 | "email": "rymanalu@gmail.com" 16 | } 17 | ], 18 | "autoload": { 19 | "psr-4": { 20 | "Rymanalu\\FactoryGenerator\\": "src/" 21 | } 22 | }, 23 | "homepage": "https://github.com/rymanalu/factory-generator" 24 | } 25 | -------------------------------------------------------------------------------- /src/Console/MakeCommand.php: -------------------------------------------------------------------------------- 1 | laravel->databasePath().'/factories/'.$name.'Factory.php'; 60 | } 61 | 62 | /** 63 | * Build the class with the given name. 64 | * 65 | * @param string $name 66 | * @return string 67 | */ 68 | protected function buildClass($name) 69 | { 70 | $stub = $this->files->get($this->getStub()); 71 | 72 | return $this->replaceClassAndNamespace($name, $stub)->replaceFields($name, $stub); 73 | } 74 | 75 | /** 76 | * Replace the classname and the namespace for the given stub. 77 | * 78 | * @param string $name 79 | * @param string $stub 80 | * @return \Rymanalu\FactoryGenerator\Console\MakeCommand 81 | */ 82 | protected function replaceClassAndNamespace($name, &$stub) 83 | { 84 | $stub = str_replace( 85 | ['DummyClassWithNamespace', 'DummyClass'], 86 | [$this->argument('name'), $name], 87 | $stub 88 | ); 89 | 90 | return $this; 91 | } 92 | 93 | /** 94 | * Replace the fields of the model factory. 95 | * 96 | * @param string $name 97 | * @param string $stub 98 | * @return string 99 | */ 100 | protected function replaceFields($name, &$stub) 101 | { 102 | if (empty($fillable = $this->getFillable())) { 103 | return str_replace('fields', '//', $stub); 104 | } 105 | 106 | $fields = 'return ['; 107 | 108 | foreach ($fillable as $column) { 109 | $fields .= PHP_EOL." '{$column}' => \$faker->word,"; 110 | } 111 | 112 | return str_replace('fields', $fields.PHP_EOL.' ];', $stub); 113 | } 114 | 115 | /** 116 | * Get the fillable attributes for the classname model. 117 | * 118 | * @return array 119 | */ 120 | protected function getFillable() 121 | { 122 | $model = $this->argument('name'); 123 | 124 | return (new $model)->getFillable(); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/Console/stubs/make.stub: -------------------------------------------------------------------------------- 1 | define(DummyClass::class, function (Faker $faker) { 7 | fields 8 | }); 9 | -------------------------------------------------------------------------------- /src/FactoryGeneratorServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 17 | $this->commands(Console\MakeCommand::class); 18 | } 19 | } 20 | } 21 | --------------------------------------------------------------------------------