Skip to content

Commit 2ea2bb1

Browse files
committed
add type column
1 parent fc0b774 commit 2ea2bb1

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

database/migrations/create_workflows_table.php.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ return new class extends Migration
1111
Schema::create('workflows', function (Blueprint $table) {
1212
$table->id();
1313

14+
// definition class
15+
$table->string('type')->index();
16+
1417
// serialized class
1518
$table->longText('definition');
1619

src/Concerns/HasWorkflows.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Elegantly\Workflow\Concerns;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\MorphMany;
7+
8+
trait HasWorkflows
9+
{
10+
/**
11+
* @return MorphMany<Workflow, Model>
12+
*/
13+
public function workflows(): MorphMany
14+
{
15+
return $this->morphMany(Workflow::class, 'model');
16+
}
17+
}

src/Models/Workflow.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
/**
1717
* @property int $id
18+
* @property string $type Definition class
1819
* @property ?string $step
1920
* @property WorkflowDefinition $definition Serialized Workflow definition
2021
* @property Collection<int, WorkflowItem> $items
@@ -69,12 +70,12 @@ public function model(): MorphTo
6970
}
7071

7172
/**
72-
* @return Attribute<WorkflowDefinition, WorkflowDefinition|string>
73+
* @return Attribute<WorkflowDefinition, WorkflowDefinition>
7374
*/
7475
protected function definition(): Attribute
7576
{
7677
return Attribute::make(
77-
get: function ($value) {
78+
get: function (mixed $value) {
7879
if (is_string($value)) {
7980
return unserialize($value);
8081
}
@@ -84,15 +85,11 @@ protected function definition(): Attribute
8485

8586
return null;
8687
},
87-
set: function ($value) {
88-
if (is_string($value)) {
89-
return $value;
90-
}
91-
if ($value instanceof WorkflowDefinition) {
92-
return serialize($value);
93-
}
94-
95-
return null;
88+
set: function (WorkflowDefinition $value) {
89+
return [
90+
'definition' => serialize($value),
91+
'type' => $value::class,
92+
];
9693
},
9794
);
9895
}

tests/Feature/WorkflowDefinitionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
$workflow = $definition->start();
1111

12+
expect($workflow->type)->toBe(TestWorkflowDefinition::class);
13+
expect($workflow->definition)->toBeInstanceOf(TestWorkflowDefinition::class);
1214
expect($workflow->exists)->toBe(true);
1315
expect($workflow->items)->toHaveLength(2);
1416

0 commit comments

Comments
 (0)