php-shell/examples/simple.php

95 lines
1.7 KiB
PHP

<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Shell\Shell;
use NoccyLabs\Shell\Command;
use NoccyLabs\Shell\Context;
class MyCommand extends Command
{
public function execute()
{
echo "Executing command";
}
}
class MyContext extends Context
{
/**
* @command testme
* @args
* @help Useful test!
* @global
*/
public function test()
{
echo "Test\n";
}
/**
* @command context
* @help Create a new context
*/
public function context()
{
return new OtherContext("newcontext");
}
}
class OtherContext extends Context
{
/**
* @command other
* @args
* @help Other test
*/
public function test()
{
}
}
class MyShell extends Shell
{
protected $seq = 0;
protected function configure()
{
$context = new MyContext();
$this->pushContext($context);
$context->addCommand("hello", function () {
echo "world\nthis\nis\na\ntest\n";
});
$context->addCommand("mycommand", new MyCommand());
$this->updatePrompt();
$this->addListener("update", function() {
static $lt;
$t = floor(microtime(true));
if ($t > $lt) {
$lt = $t + 5;
echo date("Y-m-d h:i:s")."\n";
}
});
}
protected function updatePrompt()
{
$this->setPrompt("test[{$this->seq}]: ");
}
protected function onCommand($buffer)
{
$this->seq++;
$this->updatePrompt();
parent::onCommand($buffer);
}
}
$myShell = new MyShell();
$myShell->run();
echo "Exiting\n";