61 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/*
 | 
						|
 *  This example demonstrates how to use events to catch commands that
 | 
						|
 *  have not been handled by any context or builtin.
 | 
						|
 *
 | 
						|
 */
 | 
						|
 | 
						|
require_once __DIR__."/../vendor/autoload.php";
 | 
						|
 | 
						|
use NoccyLabs\Shell\Shell;
 | 
						|
use NoccyLabs\Shell\Style;
 | 
						|
use NoccyLabs\Shell\Context;
 | 
						|
use NoccyLabs\Shell\TaskInterface;
 | 
						|
 | 
						|
class MyTask implements TaskInterface
 | 
						|
{
 | 
						|
    protected $start;
 | 
						|
    protected $last;
 | 
						|
    public function __construct()
 | 
						|
    {
 | 
						|
        $this->start = time();
 | 
						|
    }
 | 
						|
    public function update()
 | 
						|
    {
 | 
						|
        if ($this->last < time()) {
 | 
						|
            echo date("H:i:s")."\n";
 | 
						|
            $this->last = time();
 | 
						|
        }
 | 
						|
    }
 | 
						|
    public function isValid()
 | 
						|
    {
 | 
						|
        return (time() - $this->start < 5);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
$myShell = new Shell();
 | 
						|
 | 
						|
$myShell->addListener(Shell::EVT_TASK_CREATED, function ($e) {
 | 
						|
    echo "EVT_TASK_CREATED:\n";
 | 
						|
    printf("|_task: %s\n", get_class($e->task));
 | 
						|
});
 | 
						|
$myShell->addListener(Shell::EVT_TASK_DESTROYED, function ($e) {
 | 
						|
    echo "EVT_TASK_DESTROYED:\n";
 | 
						|
    printf("|_task: %s\n", get_class($e->task));
 | 
						|
});
 | 
						|
 | 
						|
 | 
						|
$myShell->addTask(new MyTask());
 | 
						|
 | 
						|
// Set the initial prompt, not really needed.
 | 
						|
$myShell->setPrompt("test>");
 | 
						|
 | 
						|
// Create an anonymous context and add a command
 | 
						|
$ctx = $myShell->createContext("root");
 | 
						|
$ctx->addCommand("hello", function () {
 | 
						|
    echo "Hello World!\n";
 | 
						|
});
 | 
						|
 | 
						|
// Run the shell
 | 
						|
$myShell->run();
 |