Skip to content

Commit 52aab2f

Browse files
committed
Prepare Service Provider classes and Dependency Injection
1 parent 54a1388 commit 52aab2f

File tree

11 files changed

+136
-33
lines changed

11 files changed

+136
-33
lines changed

database/migrations/wf4_create_workflow_state_transitions_table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public function up()
1010
{
1111
Schema::create('workflow_state_transitions', function (Blueprint $table) {
1212
$table->id();
13-
$table->foreignId('transition_id')->constrained('workflow_transitions')->onDelete('cascade');
13+
$table->foreignId('workflow_transition_id')->constrained('workflow_transitions')->onDelete('cascade');
1414
$table->foreignId('from_state_id')->constrained('workflow_states')->onDelete('cascade');
15-
$table->unique(['transition_id', 'from_state_id'], 'wf_transition_from_state_unique');
15+
$table->unique(['workflow_transition_id', 'from_state_id'], 'wf_transition_from_state_unique');
1616
$table->timestamps();
1717
});
1818
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Soap\WorkflowStorage\DatabaseLoader;
7+
8+
class WorkflowServiceProvider extends ServiceProvider
9+
{
10+
public function register()
11+
{
12+
13+
}
14+
15+
public function boot()
16+
{
17+
$registy = app()->make('workflow');
18+
$workflowLoader = app()->make('workflow-storage');
19+
foreach ($wokflowLoader->all() as $workflow => $config) {
20+
$registy->addFromArray($workflow, $config);
21+
}
22+
}
23+
}

src/Facades/WorkflowStorage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class WorkflowStorage extends Facade
1111
{
1212
protected static function getFacadeAccessor(): string
1313
{
14-
return \Soap\WorkflowStorage\WorkflowStorage::class;
14+
return 'workflow-storage';
1515
}
1616
}

src/Models/Workflow.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@ class Workflow extends Model
1212
{
1313
use HasFactory;
1414

15-
protected $fillable = [
16-
'name',
17-
'marking_store_attribute',
18-
'type',
19-
'description',
20-
'supports',
21-
'metadata',
22-
];
15+
protected $guarded = ['id'];
2316

2417
protected $casts = [
2518
'supports' => 'array',

src/Models/WorkflowState.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ class WorkflowState extends Model
1111
{
1212
use HasFactory;
1313

14-
protected $fillable = [
15-
'workflow_id',
16-
'name',
17-
'initial_state',
18-
'final_state',
19-
'metadata',
20-
];
14+
protected $guarded = ['id'];
2115

2216
protected $casts = [
2317
'metadata' => 'array',

src/Models/WorkflowStateTransition.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ class WorkflowStateTransition extends Model
1111
{
1212
use HasFactory;
1313

14-
protected $fillable = [
15-
'transition_id',
16-
'from_state_id',
17-
'metadata',
18-
];
14+
protected $guarded = ['id'];
1915

2016
protected $casts = [
2117
'metadata' => 'array',

src/Models/WorkflowTransition.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ class WorkflowTransition extends Model
1212
{
1313
use HasFactory;
1414

15-
protected $fillable = [
16-
'workflow_id',
17-
'from_state_id',
18-
'to_state_id',
19-
'metadata',
20-
];
15+
protected $guarded = ['id'];
2116

2217
protected $casts = [
2318
'metadata' => 'array',
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Soap\WorkflowStorage\Repositories;
4+
5+
use Soap\WorkflowStorage\Models\Workflow;
6+
7+
class WorkflowRepository
8+
{
9+
public function all()
10+
{
11+
return [];
12+
}
13+
14+
public function find($id)
15+
{
16+
//$workflow = Workflow::with(['transitions.stateTransitions', 'states'])->find($id);
17+
}
18+
}

src/WorkflowStorage.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,31 @@ class WorkflowStorage
88
{
99
protected array $loaders = [];
1010

11+
public function __construct(?WorkflowLoader $loader)
12+
{
13+
if ($loader) {
14+
$this->registerLoader($loader);
15+
}
16+
}
17+
1118
public function registerLoader(WorkflowLoader $loader)
1219
{
1320
$this->loaders[$loader::class] = $loader;
1421
}
22+
23+
public function getLoader(string $loader): WorkflowLoader
24+
{
25+
return $this->loaders[$loader];
26+
}
27+
28+
public function all(): array
29+
{
30+
if (count($this->loaders) === 0) {
31+
return [];
32+
}
33+
34+
return collect($this->loaders)->mapWithKeys(function ($loader) {
35+
return [$loader::class => $loader->all()];
36+
})->toArray();
37+
}
1538
}

src/WorkflowStorageServiceProvider.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ public function configurePackage(Package $package): void
2424
'wf3_create_workflow_transitions_table',
2525
'wf4_create_workflow_state_transitions_table',
2626
])
27-
->hasCommand(WorkflowStorageListCommand::class);
27+
->hasCommand(WorkflowStorageListCommand::class)
28+
->publishesServiceProvider('WorkflowServiceProvider');
2829
}
2930

3031
public function packageRegistered()
3132
{
33+
$this->app->singleton('workflow-storage', function ($app) {
34+
$workflowStorage = new WorkflowStorage($app->make(DatabaseLoader::class));
35+
});
36+
3237
$this->app->singleton(DatabaseLoader::class, function ($app) {
3338
$config = $app->make('config')->get('workflow-storage.databaseLoader', []);
3439

3540
return new DatabaseLoader(config: $config);
3641
});
3742

38-
$this->app->singleton(WorkflowStorage::class, function ($app) {
39-
return new WorkflowStorage;
40-
});
4143
}
4244
}

0 commit comments

Comments
 (0)