Execute command

@param  array $argv Arguments
@return mixed
553671
public function execute(array $argv = [])
    {
        if (empty($argv)) {
            return $this->help();
        }

        $cmd = array_shift($argv);
        if (isset($this->commands[$cmd])) {
            return $this->commands[$cmd]->initialize($argv, $this);
        }

        return $this->help($argv);
    }
809752
public static function exec($args, $context)
    {
        $argc = count($args);
        $first = array_shift($args);
        $first = $context->resolve($first);
        switch ($argc) {
            case 1 :
                return $first;
            case 2 :
                if (is_array($first) && isset($first['operator']) && $first['operator'] == 'not') {
                    $operant = array_shift($args);
                    $operant = $context->resolve($operant);

                    return !($operant);
                }
            case 3 :
                list($op, $right) = $args;
                $right = $context->resolve($right);

                return call_user_func(array('Evaluator', $op['operator']), $first, $right);
            default:
                return false;
        }
    }
1364011
public function run(array $argv)
    {
        $this->setProgramName($argv[0]);

        $currentCommand = $this;

        // init application,
        // before parsing options, we have to known the registered commands.
        $currentCommand->init();

        // use getoption kit to parse application options
        $parser = new ContinuousOptionParser($currentCommand->getOptionCollection());

        // parse the first part options (options after script name)
        // option parser should stop before next command name.
        //
        //    $ app.php -v -d next
        //                  |
        //                  |->> parser
        //
        //
        $currentCommand->setOptions(
            $parser->parse($argv)
        );

        if (false === $currentCommand->prepare()) {
            return false;
        }


        $commandStack = [];
        $arguments = [];

        // build the command list from command line arguments
        while (!$parser->isEnd()) {
            $a = $parser->getCurrentArgument();

            // if current command is in subcommand list.
            if ($currentCommand->hasCommands()) {

                if (!$currentCommand->hasCommand($a)) {
                    if (!$this->getOption('no-interact') && ($guess = $currentCommand->guessCommand($a)) !== null) {
                        $a = $guess;
                    } else {
                        throw new CommandNotFoundException($currentCommand, $a);
                    }
                }

                $parser->advance(); // advance position

                // get command object of "$a"
                /** @var Command $nextCommand */
                $nextCommand = $currentCommand->getCommand($a);

                $parser->setSpecs($nextCommand->getOptionCollection());

                // parse the option result for command.
                $nextCommand->setOptions(
                    $parser->continueParse()
                );

                $commandStack[] = $currentCommand = $nextCommand; // save command object into the stack
            } else {
                $r = $parser->continueParse();

                if (count($r)) {
                    // get the option result and merge the new result
                    $currentCommand->getOptions()->merge($r);
                } else {
                    $a = $parser->advance();
                    $arguments[] = $a;
                }
            }
        }

        foreach ($commandStack as $cmd) {
            if (false === $cmd->prepare()) {
                return false;
            }
        }

        // get last command and run
        if ($lastCommand = array_pop($commandStack)) {

            $lastCommand->executeWrapper($arguments);
            $lastCommand->finish();
            while ($cmd = array_pop($commandStack)) {
                // call finish stage.. of every command.
                $cmd->finish();
            }

        } else {
            // no command specified.
            $this->executeWrapper($arguments);

            return true;
        }

        $currentCommand->finish();
        $this->finish();

        return true;
    }
23019
public function main($argv)
    {
        $this->addArguments();
        try {
            $this->cli->arguments->parse($argv);
        } catch (\Exception $e) {
            $this->cli->usage();
            return 0;
        }
        if ($this->cli->arguments->get('help')) {
            $this->cli->usage();
            return 0;
        }
        return $this->execute();
    }
819599
public function execute(array $args = [], array $options = [])
    {
        if (!$this->evaluate($this->condition)) {
            return;
        }

        $this->command->execute($args, $options);
    }
1025555
public function execute($code,$args=array()) {
        $this->executeTimes++;
        N('db_write',1); // 兼容代码
        $this->debug(true);
        $this->queryStr = 'execute:'.$code;
        $result   = $this->_mongo->execute($code,$args);
        $this->debug(false);
        if($result['ok']) {
            return $result['retval'];
        }else{
            E($result['errmsg']);
        }
    }
