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