php-shell/examples/simple.php

95 lines
1.7 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;
2016-11-01 15:10:35 +00:00
use NoccyLabs\Shell\Context;
2016-04-25 18:47:30 +00:00
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
}
}
2016-11-01 15:10:35 +00:00
class MyContext extends Context
{
/**
* @command testme
2016-11-01 15:10:35 +00:00
* @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
2016-11-01 15:10:35 +00:00
*/
public function test()
{
}
}
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 15:10:35 +00:00
$context = new MyContext();
$this->pushContext($context);
2016-11-01 14:12:11 +00:00
$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-11-01 14:15:47 +00:00
$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";
}
});
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 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";