PHP code example of siarko / cli-bootstrap

1. Go to this page and download the library: Download siarko/cli-bootstrap 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/ */

    

siarko / cli-bootstrap example snippets


// This is a dependency Event manager, or any that implements interface
$eventManager = new EventManager();
// Logger is also a part of this lib; but very basic
$logger = new BasicLogger(__DIR__.'/logs.log');

$bootstrap = new Bootstrap($eventManager, $logger);

// Set how much times to execute tasks per second
$bootstrap->setTargetTPS(2);


/** @var $event SignalEvent */
$eventManager->addListener(function(SignalEvent $event) use($bootstrap){
    // CTRL + C -> We should stop running
    if($event->getData() === SIGINT) {
        echo "[INTERRUPT]\n";
        // This is how we stop
        $bootstrap->stop();
    }
});

$eventManager->addListener(function(CleanupEvent $event) {
    // This will be called almost always
    echo "[CLEANUP EVENT]\n";
});

$eventManager->addListener(function(ExitEvent $event) {
    // This will be called almost always - last event before exiting
    echo "[EXIT EVENT]\n";
});

$bootstrap->addTask(function () use ($bootstrap){
    // Just to slow down the logs
    usleep(200000);
    echo "ITERATION\n";
    echo $bootstrap->getLoopTime().", TPS: ". $bootstrap->getCurrentTPS()."\n";
});

// Start loop execution. Will block until loop is stopped
// Either with $bootstrap->stop(), with error or however else.
$bootstrap->run();