diff --git a/config/blueprint.php b/config/blueprint.php index 3fbe5ad9..47ed467a 100644 --- a/config/blueprint.php +++ b/config/blueprint.php @@ -69,4 +69,15 @@ 'use_constraints' => false, 'fake_nullables' => true, + + /* + |-------------------------------------------------------------------------- + | Models + |-------------------------------------------------------------------------- + | + | Here you may choose to use $guarded instead $fillable. + | + */ + + 'use_guarded' => false, ]; diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 75e274e8..6c93c940 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -92,11 +92,15 @@ private function buildProperties(Model $model) { $properties = ''; - $columns = $this->fillableColumns($model->columns()); - if (!empty($columns)) { - $properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model/fillable.stub')); + if (config('blueprint.use_guarded')) { + $properties .= $this->files->stub('model/guarded.stub'); } else { - $properties .= $this->files->stub('model/fillable.stub'); + $columns = $this->fillableColumns($model->columns()); + if (!empty($columns)) { + $properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model/fillable.stub')); + } else { + $properties .= $this->files->stub('model/fillable.stub'); + } } $columns = $this->hiddenColumns($model->columns()); diff --git a/stubs/model/guarded.stub b/stubs/model/guarded.stub new file mode 100644 index 00000000..3822ab1f --- /dev/null +++ b/stubs/model/guarded.stub @@ -0,0 +1,6 @@ + /** + * The attributes that aren't mass assignable. + * + * @var array + */ + protected $guarded = []; diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index 97a60c47..a79a8a50 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -197,6 +197,7 @@ public function output_generates_phpdoc_for_model($definition, $path, $model) $this->files->expects('exists') ->with(dirname($path)) ->andReturnTrue(); + $this->files->expects('put') ->with($path, $this->fixture($model)); @@ -206,6 +207,42 @@ public function output_generates_phpdoc_for_model($definition, $path, $model) $this->assertEquals(['created' => [$path]], $this->subject->output($tree)); } + /** + * @test + */ + public function output_generates_models_with_guarded_property_when_config_option_is_set() + { + $this->app['config']->set('blueprint.use_guarded', true); + + $this->files->expects('stub') + ->with('model/class.stub') + ->andReturn(file_get_contents('stubs/model/class.stub')); + + $this->files->expects('stub') + ->with('model/guarded.stub') + ->andReturn(file_get_contents('stubs/model/guarded.stub')); + + $this->files->expects('stub') + ->with('model/casts.stub') + ->andReturn(file_get_contents('stubs/model/casts.stub')); + + $this->files->shouldReceive('stub') + ->with('model/method.stub') + ->andReturn(file_get_contents('stubs/model/method.stub')); + + $this->files->expects('exists') + ->with(dirname('app/Comment.php')) + ->andReturnTrue(); + + $this->files->expects('put') + ->with('app/Comment.php', $this->fixture('models/model-guarded.php')); + + $tokens = $this->blueprint->parse($this->fixture('definitions/model-guarded.bp')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => ['app/Comment.php']], $this->subject->output($tree)); + } + public function modelTreeDataProvider() { return [ diff --git a/tests/fixtures/definitions/model-guarded.bp b/tests/fixtures/definitions/model-guarded.bp new file mode 100644 index 00000000..d89c2865 --- /dev/null +++ b/tests/fixtures/definitions/model-guarded.bp @@ -0,0 +1,3 @@ +models: + Comment: + content: text \ No newline at end of file diff --git a/tests/fixtures/models/model-guarded.php b/tests/fixtures/models/model-guarded.php new file mode 100644 index 00000000..58a9d7b0 --- /dev/null +++ b/tests/fixtures/models/model-guarded.php @@ -0,0 +1,24 @@ + 'integer', + ]; +}