Example #1
0
 /**
  * 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;
         }
     }
 }