think\Console::add PHP Méthode

add() public méthode

添加一个指令
public add ( Command $command ) : Command
$command think\console\Command
Résultat think\console\Command
    public function add(Command $command)
    {
        $command->setConsole($this);
        if (!$command->isEnabled()) {
            $command->setConsole(null);
            return null;
        }
        if (null === $command->getDefinition()) {
            throw new \LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
        }
        $this->commands[$command->getName()] = $command;
        foreach ($command->getAliases() as $alias) {
            $this->commands[$alias] = $command;
        }
        return $command;
    }

Usage Example

Exemple #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();
 }