Skip to content

Commit 037f5b9

Browse files
committed
Support adding SoftDeletes trait
1 parent 7e16204 commit 037f5b9

File tree

10 files changed

+128
-19
lines changed

10 files changed

+128
-19
lines changed

src/Blueprint.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class Blueprint
1414

1515
public function parse($content)
1616
{
17-
$content = preg_replace('/^(\s+)(id|timestamps)$/m', '$1$2: $2', $content);
17+
$content = preg_replace_callback('/^(\s+)(id|timestamps|soft[dD]eletes)$/m', function ($matches) {
18+
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
19+
}, $content);
1820

1921
return Yaml::parse($content);
2022
}

src/Generators/ModelGenerator.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ protected function populateStub(string $stub, Model $model)
4141
$stub = str_replace('DummyNamespace', 'App', $stub);
4242
$stub = str_replace('DummyClass', $model->name(), $stub);
4343
$stub = str_replace('// properties...', $this->buildProperties($model), $stub);
44+
$stub = $this->addTraits($model, $stub);
4445

4546
return $stub;
4647
}
@@ -49,19 +50,21 @@ private function buildProperties(Model $model)
4950
{
5051
$properties = '';
5152

52-
$property = $this->fillableColumns($model->columns());
53-
if (!empty($property)) {
54-
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($property, false), $this->propertyStub('fillable'));
53+
$columns = $this->fillableColumns($model->columns());
54+
if (!empty($columns)) {
55+
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->propertyStub('fillable'));
56+
} else {
57+
$properties .= $this->propertyStub('fillable');
5558
}
5659

57-
$property = $this->castableColumns($model->columns());
58-
if (!empty($property)) {
59-
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($property), $this->propertyStub('casts'));
60+
$columns = $this->castableColumns($model->columns());
61+
if (!empty($columns)) {
62+
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns), $this->propertyStub('casts'));
6063
}
6164

62-
$property = $this->dateColumns($model->columns());
63-
if (!empty($property)) {
64-
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($property, false), $this->propertyStub('dates'));
65+
$columns = $this->dateColumns($model->columns());
66+
if (!empty($columns)) {
67+
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->propertyStub('dates'));
6568
}
6669

6770
return trim($properties);
@@ -148,4 +151,16 @@ private function propertyStub(string $stub)
148151

149152
return $stubs[$stub];
150153
}
154+
155+
private function addTraits(Model $model, $stub)
156+
{
157+
if (!$model->usesSoftDeletes()) {
158+
return $stub;
159+
}
160+
161+
$stub = str_replace('use Illuminate\\Database\\Eloquent\\Model;', 'use Illuminate\\Database\\Eloquent\\Model;' . PHP_EOL . 'use Illuminate\\Database\\Eloquent\\SoftDeletes;', $stub);
162+
$stub = str_replace('{', '{' . PHP_EOL . ' use SoftDeletes;' . PHP_EOL, $stub);
163+
164+
return $stub;
165+
}
151166
}

src/Lexers/ModelLexer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ private function buildModel(string $name, array $columns)
110110
unset($columns['timestamps']);
111111
}
112112

