Updated examples, added onEnter method to contexts
This commit is contained in:
parent
ec60970b5d
commit
75c624520d
@ -36,9 +36,9 @@ $myShell->setPrompt("test>");
|
|||||||
|
|
||||||
// Create an anonymous context and add a command
|
// Create an anonymous context and add a command
|
||||||
$ctx = $myShell->createContext("root");
|
$ctx = $myShell->createContext("root");
|
||||||
$ctx->addCommand("hello", function () {
|
$ctx->addCommand("hello", function ($who="World") {
|
||||||
echo "Hello World!\n";
|
echo "Hello {$who}!\n";
|
||||||
});
|
}, [ "help"=>"Say hello", "args"=>"who" ]);
|
||||||
|
|
||||||
// Run the shell
|
// Run the shell
|
||||||
$myShell->run();
|
$myShell->run();
|
||||||
|
@ -6,21 +6,46 @@ use NoccyLabs\Shell\Shell;
|
|||||||
use NoccyLabs\Shell\Context;
|
use NoccyLabs\Shell\Context;
|
||||||
|
|
||||||
$shell = new Shell();
|
$shell = new Shell();
|
||||||
$shell->addListener("prompt", function ($event, $shell) {
|
$shell->addListener(Shell::EVT_UPDATE_PROMPT, function ($event) {
|
||||||
$name = $shell->getContextPath();
|
$name = $event->shell->getContextPath();
|
||||||
$shell->setPrompt("shell{$name}> ");
|
$event->shell->setPrompt("shell{$name}> ");
|
||||||
});
|
});
|
||||||
|
|
||||||
$root = new Context();
|
class MyContext extends Context
|
||||||
$root->addCommand("test", function () {
|
{
|
||||||
echo "It works!\n";
|
public function __construct()
|
||||||
});
|
{
|
||||||
$root->addCommand("context", function ($name) {
|
// Remember to call the parent constructor if you want to use
|
||||||
$context = new Context($name);
|
// the doccomment syntax to mark commands, as demonstrated
|
||||||
$context->name = $name;
|
// at the end of this class for the bar command.
|
||||||
return $context;
|
parent::__construct();
|
||||||
});
|
$this->addCommand("foo",[$this,"foo"],[
|
||||||
$shell->pushContext($root);
|
'help' => "Foo command"
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onEnter()
|
||||||
|
{
|
||||||
|
echo "Entering context!\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function foo()
|
||||||
|
{
|
||||||
|
echo "Foo!\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @command bar
|
||||||
|
* @args
|
||||||
|
* @help Bar command
|
||||||
|
*/
|
||||||
|
public function bar()
|
||||||
|
{
|
||||||
|
echo "Bar!\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$shell->pushContext(new MyContext());
|
||||||
|
|
||||||
|
|
||||||
$shell->run();
|
$shell->run();
|
||||||
|
@ -66,6 +66,11 @@ class Context
|
|||||||
$this->findCommands();
|
$this->findCommands();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onEnter()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected function findCommands()
|
protected function findCommands()
|
||||||
{
|
{
|
||||||
$refl = new \ReflectionClass(get_called_class());
|
$refl = new \ReflectionClass(get_called_class());
|
||||||
|
@ -113,6 +113,7 @@ class Shell
|
|||||||
}
|
}
|
||||||
$context->setShell($this);
|
$context->setShell($this);
|
||||||
$this->context = $context;
|
$this->context = $context;
|
||||||
|
$context->onEnter();
|
||||||
$this->dispatchEvent(self::EVT_CONTEXT_CHANGED);
|
$this->dispatchEvent(self::EVT_CONTEXT_CHANGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user