iconic / engine
Engine is a workflow state machine that helps keeping business logic centralized
v0.1.9
2020-12-08 09:08 UTC
Requires
- php: >=7.4
- iconic/uniproperty: ^0.1
Requires (Dev)
- behat/behat: ^3.8
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^9.4
- symfony/var-dumper: ^5.2
This package is auto-updated.
Last update: 2024-10-27 15:19:59 UTC
README
Engine is heavily inspired by symfony/workflow, but meant to be significantly simpler to use, and allow extra checks about both the actor and the object of the workflow transitions and actions.
Installation
You can install the library using composer
composer require iconic/engine
Quick Guide
All you have to do is create an instance of Engine and start defining and checking allowed actions on properties and from actors (supports getters/setters and public properties).
- Allow everyone to view
$engine = new \Iconic\Engine\Engine(); $engine->allow('view'); $allowed = $engine->can('view'); //returns true $allowed = $engine->can('edit'); //returns false
- Allow publishing of posts only if their status is draft
$post = new Post(); $post->status = "submitted"; $engine = new \Iconic\Engine\Engine(); $engine->allow('publish')->of('status', 'draft', 'published'); $allowed = $engine->can('publish', $post); //returns false $engine->apply('publish', $post); //throws Exception $post->status = "draft"; $allowed = $engine->can('publish', $post); //returns true $engine->apply('publish', $post); //changes post status to "published"
- Allow only actors with "role" "editor" to edit, without defining object restrictions
$user = new User(); $user->role = "user"; $engine = new \Iconic\Engine\Engine(); $engine->allow('edit')->if('role', 'editor'); $allowed = $engine->can('edit', null, $user); //returns false
- Combine the two above scenarios. Allow posts with "status" "draft" to be "published" by actors with "role" "editor"
$editor = new User(); $editor->role = 'editor'; $user = new User(); $user->role = 'user'; $draftPost = new Post(); $draftPost->status = "draft"; $deletedPost = new Post(); $deletedPost->status = "deleted"; $engine = new \Iconic\Engine\Engine(); $engine->allow('publish')->of('status', 'draft', 'published'); $engine->allow('publish')->if('role', 'editor'); $engine->can('publish', $draftPost, $editor); //returns true $engine->apply('publish', $draftPost, $editor); //changes post status to "published" $engine->can('publish', $deletedPost, $editor); //returns false $engine->apply('publish', $deletedPost, $editor); //throws exception $engine->can('publish', $draftPost, $user); //returns false $engine->apply('publish', $draftPost, $user); //throws exception
- You can define multiple rules for each action