646558
protected function executeCommand()
    {
        static $methods = [
            'HEAD',
            'GET',
            'POST',
            'PUT',
            'DELETE'
        ];

        \Akamai\Open\EdgeGrid\Client::setDebug(true);
        \Akamai\Open\EdgeGrid\Client::setVerbose(true);

        $args = $this->climate->arguments->all();
        $client = new Client();

        if ($this->climate->arguments->defined('auth-type')) {
            $auth = $this->climate->arguments->get('auth');
            if ($this->climate->arguments->get('auth-type') === 'edgegrid' ||
                (!$this->climate->arguments->defined('auth-type'))) {
                $section = 'default';
                if ($this->climate->arguments->defined('auth')) {
                    $section = (substr($auth, -1) === ':') ? substr($auth, 0, -1) : $auth;
                }
                $client = Client::createFromEdgeRcFile($section);
            }

            if (in_array($this->climate->arguments->get('auth-type'), ['basic', 'digest'])) {
                if (!$this->climate->arguments->defined('auth') || $this->climate->arguments->get('auth') === null) {
                    $this->help();
                    return;
                }

                $auth = [
                    $auth,
                    null,
                    $this->climate->arguments->get('auth-type')
                ];

                if (strpos(':', $auth[0]) !== false) {
                    list($auth[0], $auth[1]) = explode(':', $auth[0]);
                }

                $client = new Client(['auth' => $auth]);
            }
        }

        $method = 'GET';
        $options = [];
        $body = [];

        foreach ($args as $arg) {
            $value = $arg->value();
            if (empty($value) || is_bool($value) || $arg->longPrefix()) {
                continue;
            }

            if (in_array(strtoupper($value), $methods)) {
                $method = $arg->value();
                continue;
            }

            if (!isset($url) && preg_match('@^(http(s?)://|:).*$@', trim($value))) {
                $url = $value;

                if ($url{0} === ':') {
                    $url = substr($url, 1);
                }

                continue;
            }

            $matches = [];
            if (preg_match('/^(?<key>.*?):=(?<file>@?)(?<value>.*?)$/', $value, $matches)) {
                if (!$value = $this->getArgValue($matches)) {
                    return false;
                }

                $body[$matches['key']] = json_decode($value);
                continue;
            }

            if (preg_match('/^(?<header>.*?):(?<value>.*?)$/', $value, $matches)
                && !preg_match('@^http(s?)://@', $value)) {
                $options['headers'][$matches['header']] = $matches['value'];
                continue;
            }

            if (preg_match('/^(?<key>.*?)=(?<file>@?)(?<value>.*?)$/', $value, $matches)) {
                if (!$value = $this->getArgValue($matches)) {
                    return false;
                }

                $body[$matches['key']] = $matches['value'];
                continue;
            }

            if (!isset($url)) {
                $url = $value;
                continue;
            }

            $this->help();
            $this->climate->error('Unknown argument: ' . $value);

            return false;
        }

        $stdin = '';
        $fp = fopen('php://stdin', 'rb');
        if ($fp) {
            stream_set_blocking($fp, false);
            $stdin = fgets($fp);
            if (!empty(trim($stdin))) {
                while (!feof($fp)) {
                    $stdin .= fgets($fp);
                }
                fclose($fp);
            }
            $stdin = rtrim($stdin);
        }

        if (!empty($stdin) && !empty($body)) {
            $this->help();
            $this->climate->error(
                'error: Request body (from stdin or a file) and request data (key=value) cannot be mixed.'
            );
            return;
        }

        if (!empty($stdin)) {
            $body = $stdin;
        }

        if (count($body) && !$this->climate->arguments->defined('form')) {
            if (!isset($options['headers']['Content-Type'])) {
                $options['headers']['Content-Type'] = 'application/json';
            }
            if (!isset($options['headers']['Accept'])) {
                $options['headers']['Accept'] = 'application/json';
            }
            $options['body'] = (!is_string($body)) ? json_encode($body) : $body;
        }

        if (count($body) && $this->climate->arguments->defined('form')) {
            if (!isset($options['headers']['Content-Type'])) {
                $options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
            }

            $options['body'] = (!is_string($body)) ? http_build_query($body, null, null, PHP_QUERY_RFC1738) : $body;
        }

        $options['allow_redirects'] = false;
        if ($this->climate->arguments->defined('follow')) {
            $options['allow_redirects'] = true;
        }

        return $client->request($method, $url, $options);
    }
714700
public function handle(array $args = [])
    {
        // remove source file name from argv
        $source = array_shift($args);

        /** @var ConsoleEvent $applicationEvent */
        $applicationEvent = $this->getApplicationEvent();
        $applicationEvent->setName(self::EVENT_REQUEST_RECEIVED);
        $applicationEvent->setSourceFile($source);
        $applicationEvent->setArguments($args);
        $this->emit($applicationEvent);

        // init dispatcher
        $dispatcher = new Dispatcher($this->commands, $this->container);

        $middlewareRunner = new MiddlewareRunner($this->getMiddlewares());

        $middlewareRunner->run([$applicationEvent->getArguments()], function ($args) use ($applicationEvent, $dispatcher) {
            // dispatch command with args from cli
            $dispatcher->dispatch($args);
        });
    }