113+
if (isset($columns['softdeletes'])) {
114+
$model->enableSoftDeletes();
115+
unset($columns['softdeletes']);
116+
}
117+
113118
if (!isset($columns['id'])) {
114119
$column = $this->buildColumn('id', 'id');
115120
$model->addColumn($column);

src/Model.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Model
88
{
99
private $name;
1010
private $timestamps = true;
11+
private $softDeletes = false;
1112
private $columns = [];
1213

1314
/**
@@ -52,4 +53,14 @@ public function tableName()
5253
{
5354
return Str::snake(Str::pluralStudly($this->name));
5455
}
56+
57+
public function usesSoftDeletes(): bool
58+
{
59+
return $this->softDeletes;
60+
}
61+
62+
public function enableSoftDeletes()
63+
{
64+
$this->softDeletes = true;
65+
}
5566
}

tests/Feature/BlueprintTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function it_parses_shorthands()
7979
$this->assertEquals([
8080
'models' => [
8181
'Name' => [
82+
'softdeletes' => 'softDeletes',
8283
'id' => 'id',
8384
'timestamps' => 'timestamps',
8485
],

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,25 @@ public function output_writes_nothing_for_empty_tree()
4747
*/
4848
public function output_writes_migration_for_model_tree($definition, $path, $model)
4949
{
50+
static $iteration = 0;
51+
5052
$this->files->expects('get')
5153
->with('stubs/model/class.stub')
5254
->andReturn(file_get_contents('stubs/model/class.stub'));
5355

54-
$this->files->expects('get')
55-
->with('stubs/model/fillable.stub')
56-
->andReturn(file_get_contents('stubs/model/fillable.stub'));
56+
if ($iteration === 0) {
57+
$this->files->expects('get')
58+
->with('stubs/model/fillable.stub')
59+
->andReturn(file_get_contents('stubs/model/fillable.stub'));
5760

58-
$this->files->expects('get')
59-
->with('stubs/model/casts.stub')
60-
->andReturn(file_get_contents('stubs/model/casts.stub'));
61+
$this->files->expects('get')
62+
->with('stubs/model/casts.stub')
63+
->andReturn(file_get_contents('stubs/model/casts.stub'));
6164

62-
$this->files->expects('get')
63-
->with('stubs/model/dates.stub')
64-
->andReturn(file_get_contents('stubs/model/dates.stub'));
65+
$this->files->expects('get')
66+
->with('stubs/model/dates.stub')
67+
->andReturn(file_get_contents('stubs/model/dates.stub'));
68+
}
6569

6670
$this->files->expects('put')
6771
->with($path, $this->fixture($model));
@@ -70,13 +74,15 @@ public function output_writes_migration_for_model_tree($definition, $path, $mode
7074
$tree = $this->blueprint->analyze($tokens);
7175

7276
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
77+
++$iteration;
7378
}
7479

7580

7681
public function modelTreeDataProvider()
7782
{
7883
return [
7984
['definitions/readme-example.bp', 'app/Post.php', 'models/readme-example.php'],
85+
['definitions/soft-deletes.bp', 'app/Comment.php', 'models/soft-deletes.php'],
8086
];
8187
}
8288
}

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function it_returns_models()
5353
$model = $actual['models']['ModelOne'];
5454
$this->assertEquals('ModelOne', $model->name());
5555
$this->assertTrue($model->usesTimestamps());
56+
$this->assertFalse($model->usesSoftDeletes());
5657

5758
$columns = $model->columns();
5859
$this->assertCount(2, $columns);
@@ -66,6 +67,7 @@ public function it_returns_models()
6667
$model = $actual['models']['ModelTwo'];
6768
$this->assertEquals('ModelTwo', $model->name());
6869
$this->assertTrue($model->usesTimestamps());
70+
$this->assertFalse($model->usesSoftDeletes());
6971

7072
$columns = $model->columns();
7173
$this->assertCount(2, $columns);
@@ -98,6 +100,7 @@ public function it_defaults_the_id_column()
98100
$model = $actual['models']['Model'];
99101
$this->assertEquals('Model', $model->name());
100102
$this->assertTrue($model->usesTimestamps());
103+
$this->assertFalse($model->usesSoftDeletes());
101104

102105
$columns = $model->columns();
103106
$this->assertCount(2, $columns);
@@ -132,6 +135,7 @@ public function it_disables_timestamps()
132135
$model = $actual['models']['Model'];
133136
$this->assertEquals('Model', $model->name());
134137
$this->assertFalse($model->usesTimestamps());
138+
$this->assertFalse($model->usesSoftDeletes());
135139
}
136140

137141
/**
@@ -155,6 +159,7 @@ public function it_defaults_to_string_datatype()
155159
$model = $actual['models']['Model'];
156160
$this->assertEquals('Model', $model->name());
157161
$this->assertTrue($model->usesTimestamps());
162+
$this->assertFalse($model->usesSoftDeletes());
158163

159164
$columns = $model->columns();
160165
$this->assertCount(2, $columns);
@@ -191,6 +196,7 @@ public function it_accepts_lowercase_keywords()
191196
$model = $actual['models']['Model'];
192197
$this->assertEquals('Model', $model->name());
193198
$this->assertTrue($model->usesTimestamps());
199+
$this->assertFalse($model->usesSoftDeletes());
194200

195201
$columns = $model->columns();
196202
$this->assertCount(4, $columns);
@@ -235,6 +241,7 @@ public function it_handles_data_type_attributes($definition, $data_type, $attrib
235241
$model = $actual['models']['Model'];
236242
$this->assertEquals('Model', $model->name());
237243
$this->assertTrue($model->usesTimestamps());
244+
$this->assertFalse($model->usesSoftDeletes());
238245

239246
$columns = $model->columns();
240247
$this->assertCount(2, $columns);
@@ -269,6 +276,7 @@ public function it_handles_modifier_attributes($definition, $modifier, $attribut
269276
$model = $actual['models']['Model'];
270277
$this->assertEquals('Model', $model->name());
271278
$this->assertTrue($model->usesTimestamps());
279+
$this->assertFalse($model->usesSoftDeletes());
272280

273281
$columns = $model->columns();
274282
$this->assertCount(2, $columns);
@@ -281,6 +289,36 @@ public function it_handles_modifier_attributes($definition, $modifier, $attribut
281289
$this->assertEquals([[$modifier => $attributes], 'nullable'], $columns['column']->modifiers());
282290
}
283291

292+
/**
293+
* @test
294+
*/
295+
public function it_enables_soft_deletes()
296+
{
297+
$tokens = [
298+
'models' => [
299+
'Model' => [
300+
'softdeletes' => 'softdeletes'
301+
]
302+
],
303+
];
304+
305+
$actual = $this->subject->analyze($tokens);
306+
307+
$this->assertIsArray($actual['models']);
308+
$this->assertCount(1, $actual['models']);
309+
310+
$model = $actual['models']['Model'];
311+
$this->assertEquals('Model', $model->name());
312+
$this->assertTrue($model->usesTimestamps());
313+
$this->assertTrue($model->usesSoftDeletes());
314+
315+
$columns = $model->columns();
316+
$this->assertCount(1, $columns);
317+
$this->assertEquals('id', $columns['id']->name());
318+
$this->assertEquals('id', $columns['id']->dataType());
319+
$this->assertEquals([], $columns['id']->modifiers());
320+
}
321+
284322
public function dataTypeAttributesDataProvider()
285323
{
286324
return [
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
models:
22
Name:
3+
softDeletes
34
id
45
timestamps
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
models:
2+
Comment:
3+
softdeletes
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
8+
class Comment extends Model
9+
{
10+
use SoftDeletes;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var array
16+
*/
17+
protected $fillable = [];
18+
19+
/**
20+
* The attributes that should be cast to native types.
21+
*
22+
* @var array
23+
*/
24+
protected $casts = [
25+
'id' => 'integer',
26+
];
27+
}

0 commit comments

Comments
 (0)