Ejemplo n.º 1
0
 /**
  * 执行应用程序
  * @access public
  * @return void
  */
 public static function run()
 {
     self::init();
     // 实例化console
     $console = new Console('Think Console', '0.1');
     // 读取指令集
     if (is_file(APP_PATH . 'command' . EXT)) {
         $commands = (include APP_PATH . 'command' . EXT);
         if (is_array($commands)) {
             foreach ($commands as $command) {
                 if (class_exists($command) && is_subclass_of($command, "\\think\\console\\command\\Command")) {
                     // 注册指令
                     $console->add(new $command());
                 }
             }
         }
     }
     // 运行
     $console->run();
 }
Ejemplo n.º 2
0
 /**
  * 合并参数定义
  * @param bool $mergeArgs
  */
 public function mergeConsoleDefinition($mergeArgs = true)
 {
     if (null === $this->console || true === $this->consoleDefinitionMerged && ($this->consoleDefinitionMergedWithArgs || !$mergeArgs)) {
         return;
     }
     if ($mergeArgs) {
         $currentArguments = $this->definition->getArguments();
         $this->definition->setArguments($this->console->getDefinition()->getArguments());
         $this->definition->addArguments($currentArguments);
     }
     $this->definition->addOptions($this->console->getDefinition()->getOptions());
     $this->consoleDefinitionMerged = true;
     if ($mergeArgs) {
         $this->consoleDefinitionMergedWithArgs = true;
     }
 }
Ejemplo n.º 3
0
 /**
  * @param array $commands
  * @return array
  */
 private function sortCommands(array $commands)
 {
     $namespacedCommands = [];
     foreach ($commands as $name => $command) {
         $key = $this->console->extractNamespace($name, 1);
         if (!$key) {
             $key = '_global';
         }
         $namespacedCommands[$key][$name] = $command;
     }
     ksort($namespacedCommands);
     foreach ($namespacedCommands as &$commandsSet) {
         ksort($commandsSet);
     }
     // unset reference to keep scope clear
     unset($commandsSet);
     return $namespacedCommands;
 }
Ejemplo n.º 4
0
 /**
  * 描述控制台
  * @param Console $console
  * @param array   $options
  * @return string|mixed
  */
 protected function describeConsole(Console $console, array $options = [])
 {
     $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
     $description = new ConsoleDescription($console, $describedNamespace);
     if (isset($options['raw_text']) && $options['raw_text']) {
         $width = $this->getColumnWidth($description->getCommands());
         foreach ($description->getCommands() as $command) {
             $this->writeText(sprintf("%-{$width}s %s", $command->getName(), $command->getDescription()), $options);
             $this->writeText("\n");
         }
     } else {
         if ('' != ($help = $console->getHelp())) {
             $this->writeText("{$help}\n\n", $options);
         }
         $this->writeText("<comment>Usage:</comment>\n", $options);
         $this->writeText("  command [options] [arguments]\n\n", $options);
         $this->describeInputDefinition(new InputDefinition($console->getDefinition()->getOptions()), $options);
         $this->writeText("\n");
         $this->writeText("\n");
         $width = $this->getColumnWidth($description->getCommands());
         if ($describedNamespace) {
             $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
         } else {
             $this->writeText('<comment>Available commands:</comment>', $options);
         }
         // add commands by namespace
         foreach ($description->getNamespaces() as $namespace) {
             if (!$describedNamespace && ConsoleDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
                 $this->writeText("\n");
                 $this->writeText(' <comment>' . $namespace['id'] . '</comment>', $options);
             }
             foreach ($namespace['commands'] as $name) {
                 $this->writeText("\n");
                 $spacingWidth = $width - strlen($name);
                 $this->writeText(sprintf("  <info>%s</info>%s%s", $name, str_repeat(' ', $spacingWidth), $description->getCommand($name)->getDescription()), $options);
             }
         }
         $this->writeText("\n");
     }
 }