751389
protected function executeCommand($args)
    {
        $io = clone $this->io;

        $io->setArguments($args);
        $io->setOption('no-backup', true);
        $io->setOption('no-create-database', true);

//        foreach ($this->io->getOptions() as $k => $v)
//        {
//            $io->setOption($k, $v);
//        }

        return $this->console->getRootCommand()->setIO($io)->execute();
    }
539939
public function run(array $args)
    {
        print PHP_EOL;

        $command = 'help';

        //as in 'eve generate'
        if(isset($args[1])) {
            $command = $args[1];
        }

        $class = '\\Eve\\Framework\\Cli\\'.ucwords($command);

        if(!class_exists($class)) {
            self::error('No such command `'.$command.'` found.');
        }

        array_shift($args);
        $runner = new $class($this->cwd);

        $results = $runner->run($args);

        print PHP_EOL;

        return $results;
    }
32767
public function getArgs(int $argc, array $argv)
    {
        if ($argc == 1) {
            Output::displayAsGreen(
                "Welcome to the Framework Console Manager\n".
                "I noticed that you didn't enter any parameters.\n".
                "For more information, you can use the help command to get a command list."
            );
        }

        $c = $this->searchCommand($argv[1]);
        if (empty($c)) {
            CoreManager::setCurrentModule("Error");
            $predict = (PredictionManager::parsePredict($argv[1]));
            if (empty($predict)) {
                Output::displayAsError("Command not found.\n".
                    "For more information, you can use the [help] command to get a command list.");
            } else {
                Output::displayAsError("Input [".$argv[1]."] is ambigus.\n".
                    "To get command list, use [help] keyword.\n\n".
                    PredictionManager::structMessage($predict));
            }
        }

        $p = ParseManager::parse($argv);
        $ref = new Reflex();
        $ref->__simple($c['class'], $p);
    }
1151407
protected function executeDirective($directive, $context, $argv)
    {
        if (!$this->isRegistered($directive)) {
            throw new \RuntimeException(sprintf(
                "Undefined Directive \"%s\" in %s", $directive, $this->source
            ));
        }

        $callback = $this->directives[$directive];

        array_unshift($argv, $context);

        return call_user_func_array($callback, $argv);
    }
223427
public function executeCommand(array $args, OutputInterface $output, $message)
    {
        $name = $args[0];
        $cmd = $this->getCmd($args);

        $output->writeln('');
        $output->writeln(
            sprintf(
                '<info>Running %s</info>',
                $name
            )
        );

        $output->writeln(
            sprintf(
                '<comment>%s</comment>',
                $cmd
            )
        );

        $this->process->setCommandLine($cmd);
        $this->process->run(
            function ($type, $buffer) use ($output, $cmd) {
                if (Process::ERR === $type) {
                    $output->writeln(
                        sprintf(
                            '<error>%s</error>',
                            $buffer
                        )
                    );
                } else {
                    $output->writeln(
                        sprintf(
                            '<comment>%s</comment>',
                            $buffer
                        )
                    );
                }
            }
        );

        if (!$this->process->isSuccessful()) {
            throw new \RuntimeException($message . '<error>' . $this->process->getErrorOutput() . '</error>');
        }
    }
398194
protected function execute($command, array $arguments = array(), $cwd = null)
    {
        if (null === $cwd) {
            $cwd = $this->cwd;
        }

        return $this->adapter->execute($command, $arguments, $cwd);
    }
268987
public function execute($command, array $arguments = array(), $cwd = null)
    {
        $arguments += $this->getGlobalArguments();

        try {
            $result = $this->adapter->execute($command, $arguments, $cwd);
        } catch (\Exception $e) {
            // @todo move to a generic error handler? Something similar to the ParserInterface
            if (preg_match('/url \'[^\']+\' non-existent/i', $e->getMessage()) ||
                preg_match('/file not found/i', $e->getMessage()) ||
                preg_match('/path not found/i', $e->getMessage())
            ) {
                throw new NotFoundException($e->getMessage());
            }

            throw $e;
        }

        return $result;
    }
773802
public function run($argc, $argv)
    {
        $this->addDefaultCommands();

        // @todo place better command parser here
        if ($argc == 1) {
            $run = $this->getDefaultCommand();
            $argv = [];
        } else {
            $run = $argv[1];
            $argv = array_slice($argv, 2);
        }

        $found = false;

        /** @var CommandInterface $command */
        foreach ($this->commands as $command) {
            // @todo prevent multiple commands having the same name (why?)
            if ($command->getName() == $run) {
                $command->run([], $argv);
                $found = true;
            }
        }

        // @todo more graceful end
        if (!$found) {
            echo "command unknown\n";
            // exit with error
            exit(1);
        }

        // this is running the react event loop, maybe this should run the actual commands too?
        $this->loop->run();
    }
289368
protected function runCommand(Command $command, array $argv, ConsoleIo $io)
    {
        try {
            return $command->run($argv, $io);
        } catch (StopException $e) {
            return $e->getCode();
        }
    }