injectableFactory = $injectableFactory; $this->metadata = $metadata; } public function run(string $command) { $command = ucfirst(Util::hyphenToCamelCase($command)); $params = $this->getParams($_SERVER['argv']); $options = $params['options']; $flagList = $params['flagList']; $argumentList = $params['argumentList']; $className = $this->getClassName($command); $obj = $this->injectableFactory->create($className); return $obj->run($options, $flagList, $argumentList); } protected function getClassName(string $command) : string { $className = $this->metadata->get(['app', 'consoleCommands', $command, 'className']) ?? 'Espo\\Core\\Console\\Commands\\' . $command; if (!class_exists($className)) { $msg = "Command '{$command}' does not exist."; echo $msg . "\n"; throw new Error($msg); } return $className; } protected function getParams(array $argv) : array { $argumentList = []; $options = []; $flagList = []; $skipIndex = 1; if (isset($argv[0]) && $argv[0] === 'command.php') { $skipIndex = 2; } foreach ($argv as $i => $item) { if ($i < $skipIndex) continue; if (strpos($item, '--') === 0 && strpos($item, '=') > 2) { list($name, $value) = explode('=', substr($item, 2)); $name = Util::hyphenToCamelCase($name); $options[$name] = $value; } else if (strpos($item, '--') === 0) { $flagList[] = Util::hyphenToCamelCase(substr($item, 2)); } else if (strpos($item, '-') === 0) { $flagList[] = substr($item, 1); } else { $argumentList[] = $item; } } return [ 'argumentList' => $argumentList, 'options' => $options, 'flagList' => $flagList, ]; } }