Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@
'use_constraints' => false,

'fake_nullables' => true,

/*
|--------------------------------------------------------------------------
| Models
|--------------------------------------------------------------------------
|
| Here you may choose to use $guarded instead $fillable.
|
*/

'use_guarded' => false,
];
12 changes: 8 additions & 4 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 6 additions & 0 deletions stubs/model/guarded.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];
37 changes: 37 additions & 0 deletions tests/Feature/Generator/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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 [
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/definitions/model-guarded.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
models:
Comment:
content: text
24 changes: 24 additions & 0 deletions tests/fixtures/models/model-guarded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];
}