ManaPHP\Cli\Application::handle PHP Method

handle() public method

public handle ( array $args = null ) : integer
$args array
return integer
    public function handle($args = null)
    {
        $this->_args = $args ?: $GLOBALS['argv'];
        $command = count($this->_args) === 1 ? null : $this->_args[1];
        if (!$this->cliRouter->route($command)) {
            $this->console->writeLn('command name is invalid: ' . $command);
            return 1;
        }
        $controllerName = $this->cliRouter->getControllerName();
        $actionName = lcfirst($this->cliRouter->getActionName());
        $this->console->writeLn('executed command is `' . Text::underscore($controllerName) . ':' . Text::underscore($actionName) . '`');
        $controllerClassName = null;
        foreach ([$this->alias->resolve('@ns.app\\Cli\\Controllers\\' . $controllerName . 'Controller'), 'ManaPHP\\Cli\\Controllers\\' . $controllerName . 'Controller'] as $class) {
            if ($this->_dependencyInjector->has($class) || class_exists($class)) {
                $controllerClassName = $class;
            }
        }
        if (!$controllerClassName) {
            $this->console->writeLn('``:command` command is not exists', ['command' => lcfirst($controllerName) . ':' . $actionName]);
            return 1;
        }
        $controllerInstance = $this->_dependencyInjector->getShared($controllerClassName);
        $actionMethod = $actionName . 'Command';
        if (!method_exists($controllerInstance, $actionMethod)) {
            $this->console->writeLn('`:command` sub command is not exists', ['command' => lcfirst($controllerName) . ':' . $actionName]);
            return 1;
        }
        $r = $controllerInstance->{$actionMethod}();
        return is_int($r) ? $r : 0;
    }