Skip to content

Add support for adding custom traits to models #746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@ protected function addTraits(Model $model, $stub): string
$traits[] = 'HasUuids';
}

if ($model->usesCustomTraits()) {
foreach ($model->customTraits() as $trait) {
$this->addImport($model, $trait);
$traits[] = Str::afterLast($trait, '\\');
}
}

sort($traits);

return Str::replaceFirst('use HasFactory', 'use ' . implode(', ', $traits), $stub);
Expand Down
8 changes: 8 additions & 0 deletions src/Lexers/ModelLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ private function buildModel(string $name, array $columns): Model
unset($columns['relationships']);
}

if (isset($columns['traits'])) {
foreach (explode(' ', $columns['traits']) as $trait) {
$model->addCustomTrait(trim($trait));
}

unset($columns['traits']);
}

if (isset($columns['indexes'])) {
foreach ($columns['indexes'] as $index) {
$model->addIndex(new Index(key($index), array_map('trim', explode(',', current($index)))));
Expand Down
17 changes: 17 additions & 0 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Model implements BlueprintModel

private array $indexes = [];

private array $customTraits = [];

public function __construct($name)
{
$this->name = class_basename($name);
Expand Down Expand Up @@ -111,6 +113,11 @@ public function usesUuids(): bool
return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'uuid';
}

public function usesCustomTraits(): bool
{
return count($this->customTraits) > 0;
}

public function idType(): ?string
{
if (!$this->usesPrimaryKey()) {
Expand Down Expand Up @@ -252,6 +259,16 @@ public function addIndex(Index $index): void
$this->indexes[] = $index;
}

public function customTraits(): array
{
return $this->customTraits;
}

public function addCustomTrait(string $trait): void
{
$this->customTraits[] = $trait;
}

public function pivotTables(): array
{
return $this->pivotTables;
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function output_generates_models($definition, $path, $model): void
$this->filesystem->expects('exists')
->with(dirname($path))
->andReturnTrue();

$this->filesystem->expects('put')
->with($path, $this->fixture($model));

Expand Down Expand Up @@ -648,6 +649,7 @@ public static function modelTreeDataProvider(): array
['drafts/infer-belongsto.yaml', 'app/Models/Conference.php', 'models/infer-belongsto.php'],
['drafts/model-with-ulid-id.yaml', 'app/Models/User.php', 'models/model-with-ulid-trait.php'],
['drafts/model-with-uuid-id.yaml', 'app/Models/User.php', 'models/model-with-uuid-trait.php'],
['drafts/model-with-traits.yaml', 'app/Models/User.php', 'models/model-with-traits.php'],
];
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Feature/Lexers/ModelLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function it_returns_models(): void
],
'ModelThree' => [
'id' => 'increments',
'traits' => 'Spatie\Permission\Traits\HasRoles',
],
],
];
Expand All @@ -55,6 +56,7 @@ public function it_returns_models(): void
$this->assertEquals('ModelOne', $model->name());
$this->assertTrue($model->usesTimestamps());
$this->assertFalse($model->usesSoftDeletes());
$this->assertFalse($model->usesCustomTraits());

$columns = $model->columns();
$this->assertCount(2, $columns);
Expand All @@ -69,6 +71,8 @@ public function it_returns_models(): void
$this->assertEquals('ModelTwo', $model->name());
$this->assertTrue($model->usesTimestamps());
$this->assertFalse($model->usesSoftDeletes());
$this->assertFalse($model->usesCustomTraits());


$columns = $model->columns();
$this->assertCount(2, $columns);
Expand All @@ -83,6 +87,7 @@ public function it_returns_models(): void
$this->assertEquals('ModelThree', $model->name());
$this->assertTrue($model->usesTimestamps());
$this->assertFalse($model->usesSoftDeletes());
$this->assertTrue($model->usesCustomTraits());

$columns = $model->columns();
$this->assertCount(1, $columns);
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/drafts/model-with-traits.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
models:
User:
traits:
- Spatie\Permission\Traits\HasRoles
28 changes: 28 additions & 0 deletions tests/fixtures/models/model-with-traits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Permission\Traits\HasRoles;

class User extends Model
{
use HasFactory, HasRoles;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [];

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