PHP Classes

File: examples/examplecli.php

Recommend this page to a friend!
  Packages of Jorge Castro   MultiOne   examples/examplecli.php   Download  
File: examples/examplecli.php
Role: Example script
Content type: text/plain
Description: Example script
Class: MultiOne
Execute many tasks in parallel using JavaScript
Author: By
Last change:
Date: 1 year ago
Size: 3,764 bytes
 

Contents

Class file image Download
<?php /** @noinspection DuplicatedCode */
/** @noinspection DuplicatedCode */
/** @noinspection PhpUnhandledExceptionInspection */

/** @noinspection PhpUnusedParameterInspection */

use Eftec\MultiOne\MultiOne;
include __DIR__ . '/../vendor/autoload.php';
MultiOne::Factory(
    1000, // every miliseconds
    basename(__FILE__), // the url to call
    1

)->setMethods(
    static function($numWorkers, $payload): array { // the initial call
        $_SESSION['USERNAME'] = '(no user)';
        $width=$payload['width'];
        $height=$payload['height'];
        return [
            str_repeat('-', $width),
            "- Session ID:".session_id(),
            "- Write login <username> to change username",
            "- Colors: red,green,blue,yellow,magenta,cyan,white and black",
            "- Back: bred,bgreen,bblue,byellow,bmagenta,bcyan,bwhite and bblack",
            "- others: error (show error),exit(show prompt),ls(list values),clean (clean screen)",
            str_repeat('-', $width),];
    },
    static function($idWorker, $payload): array { // the worker call
        $username=$_SESSION['USERNAME']??null;
        // it reads the data and reduces to 1.
        if($username===null) {
            throw new RuntimeException('no user is not set');
        }
        $line = $payload['line'];
        if (strpos($line, 'login ') === 0) {
            $_SESSION['USERNAME'] = substr($line, strlen('login '));
            $username=$_SESSION['USERNAME']??null;
            return MultiOne::msgRun($username . ': ok',$username.'> ');
        }
        $defaultPrompt=$username.'> ';
        if ($line === 'red') {
            return MultiOne::msgRun($username . ': <red>Red</red>');
        }
        $colors=['green','red','blue','yellow','magenta','cyan','white','black'];
        $colorsb=['bgreen','bred','bblue','byellow','bmagenta','bcyan','bwhite','bblack'];
        if(in_array($line, $colors, true)) {
            return MultiOne::msgRun($username . ": <$line>$line</$line>",$defaultPrompt);
        }
        if(in_array($line, $colorsb, true)) {
            return MultiOne::msgRun($username . ": <$line>$line</$line>",$defaultPrompt);
        }
        switch ($line) {
            case 'logout':
                $_SESSION['USERNAME'] = '(no user)';
                break;
            case 'sudo':
                return MultiOne::msgRun("<bred><black>no sudo\n</black></bred>",$defaultPrompt);
            case 'ls':
                return MultiOne::msgRun(
                    [
                        $username . ': ' . $payload['line'],
                        'file1.jpg        233222',
                        'file2.jpg        233232',
                        'file3.jpg        545454',
                        'file4.jpg        566556'
                    ],$defaultPrompt);
            case 'error':
                return MultiOne::msgError(
                    null,$defaultPrompt,$username . ': <red>? Error</red>');
            case 'clean':
                return MultiOne::msgFull($username . ': ' . $payload['line'],$defaultPrompt);
            case 'exit':
                return MultiOne::msgRun(
                    $username . ': <green>Do you want to exit?</green>'
                    , 'Do you want to exit? '); // the new cursor (if none is set then it returns to the default value
        }

        return MultiOne::msgRun($username . ': ' . $payload['line'],$defaultPrompt);
    },
    static function($bodies) { // the worker-end call
        echo "all worker ended";
    }
)->setJSMethods(
    "function(nw,json) { 
    console.log(json); 
    return json;
    }",
    "function(nw,response) { 
    return response;
    }")
    ->setUI()
    ->setCli('(no user)> ','100%','600px')
    ->runAuto();