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.

2.0.1 2024-12-24 17:39 UTC

This package is auto-updated.

Last update: 2024-12-24 22:07:36 UTC


README

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.

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.