/** * Returns all available commands. * * @return array<\wcf\system\cli\command\ICLICommand> */ public static function getCommands() { if (empty(self::$commands)) { $directory = DirectoryUtil::getInstance(WCF_DIR . 'lib/system/cli/command/'); $commands = $directory->getFiles(SORT_ASC, new Regex('Command\\.class\\.php$')); foreach ($commands as $command) { $class = 'wcf\\system\\cli\\command\\' . basename($command, '.class.php'); if (!class_exists($class) && !interface_exists($class)) { Log::info('Invalid command file: ', $command); continue; } if (!class_exists($class)) { continue; } $object = new $class(); if (!$object instanceof ICLICommand) { Log::info('Invalid command file: ', $command); continue; } if (!$object->canAccess()) { continue; } self::$commands[strtolower(basename($command, 'CLICommand.class.php'))] = $object; } } return self::$commands; }
/** * Displays an error message. * * @param string $name * @param array $parameters */ public function error($name, array $parameters = array()) { Log::error('package.' . $name . ':' . JSON::encode($parameters)); if ($parameters) { throw new ArgvException(CLIWCF::getLanguage()->getDynamicVariable('wcf.acp.package.error.' . $name, $parameters), $this->getUsage()); } else { throw new ArgvException(CLIWCF::getLanguage()->get('wcf.acp.package.error.' . $name), $this->argv->getUsageMessage()); } }
/** * Returns an array with the list of all available workers. * * @return array */ public function generateList() { $directory = DirectoryUtil::getInstance(WCF_DIR . 'lib/system/worker/'); $workerList = $directory->getFiles(SORT_ASC, new Regex('Worker\\.class\\.php$')); $table = array(array('Class', 'Description')); foreach ($workerList as $worker) { $class = 'wcf\\system\\worker\\' . basename($worker, '.class.php'); if (!class_exists($class) && !interface_exists($class)) { Log::info('Invalid worker file: ', $worker); continue; } $reflection = new \ReflectionClass($class); if (!$reflection->isInstantiable()) { continue; } if (!ClassUtil::isInstanceOf($class, 'wcf\\system\\worker\\IWorker')) { Log::info('Invalid worker file: ', $worker); continue; } $docComment = explode("\n", StringUtil::unifyNewlines($reflection->getDocComment())); foreach ($docComment as $commentLine) { if (Regex::compile('[a-z]', Regex::CASE_INSENSITIVE)->match($commentLine)) { $comment = Regex::compile('^[^a-z]+', Regex::CASE_INSENSITIVE)->replace($commentLine, ''); break; } } $table[] = array(basename($worker, '.class.php'), $comment); } return $table; }
/** * Initializes command handling. */ protected function initCommands() { // add command name completer self::getReader()->addCompleter(new CLICommandNameCompleter()); while (true) { // roll back open transactions of the previous command, as they are dangerous in a long living script if (WCF::getDB()->rollBackTransaction()) { Log::warn('Previous command had an open transaction.'); } self::getReader()->setHistoryEnabled(true); $line = self::getReader()->readLine('>'); if ($line === null) { exit; } $line = StringUtil::trim($line); try { $command = CLICommandHandler::getCommand($line); $command->execute(CLICommandHandler::getParameters($line)); } catch (IllegalLinkException $e) { Log::error('notFound:' . JSON::encode(array('command' => $line))); self::getReader()->println(WCF::getLanguage()->getDynamicVariable('wcf.cli.error.command.notFound', array('command' => $line))); if (self::getArgvParser()->exitOnFail) { exit(1); } continue; } catch (PermissionDeniedException $e) { Log::error('permissionDenied'); self::getReader()->println(WCF::getLanguage()->getDynamicVariable('wcf.global.error.permissionDenied')); if (self::getArgvParser()->exitOnFail) { exit(1); } continue; } catch (ArgvException $e) { // show error message and usage if ($e->getMessage()) { echo $e->getMessage() . PHP_EOL; } echo $e->getUsageMessage(); if (self::getArgvParser()->exitOnFail) { exit(1); } continue; } catch (\Exception $e) { Log::error($e); if (self::getArgvParser()->exitOnFail) { exit(1); } continue; } } }