PHP Classes

File: test-cli.example.php

Recommend this page to a friend!
  Packages of JImmy Bo   PHP Non Blocking CLI   test-cli.example.php   Download  
File: test-cli.example.php
Role: Example script
Content type: text/plain
Description: Basic Example
Class: PHP Non Blocking CLI
Implement CLI scripts that do not block the output
Author: By
Last change:
Date: 3 years ago
Size: 4,369 bytes
 

Contents

Class file image Download
<?php

    include_once('class.nb_cli_1.0.php');

    # An example test plugin that would be the part added by the user to create their program
    class my_plugin extends nb_plugin {
        private $stdin;
        private $stdout;

        // so we can give this plugin some loop independence (run on their own timers)
        private $next_run       = []; // when a loop should run // An array so we can have multiple loops.
        private $loop_running   = []; // so we don't engage another loop until the other is finished processing

        public function __construct() {
            // Set non-blocking mode for STDOUT
            stream_set_blocking(STDOUT, false);

            // Set non-blocking mode for STDIN
            stream_set_blocking(STDIN, false);

            $this->stdin    = fopen('php://stdin', 'r');
            $this->stdout   = fopen('php://stdout', 'w');

            $this->welcome();
        }

        public function out($msg) {
            fwrite($this->stdout, $msg);
        }

        public function handleCommand($command) {
            // process commands from user when they press enter
            switch ($command) {
                case 'test':
                    $help = "Test command\n";
                    // fwrite($this->stdout, $help);
                    $this->out($help);
                    break;

                case 'help':
                case 'h':
                    $help = "---\n";
                    $help .= "Showing help for *TestPlugin*\n";
                    $help .= "Available commands:\n";
                    $help .= "  test - Test command\n";
                    $help .= "---\n\n";

                    $this->out($help);
                    break;
            }
        }

        public function welcome() {
            // triggered by main loop when code requests a welcome
            $welcome = "-- NonBlockingCLI test server --\n";
            $welcome .= "Welcome to the NonBlockingCLI test server.\n";
            $welcome .= "Type 'h' or 'help' for a list of commands.\n";
            $welcome .= "Type 'quit' to exit.\n";
            $welcome .= "\n\n";

            $this->out($welcome);
        }

        public function run() {
            // this loop runs at main class speed
            $this->run_loop_1(); // output current time every second
            // .. can add other loops. Use run_loop_1 as an example.
        }

        public function run_loop_1() {
            $loop_key = 1; // for $this->next_run and $this->loop_running

            // we want to take a slot in next_run in case we have other loops.
            if (!isset($this->next_run[$loop_key])) {
                $this->next_run[$loop_key] = 0.0; // initialize // example of potential values: 1 second = 1.0, 1.5 seconds = 1.5, etc.
                $this->loop_running[$loop_key] = false;
            }
            
            // check if we should run this time
            // $this->next_run = microseconds
            if ($this->loop_running[$loop_key] || microtime(true) < $this->next_run[$loop_key]) {
                return;
            }

            // set loop as running
            $this->loop_running[$loop_key] = true;

            $output = "Current time: " . date('Y-m-d H:i:s') . "\n";
            $this->out($output);
            
            // set pace of plugin loop (runs independent of main loop)
            // store our next run time in a variable
            $this->next_run[$loop_key]      = microtime(true) + 1.0; // 1 second = 1.0, 1.5 seconds = 1.5, etc.

            // set loop as not running
            $this->loop_running[$loop_key] = false;
        }
    }

    ## EXAMPLE: ##
    // create plugins
    $plugin_a = new my_plugin();
    $plugin_b = new my_plugin();

    // create main object that controls the plugins
    $cli = new nb_cli();

    // add plugins to main controller
    $cli->add_plugin($plugin_a);
    $cli->add_plugin($plugin_b);

    // run our main controller which will run the plugins welcome() functions
    $cli->welcome();

    // run our main controller which will run the plugins run() functions in a loop
    // maximum speed is set in main controller, but each plugin can time their own runs
    $cli->run();

?>