PHP code example of jeyroik / extas-m

1. Go to this page and download the library: Download jeyroik/extas-m library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jeyroik / extas-m example snippets


use extas\components\SystemContainer as Container;
use extas\interfaces\machines\IMachineRepository;
use extas\interfaces\machines\IMachine;

$machineRepo = Container::getItem(IMachineRepository::class);
$machine = $machineRepo->one([IMachine::FIELD__NAME => 'extas.demo']);
$machine->run('init', ['anything' => 'you want']);

echo '<pre>' . print_r($machine->getDump(), true) . '</pre>;

use extas\components\plugins\Plugin;

class PluginStateHello extends Plugin
{
    public function __invoke($state, &$context, $machine, &$isSuccess)
    {
        $context['text'] = $context['text'] . 'hello';
        $isSuccess = true;
    }
}

class PluginStateSpace extends Plugin
{
    public function __invoke($state, &$context, $machine, &$isSuccess)
    {
        $context['text'] = $context['text'] . ' ';
        $isSuccess = true;
    }
}

class PluginStateWorld extends Plugin
{
    public function __invoke($state, &$context, $machine, &$isSuccess)
    {
        $context['text'] = $context['text'] . 'world';
        $isSuccess = true;
    }
}

$machine = $machineRepo->one([IMachine::FIELD__NAME => 'hello_world'])
$machine->run('hello', ['text' => '']); // "hello world" 
/**
 * [
 *    [
 *      "state_from" => "hello_world.init",
 *      "state_to" => "hello_world.hello",
 *      "context" => [
 *        "text" => ""
 *      ]
 *    ],
 *    [
 *      "state_from" => "hello_world.hello",
 *      "state_to" => "hello_world.hello",
 *      "context" => [
 *        "text" => "hello"
 *      ]
 *    ], 
 *    [
 *      "state_from" => "hello_world.space",
 *      "state_to" => "hello_world.hello",
 *      "context" => [
 *        "text" => "hello "
 *      ]
 *    ],
 *    [
 *      "state_from" => "hello_world.world",
 *      "state_to" => "hello_world.end",
 *      "context" => [
 *        "text" => "hello world"
 *      ]
 *    ],
 *    [
 *      "state_from" => "hello_world.end",
 *      "state_to" => "sub_machine.print_html",
 *      "context" => [
 *        "text" => "hello world"
 *      ]
 *    ]
 * ] 
 */
$machine->dump();