tarfin-labs/event-machine

An event-driven state machine library for PHP, providing an expressive language to define and manage application states, enabling developers to create complex workflows with ease and maintainability.

1.1.0 2023-12-13 11:09 UTC

This package is auto-updated.

Last update: 2024-04-15 00:07:01 UTC


README

Where Events Drive the Gears of Progress

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

EventMachine is a PHP library for creating and managing event-driven state machines. It is designed to be simple and easy to use, while providing powerful functionality for managing complex state transitions. This library is heavily influenced by XState, a popular JavaScript state machine library.

---
title: traffic_lights_machine
---
stateDiagram-v2
    [*] --> Red
    Red --> Yellow: TIMER_RED <br/>do / wait_for_red_light
    Yellow --> Green: TIMER_YELLOW <br/>do / wait_for_yellow_light
    Green --> Red: TIMER_GREEN <br/>do / wait_for_green_light

    Red --> PowerOff: [is_power_off]
    Yellow --> PowerOff: [is_power_off]
    Green --> PowerOff: [is_power_off]

    Red : Red<br/>entry / turn_on_red_light<br/>exit / turn_off_red_light
    Yellow : Yellow<br/>entry / turn_on_yellow_light<br/>exit / turn_off_yellow_light
    Green : Green<br/>entry / turn_on_green_light<br/>exit / turn_off_green_light

Installation

You can install the package via composer:

composer require tarfin-labs/event-machine

You can publish and run the migrations with:

php artisan vendor:publish --tag="event-machine-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="event-machine-config"

This is the contents of the published config file:

return [
];

Usage

$machine = MachineDefinition::define(
        config: [
            'initial' => 'green',
            'context' => [
                'value' => 1,
            ],
            'states' => [
                'green' => [
                    'on' => [
                        'TIMER' => [
                            [
                                'target'     => 'yellow',
                                'guards' => 'isOneGuard',
                            ],
                            [
                                'target'     => 'red',
                                'guards' => 'isTwoGuard',
                            ],
                            [
                                'target' => 'pedestrian',
                            ],
                        ],
                    ],
                ],
                'yellow'     => [],
                'red'        => [],
                'pedestrian' => [],
            ],
        ],
        behavior: [
            'guards' => [
                'isOneGuard' => function (ContextManager $context, array $event): bool {
                    return $context->get('value') === 1;
                },
                'isTwoGuard' => function (ContextManager $context, array $event): bool {
                    return $context->get('value') === 2;
                },
            ],
        ],
    );

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.