php-shell/examples/simple.php

58 lines
1.1 KiB
PHP
Raw Normal View History

2016-04-25 18:47:30 +00:00
<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Shell\Shell;
use NoccyLabs\Shell\Command;
class MyCommand extends Command
{
public function execute()
{
2016-11-01 14:12:11 +00:00
echo "Executing command";
2016-04-25 18:47:30 +00:00
}
}
class MyShell extends Shell
{
protected $seq = 0;
2016-11-01 14:12:11 +00:00
protected function configure()
2016-04-25 18:47:30 +00:00
{
2016-11-01 14:12:11 +00:00
$context = $this->createContext();
$context->addCommand("hello", function () {
echo "world\nthis\nis\na\ntest\n";
2016-04-25 18:47:30 +00:00
});
2016-11-01 14:12:11 +00:00
$context->addCommand("mycommand", new MyCommand());
2016-04-29 13:49:10 +00:00
$this->updatePrompt();
2016-04-25 18:47:30 +00:00
}
2016-04-29 13:49:10 +00:00
protected function updatePrompt()
2016-04-25 18:47:30 +00:00
{
2016-04-29 13:49:10 +00:00
$this->setPrompt("test[{$this->seq}]: ");
2016-04-25 18:47:30 +00:00
}
protected function onUpdate()
{
static $lt;
$t = floor(microtime(true));
if ($t > $lt) {
$lt = $t + 5;
echo date("Y-m-d h:i:s")."\n";
}
}
protected function onCommand($buffer)
{
$this->seq++;
2016-04-29 13:49:10 +00:00
$this->updatePrompt();
2016-04-25 18:47:30 +00:00
parent::onCommand($buffer);
}
}
$myShell = new MyShell();
$myShell->run();
echo "Exiting\n";