logger = $logger; $this->commands = $commands; } //-------------------------------------------------------------------- /** * Actually execute a command. * This has to be over-ridden in any concrete implementation. * * @param array $params */ abstract public function run(array $params); //-------------------------------------------------------------------- /** * Can be used by a command to run other commands. * * @param string $command * @param array $params * * @return mixed * @throws \ReflectionException */ protected function call(string $command, array $params = []) { // The CommandRunner will grab the first element // for the command name. array_unshift($params, $command); return $this->commands->index($params); } //-------------------------------------------------------------------- /** * A simple method to display an error with line/file, * in child commands. * * @param \Exception $e */ protected function showError(\Exception $e) { CLI::newLine(); CLI::error($e->getMessage()); CLI::write($e->getFile() . ' - ' . $e->getLine()); CLI::newLine(); } //-------------------------------------------------------------------- /** * Makes it simple to access our protected properties. * * @param string $key * * @return mixed */ public function __get(string $key) { if (isset($this->$key)) { return $this->$key; } return null; } //-------------------------------------------------------------------- /** * Makes it simple to check our protected properties. * * @param string $key * * @return boolean */ public function __isset(string $key): bool { return isset($this->$key); } //-------------------------------------------------------------------- /** * show Help include (usage,arguments,description,options) */ public function showHelp() { // 4 spaces instead of tab $tab = ' '; CLI::write(lang('CLI.helpDescription'), 'yellow'); CLI::write($tab . $this->description); CLI::newLine(); CLI::write(lang('CLI.helpUsage'), 'yellow'); $usage = empty($this->usage) ? $this->name . ' [arguments]' : $this->usage; CLI::write($tab . $usage); CLI::newLine(); $pad = max($this->getPad($this->options, 6), $this->getPad($this->arguments, 6)); if (! empty($this->arguments)) { CLI::write(lang('CLI.helpArguments'), 'yellow'); foreach ($this->arguments as $argument => $description) { CLI::write($tab . CLI::color(str_pad($argument, $pad), 'green') . $description, 'yellow'); } CLI::newLine(); } if (! empty($this->options)) { CLI::write(lang('CLI.helpOptions'), 'yellow'); foreach ($this->options as $option => $description) { CLI::write($tab . CLI::color(str_pad($option, $pad), 'green') . $description, 'yellow'); } CLI::newLine(); } } //-------------------------------------------------------------------- /** * Get pad for $key => $value array output * * @param array $array * @param integer $pad * * @return integer */ public function getPad(array $array, int $pad): int { $max = 0; foreach ($array as $key => $value) { $max = max($max, strlen($key)); } return $max + $pad; } //-------------------------------------------------------------------- }