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
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 [
];
$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;
},
],
],
);
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.