Added exception handling to run()
This commit is contained in:
		@@ -321,57 +321,62 @@ class Shell
 | 
			
		||||
 | 
			
		||||
    public function run()
 | 
			
		||||
    {
 | 
			
		||||
        $this->lineReader = new LineRead();
 | 
			
		||||
        try {
 | 
			
		||||
            $this->lineReader = new LineRead();
 | 
			
		||||
 | 
			
		||||
        $this->lineReader->setPromptText($this->prompt);
 | 
			
		||||
        $this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
 | 
			
		||||
        $this->lineReader->setCommandStyle(new Style(Style::GREEN));
 | 
			
		||||
            $this->lineReader->setPromptText($this->prompt);
 | 
			
		||||
            $this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
 | 
			
		||||
            $this->lineReader->setCommandStyle(new Style(Style::GREEN));
 | 
			
		||||
 | 
			
		||||
        $this->running = true;
 | 
			
		||||
            $this->running = true;
 | 
			
		||||
 | 
			
		||||
        $this->dispatchEvent("prompt", [ "context"=>$this->context ]);
 | 
			
		||||
            $this->dispatchEvent("prompt", [ "context"=>$this->context ]);
 | 
			
		||||
 | 
			
		||||
        while ($this->running) {
 | 
			
		||||
            // Update the input stuff, sleep if nothing to do.
 | 
			
		||||
            if (!($buffer = $this->lineReader->update())) {
 | 
			
		||||
                usleep(10000);
 | 
			
		||||
            }
 | 
			
		||||
            // Escape is handy too...
 | 
			
		||||
            if ($buffer == "\e") {
 | 
			
		||||
                $this->dispatchEvent("shell.abort");
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            // we get a ^C on ^C, so deal with the ^C.
 | 
			
		||||
            if ($buffer == "\x03") {
 | 
			
		||||
                $this->dispatchEvent("shell.stop");
 | 
			
		||||
                $this->stop();
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            // Execute the buffer
 | 
			
		||||
            ob_start();
 | 
			
		||||
            $this->dispatchEvent("update");
 | 
			
		||||
            foreach ($this->timers as $timer) {
 | 
			
		||||
                $timer->update();
 | 
			
		||||
            }
 | 
			
		||||
            if ($buffer) {
 | 
			
		||||
                $this->executeBuffer($buffer);
 | 
			
		||||
            }
 | 
			
		||||
            $output = ob_get_contents();
 | 
			
		||||
            ob_end_clean();
 | 
			
		||||
            while ($this->running) {
 | 
			
		||||
                // Update the input stuff, sleep if nothing to do.
 | 
			
		||||
                if (!($buffer = $this->lineReader->update())) {
 | 
			
		||||
                    usleep(10000);
 | 
			
		||||
                }
 | 
			
		||||
                // Escape is handy too...
 | 
			
		||||
                if ($buffer == "\e") {
 | 
			
		||||
                    $this->dispatchEvent("shell.abort");
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
                // we get a ^C on ^C, so deal with the ^C.
 | 
			
		||||
                if ($buffer == "\x03") {
 | 
			
		||||
                    $this->dispatchEvent("shell.stop");
 | 
			
		||||
                    $this->stop();
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
                // Execute the buffer
 | 
			
		||||
                ob_start();
 | 
			
		||||
                $this->dispatchEvent("update");
 | 
			
		||||
                foreach ($this->timers as $timer) {
 | 
			
		||||
                    $timer->update();
 | 
			
		||||
                }
 | 
			
		||||
                if ($buffer) {
 | 
			
		||||
                    $this->executeBuffer($buffer);
 | 
			
		||||
                }
 | 
			
		||||
                $output = ob_get_contents();
 | 
			
		||||
                ob_end_clean();
 | 
			
		||||
 | 
			
		||||
            if (trim($output)) {
 | 
			
		||||
                $this->lineReader->erase();
 | 
			
		||||
                echo rtrim($output)."\n";
 | 
			
		||||
                $this->lineReader->redraw();
 | 
			
		||||
                if (trim($output)) {
 | 
			
		||||
                    $this->lineReader->erase();
 | 
			
		||||
                    echo rtrim($output)."\n";
 | 
			
		||||
                    $this->lineReader->redraw();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (!$this->context) {
 | 
			
		||||
                    $this->stop();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if ($buffer) {
 | 
			
		||||
                    $this->dispatchEvent("prompt", [ "context"=>$this->context ]);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (!$this->context) {
 | 
			
		||||
                $this->stop();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if ($buffer) {
 | 
			
		||||
                $this->dispatchEvent("prompt", [ "context"=>$this->context ]);
 | 
			
		||||
            }
 | 
			
		||||
        } catch (\Exception $e) {
 | 
			
		||||
            fprintf(STDERR, "\e[31;1mFatal: Unhandled exception\e[0m\n\n%s\n", $e);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->lineReader = null;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user