Skip to content

Update service providers and dependency injection related to package structure. #3

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

Merged
merged 2 commits into from
Sep 14, 2024
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/soap/laravel-workflow-storage/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/soap/laravel-workflow-storage/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/soap/laravel-workflow-storage.svg?style=flat-square)](https://packagist.org/packages/soap/laravel-workflow-storage)

This package extends [zerodahero/laravel-workflow](https://github.com/zerodahero/laravel-workflow) by adding option to store workflow coniguration in database. Laravel workflow only support loading configuration form Laravel configuration. This package provides user to change workflow configuration without helping from developers.
This package extends [zerodahero/laravel-workflow](https://github.com/zerodahero/laravel-workflow) by adding option to store workflow configuration in database. Laravel workflow only support loading configuration form Laravel configuration. This package provides user to change workflow configuration without helping from developers.

## Support us

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public function up()
{
Schema::create('workflow_state_transitions', function (Blueprint $table) {
$table->id();
$table->foreignId('transition_id')->constrained('workflow_transitions')->onDelete('cascade');
$table->foreignId('workflow_transition_id')->constrained('workflow_transitions')->onDelete('cascade');
$table->foreignId('from_state_id')->constrained('workflow_states')->onDelete('cascade');
$table->unique(['transition_id', 'from_state_id'], 'wf_transition_from_state_unique');
$table->unique(['workflow_transition_id', 'from_state_id'], 'wf_transition_from_state_unique');
$table->timestamps();
});
}
Expand Down
23 changes: 23 additions & 0 deletions resources/views/stubs/WorkflowServiceProvider.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Soap\WorkflowStorage\DatabaseLoader;

class WorkflowServiceProvider extends ServiceProvider
{
public function register()
{

}

public function boot()
{
$registy = app()->make('workflow');
$workflowLoader = app()->make('workflow-storage');
foreach ($wokflowLoader->all() as $workflow => $config) {
$registy->addFromArray($workflow, $config);
}
}
}
2 changes: 1 addition & 1 deletion src/Facades/WorkflowStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class WorkflowStorage extends Facade
{
protected static function getFacadeAccessor(): string
{
return \Soap\WorkflowStorage\WorkflowStorage::class;
return 'workflow-storage';
}
}
9 changes: 1 addition & 8 deletions src/Models/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ class Workflow extends Model
{
use HasFactory;

protected $fillable = [
'name',
'marking_store_attribute',
'type',
'description',
'supports',
'metadata',
];
protected $guarded = ['id'];

protected $casts = [
'supports' => 'array',
Expand Down
8 changes: 1 addition & 7 deletions src/Models/WorkflowState.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ class WorkflowState extends Model
{
use HasFactory;

protected $fillable = [
'workflow_id',
'name',
'initial_state',
'final_state',
'metadata',
];
protected $guarded = ['id'];

protected $casts = [
'metadata' => 'array',
Expand Down
6 changes: 1 addition & 5 deletions src/Models/WorkflowStateTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ class WorkflowStateTransition extends Model
{
use HasFactory;

protected $fillable = [
'transition_id',
'from_state_id',
'metadata',
];
protected $guarded = ['id'];

protected $casts = [
'metadata' => 'array',
Expand Down
7 changes: 1 addition & 6 deletions src/Models/WorkflowTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ class WorkflowTransition extends Model
{
use HasFactory;

protected $fillable = [
'workflow_id',
'from_state_id',
'to_state_id',
'metadata',
];
protected $guarded = ['id'];

protected $casts = [
'metadata' => 'array',
Expand Down
18 changes: 18 additions & 0 deletions src/Repositories/WorkflowRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Soap\WorkflowStorage\Repositories;

use Soap\WorkflowStorage\Models\Workflow;

class WorkflowRepository
{
public function all()
{
return [];
}

public function find($id)
{
//$workflow = Workflow::with(['transitions.stateTransitions', 'states'])->find($id);
}
}
23 changes: 23 additions & 0 deletions src/WorkflowStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,31 @@ class WorkflowStorage
{
protected array $loaders = [];

public function __construct(?WorkflowLoader $loader)
{
if ($loader) {
$this->registerLoader($loader);
}
}

public function registerLoader(WorkflowLoader $loader)
{
$this->loaders[$loader::class] = $loader;
}

public function getLoader(string $loader): WorkflowLoader
{
return $this->loaders[$loader];
}

public function all(): array
{
if (count($this->loaders) === 0) {
return [];
}

return collect($this->loaders)->mapWithKeys(function ($loader) {
return [$loader::class => $loader->all()];
})->toArray();
}
}
10 changes: 6 additions & 4 deletions src/WorkflowStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ public function configurePackage(Package $package): void
'wf3_create_workflow_transitions_table',
'wf4_create_workflow_state_transitions_table',
])
->hasCommand(WorkflowStorageListCommand::class);
->hasCommand(WorkflowStorageListCommand::class)
->publishesServiceProvider('WorkflowServiceProvider');
}

public function packageRegistered()
{
$this->app->singleton('workflow-storage', function ($app) {
$workflowStorage = new WorkflowStorage($app->make(DatabaseLoader::class));
});

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

return new DatabaseLoader(config: $config);
});

$this->app->singleton(WorkflowStorage::class, function ($app) {
return new WorkflowStorage;
});
}
}
59 changes: 59 additions & 0 deletions tests/Feature/WorkflowStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use Soap\WorkflowStorage\Models\Workflow;

beforeEach(function () {

$workflow = Workflow::create([
'name' => 'Test Workflow',
'type' => 'workflow',
'description' => 'Test Workflow Description',
'supports' => [],
'metadata' => [],
]);

$draftState = $workflow->states()->create([
'name' => 'draft',
'metadata' => [],
]);

$onReviewState = $workflow->states()->create([
'name' => 'on review',
'metadata' => [],
]);

$approvedState = $workflow->states()->create([
'name' => 'approved',
'metadata' => [],
]);

$rejectedState = $workflow->states()->create([
'name' => 'rejected',
'metadata' => [],
]);

$onReviewTransition = $workflow->transitions()->create([
'name' => 'submit',
'to_state_id' => $onReviewState->id,
'metadata' => [],
]);

$onReviewTransition->fromStates()->create([
'from_state_id' => $draftState->id,
]);
});

test('data can be retrieved from the database', function () {
$workflow = Workflow::first();
$states = $workflow->states;
$transitions = $workflow->transitions;

expect($workflow->name)->toBe('Test Workflow');
expect($workflow->type->value)->toBe('workflow');
expect($workflow->description)->toBe('Test Workflow Description');
expect($workflow->supports)->toBe([]);
expect($workflow->metadata)->toBe([]);

expect($states->count())->toBe(4);
expect($transitions->count())->toBe(1);
});