Skip to content

Commit 9442303

Browse files
committed
Change from Loader (load only) to Storage (load and save)
1 parent e786a96 commit 9442303

16 files changed

+162
-41
lines changed

config/workflow-storage.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

33
return [
4-
'databaseLoader' => [
4+
'databaseStorage' => [
55
'tableNames' => [
66
'workflows' => 'workflows',
77
'workflow_states' => 'workflow_states',
88
'workflow_transitions' => 'workflow_transitions',
99
'workflow_state_transitions' => 'workflow_state_transitions',
1010
],
11-
//'loaderClass' => \Soap\WorkflowStorage\DatabaseLoader::class,
11+
'loaderClass' => \Soap\WorkflowStorage\DatabaseStorage::class,
1212
],
1313
];

database/factories/WorkflowFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class WorkflowFactory extends Factory
99
{
1010
protected $model = Workflow::class;
1111

12-
public function definition()
12+
public function definition(): array
1313
{
1414
return [
1515
'name' => $this->faker->name,

src/Contracts/WorkflowLoader.php renamed to src/Contracts/WorkflowDatabaseStorage.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Soap\WorkflowStorage\Contracts;
44

5-
interface WorkflowLoader
5+
interface WorkflowDatabaseStorage extends WorkflowStorage
66
{
7-
public function load(string $workflowName): array;
8-
97
public function getWorkflowTableName(): string;
108

119
public function getWorkflowStateTableName(): string;

src/Contracts/WorkflowStorage.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Soap\WorkflowStorage\Contracts;
4+
5+
interface WorkflowStorage
6+
{
7+
public function all(): array;
8+
9+
public function load(string $workflowName): array;
10+
}

src/DatabaseLoader.php renamed to src/DatabaseStorage.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Soap\WorkflowStorage;
44

5-
use Soap\WorkflowStorage\Contracts\WorkflowLoader;
6-
use Soap\WorkflowStorage\Models\Workflow;
5+
use Soap\WorkflowStorage\Contracts\WorkflowDatabaseStorage;
76

8-
class DatabaseLoader implements WorkflowLoader
7+
class DatabaseStorage implements WorkflowDatabaseStorage
98
{
109
const KEY_TABLENAMES = 'tableNames';
1110

@@ -51,9 +50,13 @@ public function getWorkflowStateTransitionTableName(): string
5150

5251
public function load(string $workflowName): array
5352
{
54-
$workflow = Workflow::with(['states', 'transitions'])->where('name', $workflowName)->first();
55-
$config[$workflowName] = [];
53+
$workflowConfig = [];
5654

57-
return $config;
55+
return $workflowConfig;
56+
}
57+
58+
public function all(): array
59+
{
60+
return [];
5861
}
5962
}

src/Models/Workflow.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\HasMany;
8-
use Soap\WorkflowStorage\DatabaseLoader;
8+
use Soap\WorkflowStorage\DatabaseStorage;
99
use Soap\WorkflowStorage\Enums\WorkflowTypeEnum;
1010

1111
class Workflow extends Model
@@ -22,7 +22,7 @@ class Workflow extends Model
2222

2323
public function getTable(): string
2424
{
25-
return app(DatabaseLoader::class)->getWorkflowTableName();
25+
return app(DatabaseStorage::class)->getWorkflowTableName();
2626
}
2727

2828
public function states(): HasMany

src/Models/WorkflowState.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8-
use Soap\WorkflowStorage\DatabaseLoader;
8+
use Soap\WorkflowStorage\DatabaseStorage;
99

1010
class WorkflowState extends Model
1111
{
@@ -21,7 +21,7 @@ class WorkflowState extends Model
2121

2222
public function getTable(): string
2323
{
24-
return app(DatabaseLoader::class)->getWorkflowStateTableName();
24+
return app(DatabaseStorage::class)->getWorkflowStateTableName();
2525
}
2626

2727
public function workflow(): BelongsTo

src/Models/WorkflowStateTransition.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8-
use Soap\WorkflowStorage\DatabaseLoader;
8+
use Soap\WorkflowStorage\DatabaseStorage;
99

1010
class WorkflowStateTransition extends Model
1111
{
@@ -19,7 +19,7 @@ class WorkflowStateTransition extends Model
1919

2020
public function getTable(): string
2121
{
22-
return app(DatabaseLoader::class)->getWorkflowStateTransitionTableName();
22+
return app(DatabaseStorage::class)->getWorkflowStateTransitionTableName();
2323
}
2424

2525
public function transition(): BelongsTo
@@ -31,9 +31,4 @@ public function fromState(): BelongsTo
3131
{
3232
return $this->belongsTo(WorkflowState::class, 'from_state_id');
3333
}
34-
35-
public function toState(): BelongsTo
36-
{
37-
return $this->belongsTo(WorkflowState::class, 'to_state_id');
38-
}
3934
}

src/Models/WorkflowTransition.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
88
use Illuminate\Database\Eloquent\Relations\HasMany;
9-
use Soap\WorkflowStorage\DatabaseLoader;
9+
use Soap\WorkflowStorage\DatabaseStorage;
1010

1111
class WorkflowTransition extends Model
1212
{
@@ -20,7 +20,7 @@ class WorkflowTransition extends Model
2020

2121
public function getTable(): string
2222
{
23-
return app(DatabaseLoader::class)->getWorkflowTransitionTableName();
23+
return app(DatabaseStorage::class)->getWorkflowTransitionTableName();
2424
}
2525

2626
public function workflow(): BelongsTo

src/Repositories/WorkflowRepository.php

+36-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,46 @@
66

77
class WorkflowRepository
88
{
9-
public function all()
9+
protected $model;
10+
11+
public function __construct(Workflow $model)
1012
{
11-
return [];
13+
$this->model = $model;
1214
}
1315

1416
public function find($id)
1517
{
16-
//$workflow = Workflow::with(['transitions.stateTransitions', 'states'])->find($id);
18+
$workflow = $this->model->with(['transitions', 'states'])->find($id);
19+
$places = $workflow->states->map(function ($state) {
20+
return $state->name;
21+
})->toArray();
22+
$transitions = $workflow->transitions->map(function ($transition) {
23+
return [
24+
$transition->name => [
25+
'from' => $transition->fromStates->map(function ($transitionState) {
26+
return $transitionState->fromState->name;
27+
})->toArray(),
28+
'to' => $transition->toState->name,
29+
],
30+
];
31+
})->toArray();
32+
33+
return [
34+
$workflow->name => [
35+
'supports' => $workflow->supports,
36+
'places' => $places,
37+
'transitions' => $transitions,
38+
],
39+
];
40+
}
41+
42+
public function findByName(string $name): array
43+
{
44+
$workflowId = $this->model->where('name', $name)->first()->id;
45+
if (! $workflowId) {
46+
return [];
47+
}
48+
49+
return $this->find($workflowId);
1750
}
1851
}

src/WorkflowStorage.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33
namespace Soap\WorkflowStorage;
44

5-
use Soap\WorkflowStorage\Contracts\WorkflowLoader;
5+
use Soap\WorkflowStorage\Contracts\WorkflowStorage as WorkflowStorageContract;
66

77
class WorkflowStorage
88
{
99
protected array $loaders = [];
1010

11-
public function __construct(?WorkflowLoader $loader)
11+
public function __construct(?WorkflowStorageContract $loader)
1212
{
1313
if ($loader) {
1414
$this->registerLoader($loader);
1515
}
1616
}
1717

18-
public function registerLoader(WorkflowLoader $loader)
18+
public function registerLoader(WorkflowStorageContract $loader)
1919
{
2020
$this->loaders[$loader::class] = $loader;
2121
}
2222

23-
public function getLoader(string $loader): WorkflowLoader
23+
public function getLoader(string $loader): WorkflowStorageContract
2424
{
2525
return $this->loaders[$loader];
2626
}

src/WorkflowStorageServiceProvider.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public function configurePackage(Package $package): void
3131
public function packageRegistered()
3232
{
3333
$this->app->singleton('workflow-storage', function ($app) {
34-
$workflowStorage = new WorkflowStorage($app->make(DatabaseLoader::class));
34+
$workflowStorage = new WorkflowStorage($app->make(DatabaseStorage::class));
3535
});
3636

37-
$this->app->singleton(DatabaseLoader::class, function ($app) {
38-
$config = $app->make('config')->get('workflow-storage.databaseLoader', []);
37+
$this->app->singleton(DatabaseStorage::class, function ($app) {
38+
$config = $app->make('config')->get('workflow-storage.databaseStorage', []);
3939

40-
return new DatabaseLoader(config: $config);
40+
return new DatabaseStorage(config: $config);
4141
});
4242

4343
}

tests/Feature/WorkflowStorageTest.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22

3+
use Illuminate\Support\Arr;
34
use Soap\WorkflowStorage\Models\Workflow;
5+
use Soap\WorkflowStorage\Repositories\WorkflowRepository;
46

57
beforeEach(function () {
68

79
$workflow = Workflow::create([
8-
'name' => 'Test Workflow',
10+
'name' => 'test_workflow',
911
'type' => 'workflow',
1012
'description' => 'Test Workflow Description',
11-
'supports' => [],
13+
'supports' => ['App\Models\Article'],
1214
'metadata' => [],
1315
]);
1416

@@ -43,17 +45,26 @@
4345
]);
4446
});
4547

46-
test('data can be retrieved from the database', function () {
48+
test('workflow data can be retrieved via models from the database', function () {
4749
$workflow = Workflow::first();
4850
$states = $workflow->states;
4951
$transitions = $workflow->transitions;
5052

51-
expect($workflow->name)->toBe('Test Workflow');
53+
expect($workflow->name)->toBe('test_workflow');
5254
expect($workflow->type->value)->toBe('workflow');
5355
expect($workflow->description)->toBe('Test Workflow Description');
54-
expect($workflow->supports)->toBe([]);
56+
expect($workflow->supports)->toBe(['App\Models\Article']);
5557
expect($workflow->metadata)->toBe([]);
5658

5759
expect($states->count())->toBe(4);
5860
expect($transitions->count())->toBe(1);
5961
});
62+
63+
test('workflow configuration can be retrievd via the repository', function () {
64+
$repo = app()->make(WorkflowRepository::class);
65+
$config = $repo->find(1);
66+
ray($config);
67+
expect(count($config))->toBe(1);
68+
expect(count(Arr::get($config, 'test_workflow.places')))->toBe(4);
69+
expect(count(Arr::get($config, 'test_workflow.transitions')))->toBe(1);
70+
});

workbench/app/Models/Article.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Workbench\App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7+
use Workbench\AppModels\User;
8+
use ZeroDaHero\LaravelWorkflow\Traits\WorkflowTrait;
9+
10+
class Order extends Model
11+
{
12+
protected $guarded = ['id'];
13+
14+
use WorkflowTrait;
15+
16+
public function user(): BelongsTo
17+
{
18+
return $this->belongsTo(User::class);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Workbench\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
class ArticleFactory extends Factory
8+
{
9+
protected $model = Order::class;
10+
11+
public function definition()
12+
{
13+
return [
14+
'title' => $this->faker->sentence(),
15+
];
16+
}
17+
18+
public function forUser(int|User $user)
19+
{
20+
$userId = is_object($user) ? $user->id : $user;
21+
22+
return $this->state(function (array $attributes) {
23+
return [
24+
'user_id' => $userId,
25+
];
26+
});
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('articles', function (Blueprint $table) {
12+
$table->id();
13+
$table->string('title');
14+
$table->string('state')->default('draft');
15+
$table->string('intro_image')->nullable();
16+
$table->string('content_image')->nullable();
17+
$table->text('intro_text')->nullable();
18+
$table->text('content')->nullable();
19+
$table->unsignedBigInteger('user_id')->nullable();
20+
$table->timestamps();
21+
});
22+
}
23+
};

0 commit comments

Comments
 (0)