view instanceof CLIView) $event->addParameter($this->options); } /** * Executes a command in the CLI environment. * * Returns true on successful processing. Returns false on failure * * @param string $commandString */ public function executeCommand(string $commandString): void { /** @var Router $router */ /** @var Output $output */ $this->router = Factory::getInstance('router'); $this->output = Factory::getInstance('cliOutput'); // Pre-process the command $commandString = $this->preProcess($commandString); // First test if the command is empty. If it is, cancel execution if (empty($commandString)) { $this->output->warningOut("No command provided!"); return; } // Distill different parts of the command $parts = explode(' ', $commandString); // Prepare the logCount for a consolidation of logs later // @todo Find a better way to determine to first log. LogCount is not effective and contains many logs from router // @todo Perhaps using routerCallViewEvent would be useful. Much closer than this early logCount $logCount = count(Logger::$logs); try { $this->router->route($commandString, 'cli'); } catch (NotFoundException $e) { $this->output->warningOut("Requested command '$parts[0]' was not found."); } catch (RouterException $e) { $this->output->errorOut($e->getMessage()); } catch (HaltException $e) { $this->output->errorOut("Requested command '$parts[0]' was denied."); } // Consolidate logs /** @var array $logs */ $logs = array_slice(Logger::$logs, $logCount); // And parse them $verbose = isset($this->options['v']) || isset($this->options['verbose']); $this->parseLogs($logs, $verbose); } /** * Processes a commandString and extracts options from it * * @todo Proper compatiblity with string like '--option "Hello World"' * * @param string $commandString * @return string */ protected function preProcess(string $commandString): string { // Split up the command into parts $parts = explode(' ', $commandString); // Pass over each part and look for special recognized parts, such as options $options = []; $keepParts = []; for ($i=0;$ioptions = $options; return implode(' ', $keepParts); } /** * Parse all logs from FuzeWorks::logger and output all that have been thrown for this command * * @param array $logs * @param bool $verbose */ protected function parseLogs(array $logs, bool $verbose = false) { foreach ($logs as $log) { switch ($log['type']) { case 'WARNING': $this->output->warningOut($log['message']); break; case 'ERROR': case 'EXCEPTION': $this->output->errorOut($log['message']); break; case 'LEVEL_START': case 'INFO': if ($verbose) $this->output->lineOut('&f[&rINFO&f]&r ' . $log['message']); break; } } } }