Skip to content
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

Refactor code style and drop support for php7 #25

Merged
merged 8 commits into from
Oct 8, 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
40 changes: 30 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
name: Build
name: Unit Test

on:
pull_request:
branches:
- master
- main
push:
branches:
- master
- main

jobs:
build:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: [7.3, 7.4, 8.0, 8.1]
setup: [basic, stable, lowest]
php: [8.0, 8.1]
laravel: [8.*, 9.*, 10.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- dbal: ignore
- laravel: 8.*
testbench: ^6.0
dbal: install
- laravel: 9.*
dbal: install
exclude:
- php: 8.0
setup: lowest
- php: 8.1
setup: lowest
- laravel: 10.*
php: 8.0

name: PHP${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

steps:
- name: Checkout
Expand All @@ -31,11 +42,20 @@ jobs:
with:
php-version: ${{matrix.php}}

- name: Cache Composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
# Use composer.json for key, if composer.lock is not committed.
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
run: |
if [[ ${{matrix.setup}} = 'basic' ]]; then composer install --prefer-dist --no-interaction --no-suggest; fi
if [[ ${{matrix.setup}} = 'stable' ]]; then composer update --prefer-dist --no-interaction --no-suggest --prefer-stable; fi
if [[ ${{matrix.setup}} = 'lowest' ]]; then composer update --prefer-dist --no-interaction --no-suggest --prefer-lowest --prefer-stable; fi
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
if [[ ${{matrix.dbal}} = 'install' ]]; then composer require doctrine/dbal; fi
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Run PHPUnit tests
run: |
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.DS_Store
/composer.lock
/.idea/
/.phpunit.result.cache
/*.cache
/vendor/
phpunit.xml.bak
59 changes: 59 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
preset: psr12
version: 8
enabled:
- alpha_ordered_imports
- binary_operator_spaces
- blank_line_before_return
- cast_spaces
- hash_to_slash_comment
- heredoc_to_nowdoc
- include
- linebreak_after_opening_tag
- method_separation
- native_function_casing
- no_blank_lines_after_phpdoc
- no_blank_lines_after_return
- no_blank_lines_after_throw
- no_blank_lines_between_imports
- no_blank_lines_between_traits
- no_empty_comment
- no_empty_phpdoc
- no_empty_statement
- no_extra_consecutive_blank_lines
- no_short_bool_cast
- no_singleline_whitespace_before_semicolons
- no_spaces_inside_offset
- no_spaces_outside_offset
- no_trailing_comma_in_list_call
- no_trailing_comma_in_singleline_array
- no_unneeded_control_parentheses
- no_unused_imports
- no_useless_return
- no_whitespace_before_comma_in_array
- normalize_index_brace
- object_operator_without_whitespace
- phpdoc_add_missing_param_annotation
- phpdoc_indent
- phpdoc_inline_tag
- phpdoc_link_to_see
- phpdoc_no_access
- phpdoc_no_empty_return
- phpdoc_no_package
- phpdoc_order
- phpdoc_scalar
- phpdoc_separation
- phpdoc_single_line_var_spacing
- phpdoc_trim
- phpdoc_type_to_var
- phpdoc_types
- phpdoc_var_without_name
- print_to_echo
- short_array_syntax
- space_after_semicolon
- standardize_not_equals
- trailing_comma_in_multiline_array
- trim_array_spaces
- unalign_double_arrow
- unalign_equals
- unary_operator_spaces
- whitespace_after_comma_in_array
54 changes: 45 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Laravel State workflow
Implement [Symfony Workflow](https://symfony.com/doc/current/components/workflow.html) component in Laravel
[![Unit Test](https://github.com/RingierIMU/state-workflow/actions/workflows/main.yml/badge.svg)](https://github.com/RingierIMU/state-workflow/actions/workflows/main.yml)

A workflow consist of state and actions to get from one place to another.
The actions are called transitions which describes how to get from one state to another.
**Laravel State workflow** provide tools for defining and managing workflows and activities with ease.
It offers an object oriented way to define a process or a life cycle that your object goes through.
Each step or stage in the process is called a state. You do also define transitions that describe the action to get from one state to another.

A workflow consist of **state** and **actions** to get from one state to another.
These **actions** are called **transitions** which describes how to get from one state to another.
## Installation
```
$ composer require ringierimu/state-workflow
```

For Laravel versions lower than 5.5, this step is important after running above script.
- Open your config/app.php file and add custom service provider:
```php
Ringierimu\StateWorkflow\StateWorkflowServiceProvider::class
```
Publish `config/workflow.php` file
```php
$ php artisan vendor:publish --provider="Ringierimu\StateWorkflow\StateWorkflowServiceProvider"
Expand Down Expand Up @@ -81,6 +80,16 @@ use Ringierimu\StateWorkflow\Traits\HasWorkflowTrait;
class Post extends Model
{
use HasWorkflowTrait;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
...,
'current_state', // If not using default attribute, update this to match value in workflow.php
]
}
```

Expand All @@ -104,6 +113,25 @@ $post->canTransition("activate"); // True

//Return Model state history
$post->stateHistory();
```

### Authenticated User Resolver
Ability to audit and track who action a specific state change for your object.
The package leverage the default Laravel auth provider to resolve the authenticated user when applying the state changes.

For a custom authentication mechanism, you should override `authenticatedUserId` in your object class with your own implementation.

```php
/**
* Return authenticated user id.
*
* @return int|null
*/
public function authenticatedUserId()
{
// Implement authenticated user resolver
}

```
### Fired Event
Each step has three events that are fired in order:
Expand Down Expand Up @@ -252,4 +280,12 @@ You will be required to download `dot` command to make use of this command.
### Usage
```php
php artisan workflow:dump workflow_name
```
```

## Run Unit Test
```bash
composer test
```

## Credits
- [Symfony Workflow](https://symfony.com/doc/current/components/workflow.html)
26 changes: 13 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
{
"name": "ringierimu/state-workflow",
"type": "library",
"description": "State Management workflow for Laravel",
"description": "Laravel State Workflow provide tools for defining and managing workflows and activities with ease.",
"keywords": [
"workflow",
"state management",
"state machine",
"state workflow",
"laravel"
],
"homepage": "https://github.com/RingierIMU/state-workflow",
"license": "MIT",
"authors": [
{
"name": "Norby Baruani",
"email": "[email protected]",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": "^7.3|^8.0",
"illuminate/events": "^7|^8|^9|^10.0",
"illuminate/support": "^7|^8|^9|^10.0",
"php": "^8.0",
"illuminate/events": "^8|^9|^10.0",
"illuminate/support": "^8|^9|^10.0",
"symfony/event-dispatcher": "^5.1",
"symfony/workflow": "^5.1",
"symfony/property-access": "^5.1"
},
"require-dev": {
"funkjedi/composer-include-files": "^1.0",
"laravel/legacy-factories": "^1.1",
"mockery/mockery": "^1.3|^1.4.2",
"orchestra/database": "^5|^6|^7",
"orchestra/testbench": "^5|^6|^7|^8.0",
"phpunit/phpunit": "^8|^9"
"orchestra/testbench": "^6.24|^7.0|^8.0",
"phpunit/phpunit": "^9.5|^10.0"
},
"extra": {
"include_files": [
Expand All @@ -50,16 +50,16 @@
"autoload-dev": {
"psr-4": {
"Ringierimu\\StateWorkflow\\Tests\\": "tests/"
},
"classmap": [
"tests/Fixtures/database/factories"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"funkjedi/composer-include-files": true
}
},
"scripts": {
"test": "phpunit"
}
}
52 changes: 40 additions & 12 deletions config/workflow.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
<?php

return [

'setup' => [
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| This define Authentication user is model of your application.
| Ideally it should match your `providers.users.model` found in `config/auth.php`
| to leverage the default Laravel auth resolver
|
*/
'user_class' => \App\User::class,
],

// this should be your model name in camelcase. eg. PropertyListing::Class => propertyListing
/*
|--------------------------------------------------------------------------
| Domain entity
|--------------------------------------------------------------------------
|
| This should be your model name in camelCase.
|
| eg. UserProfile::Class => userProfile
|
| Attributes definition
|
| subscriber:
| Register subscriber for this workflow which contains business rules.
|
| property_path:
| Attribute on your domain entity holding the actual state (default is "current_state")
|
| states:
| Define all possible state your domain entity can transition to
|
| transitions:
| Define all allowed transitions to transit from one state to another
*/
'user' => [
// class of your domain object
'class' => \App\User::class,

// Register subscriber for this workflow which contains business rules. Uncomment line below to register subscriber
//'subscriber' => \App\Listeners\UserEventSubscriber::class,
'subscriber' => \App\Listeners\UserEventSubscriber::class,

// property of your object holding the actual state (default is "current_state")
//'property_path' => 'current_state', //uncomment this line to override default value
// Uncomment line below to override default attribute
// 'property_path' => 'current_state',

// list of all possible states
'states' => [
'new',
'pending_activation',
Expand All @@ -26,23 +55,22 @@
'blocked',
],

// list of all possible transitions
'transitions' => [
'create' => [
'from' => 'new',
'to' => 'pending_activation',
'to' => 'pending_activation',
],
'activate' => [
'from' => 'pending_activation',
'to' => 'activated',
'to' => 'activated',
],
'block' => [
'from' => ['pending_activation', 'activated'],
'to' => 'blocked',
'to' => 'blocked',
],
'delete' => [
'from' => ['pending_activation', 'activated', 'blocked'],
'to' => 'deleted',
'to' => 'deleted',
],
],
],
Expand Down
Loading
Loading