get_configurator() public static method

Get the Configurator instance
public static get_configurator ( ) : Configurator
return WP_CLI\Configurator
 /**
  * Get config value from lint config.
  *
  * @param string $key
  *
  * @return mixed
  */
 private function get_lint_config($key)
 {
     $config = WP_CLI::get_configurator()->to_array();
     if (count($config) === 1 || !isset($config[1]['lint'])) {
         return;
     }
     if (!isset($config[1]['lint'][$key])) {
         return;
     }
     return $config[1]['lint'][$key];
 }
 /**
  * Get config value from download attachments config.
  *
  * @param  string $key
  *
  * @return mixed
  */
 private function get_config_value($key)
 {
     $config = WP_CLI::get_configurator()->to_array();
     if (count($config) === 1 || !isset($config[1]['media'])) {
         return;
     }
     if (!isset($config[1]['media']['restore']) || !isset($config[1]['media']['restore'][$key])) {
         return;
     }
     return $config[1]['media']['restore'][$key];
 }
Example #3
0
 function get_longdesc()
 {
     $binding = array();
     foreach (\WP_CLI::get_configurator()->get_spec() as $key => $details) {
         if (false === $details['runtime']) {
             continue;
         }
         if (isset($details['deprecated'])) {
             continue;
         }
         if (true === $details['runtime']) {
             $synopsis = "--[no-]{$key}";
         } else {
             $synopsis = "--{$key}" . $details['runtime'];
         }
         $binding['parameters'][] = array('synopsis' => $synopsis, 'desc' => $details['desc']);
     }
     return Utils\mustache_render('man-params.mustache', $binding);
 }
Example #4
0
 private function init_config()
 {
     $configurator = \WP_CLI::get_configurator();
     $this->global_config_path = self::get_global_config_path();
     $this->project_config_path = self::get_project_config_path();
     $configurator->merge_yml($this->global_config_path);
     $configurator->merge_yml($this->project_config_path);
     list($args, $assoc_args, $runtime_config) = $configurator->parse_args(array_slice($GLOBALS['argv'], 1));
     list($this->arguments, $this->assoc_args) = self::back_compat_conversions($args, $assoc_args);
     $configurator->merge_array($runtime_config);
     list($this->config, $this->extra_config) = $configurator->to_array();
 }
Example #5
0
 function __construct($line)
 {
     // TODO: properly parse single and double quotes
     $this->words = explode(' ', $line);
     // first word is always `wp`
     array_shift($this->words);
     // last word is either empty or an incomplete subcommand
     $this->cur_word = end($this->words);
     if ("" !== $this->cur_word && !preg_match("/^\\-/", $this->cur_word)) {
         array_pop($this->words);
     }
     $is_alias = false;
     $is_help = false;
     if (!empty($this->words[0]) && preg_match("/^@/", $this->words[0])) {
         array_shift($this->words);
         // `wp @al` is false, but `wp @all ` is true.
         if (count($this->words)) {
             $is_alias = true;
         }
     } elseif (!empty($this->words[0]) && 'help' === $this->words[0]) {
         array_shift($this->words);
         $is_help = true;
     }
     $r = $this->get_command($this->words);
     if (!is_array($r)) {
         return;
     }
     list($command, $args, $assoc_args) = $r;
     $spec = SynopsisParser::parse($command->get_synopsis());
     foreach ($spec as $arg) {
         if ($arg['type'] == 'positional' && $arg['name'] == 'file') {
             $this->add('<file> ');
             return;
         }
     }
     if ($command->can_have_subcommands()) {
         // add completion when command is `wp` and alias isn't set.
         if ("wp" === $command->get_name() && false === $is_alias && false == $is_help) {
             $aliases = \WP_CLI::get_configurator()->get_aliases();
             foreach ($aliases as $name => $_) {
                 $this->add("{$name} ");
             }
         }
         foreach ($command->get_subcommands() as $name => $_) {
             $this->add("{$name} ");
         }
     } else {
         foreach ($spec as $arg) {
             if (in_array($arg['type'], array('flag', 'assoc'))) {
                 if (isset($assoc_args[$arg['name']])) {
                     continue;
                 }
                 $opt = "--{$arg['name']}";
                 if ($arg['type'] == 'flag') {
                     $opt .= ' ';
                 } elseif (!$arg['value']['optional']) {
                     $opt .= '=';
                 }
                 $this->add($opt);
             }
         }
     }
 }
Example #6
0
 private function init_config()
 {
     $configurator = \WP_CLI::get_configurator();
     $argv = array_slice($GLOBALS['argv'], 1);
     if (!empty($argv[0]) && preg_match('#^@[A-Za-z0-9-_]+$#', $argv[0], $matches)) {
         $this->alias = array_shift($argv);
     } else {
         $this->alias = null;
     }
     $this->global_config_path = $this->get_global_config_path();
     $this->project_config_path = $this->get_project_config_path();
     $configurator->merge_yml($this->global_config_path);
     $config = $configurator->to_array();
     $this->_required_files['global'] = $config[0]['require'];
     $configurator->merge_yml($this->project_config_path);
     $config = $configurator->to_array();
     $this->_required_files['project'] = $config[0]['require'];
     list($args, $assoc_args, $runtime_config) = $configurator->parse_args($argv);
     list($this->arguments, $this->assoc_args) = self::back_compat_conversions($args, $assoc_args);
     $configurator->merge_array($runtime_config);
     list($this->config, $this->extra_config) = $configurator->to_array();
     $this->aliases = $configurator->get_aliases();
     $this->_required_files['runtime'] = $this->config['require'];
 }
Example #7
0
 /**
  * Dump the list of global parameters, as JSON or in var_export format.
  *
  * ## OPTIONS
  *
  * [--with-values]
  * : Display current values also.
  *
  * [--format=<format>]
  * : Accepted values: var_export, json. Default: json.
  *
  * @subcommand param-dump
  */
 function param_dump($_, $assoc_args)
 {
     $spec = \WP_CLI::get_configurator()->get_spec();
     if (\WP_CLI\Utils\get_flag_value($assoc_args, 'with-values')) {
         $config = \WP_CLI::get_configurator()->to_array();
         // Copy current config values to $spec
         foreach ($spec as $key => $value) {
             if (isset($config[0][$key])) {
                 $current = $config[0][$key];
             } else {
                 $current = NULL;
             }
             $spec[$key]['current'] = $current;
         }
     }
     if ('var_export' === \WP_CLI\Utils\get_flag_value($assoc_args, 'format')) {
         var_export($spec);
     } else {
         echo json_encode($spec);
     }
 }
Example #8
0
 protected function get_global_params($root_command = false)
 {
     $binding = array();
     $binding['root_command'] = $root_command;
     if (!$this->can_have_subcommands() || is_object($this->parent) && get_class($this->parent) == 'WP_CLI\\Dispatcher\\CompositeCommand') {
         $binding['is_subcommand'] = true;
     }
     foreach (\WP_CLI::get_configurator()->get_spec() as $key => $details) {
         if (false === $details['runtime']) {
             continue;
         }
         if (isset($details['deprecated'])) {
             continue;
         }
         if (isset($details['hidden'])) {
             continue;
         }
         if (true === $details['runtime']) {
             $synopsis = "--[no-]{$key}";
         } else {
             $synopsis = "--{$key}" . $details['runtime'];
         }
         $binding['parameters'][] = array('synopsis' => $synopsis, 'desc' => $details['desc']);
     }
     if ($this->get_subcommands()) {
         $binding['has_subcommands'] = true;
     }
     return Utils\mustache_render('man-params.mustache', $binding);
 }
Example #9
0
File: cli.php Project: nb/wp-cli
 /**
  * Dump the list of global parameters, as JSON.
  *
  * @subcommand param-dump
  */
 function param_dump()
 {
     echo json_encode(\WP_CLI::get_configurator()->get_spec());
 }
Example #10
0
 /**
  * Run a WP-CLI command.
  *
  * Launch a new child process, or run the command in the current process.
  * Optionally:
  * * Prevent halting script execution on error.
  * * Capture and return STDOUT, or full details about command execution.
  * * Parse JSON output if the command rendered it.
  *
  * ```
  * $options = array(
  *   'return'     => true,   // Return 'STDOUT'; use 'all' for full object.
  *   'parse'      => 'json', // Parse captured STDOUT to JSON array.
  *   'launch'     => false,  // Reuse the current process.
  *   'exit_error' => true,   // Halt script execution on error.
  * );
  * $plugins = WP_CLI::runcommand( 'plugin list --format=json', $options );
  * ```
  *
  * @access public
  * @category Execution
  *
  * @param string $command WP-CLI command to run, including arguments.
  * @param array  $options Configuration options for command execution.
  * @return mixed
  */
 public static function runcommand($command, $options = array())
 {
     $defaults = array('launch' => true, 'exit_error' => true, 'return' => false, 'parse' => false);
     $options = array_merge($defaults, $options);
     $launch = $options['launch'];
     $exit_error = $options['exit_error'];
     $return = $options['return'];
     $parse = $options['parse'];
     $retval = null;
     if ($launch) {
         if ($return) {
             $descriptors = array(0 => STDIN, 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
         } else {
             $descriptors = array(0 => STDIN, 1 => STDOUT, 2 => STDERR);
         }
         $php_bin = self::get_php_binary();
         $script_path = $GLOBALS['argv'][0];
         // Persist runtime arguments unless they've been specified otherwise.
         $configurator = \WP_CLI::get_configurator();
         $argv = array_slice($GLOBALS['argv'], 1);
         list($_, $_, $runtime_config) = $configurator->parse_args($argv);
         foreach ($runtime_config as $k => $v) {
             if (preg_match("|^--{$key}=?\$|", $command)) {
                 unset($runtime_config[$k]);
             }
         }
         $runtime_config = Utils\assoc_args_to_str($runtime_config);
         $runcommand = "{$php_bin} {$script_path} {$runtime_config} {$command}";
         $env_vars = array('HOME', 'WP_CLI_AUTO_CHECK_UPDATE_DAYS', 'WP_CLI_CACHE_DIR', 'WP_CLI_CONFIG_PATH', 'WP_CLI_DISABLE_AUTO_CHECK_UPDATE', 'WP_CLI_PACKAGES_DIR', 'WP_CLI_PHP_USED', 'WP_CLI_PHP', 'WP_CLI_STRICT_ARGS_MODE');
         $env = array();
         foreach ($env_vars as $var) {
             $env[$var] = getenv($var);
         }
         $proc = proc_open($runcommand, $descriptors, $pipes, getcwd(), $env);
         if ($return) {
             $stdout = stream_get_contents($pipes[1]);
             fclose($pipes[1]);
             $stderr = stream_get_contents($pipes[2]);
             fclose($pipes[2]);
         }
         $return_code = proc_close($proc);
         if (-1 == $return_code) {
             self::warning("Spawned process returned exit code -1, which could be caused by a custom compiled version of PHP that uses the --enable-sigchild option.");
         } else {
             if ($return_code && $exit_error) {
                 exit($return_code);
             }
         }
         if (true === $return || 'stdout' === $return) {
             $retval = trim($stdout);
         } else {
             if ('stderr' === $return) {
                 $retval = trim($stderr);
             } else {
                 if ('return_code' === $return) {
                     $retval = $return_code;
                 } else {
                     if ('all' === $return) {
                         $retval = (object) array('stdout' => trim($stdout), 'stderr' => trim($stderr), 'return_code' => $return_code);
                     }
                 }
             }
         }
     } else {
         $configurator = self::get_configurator();
         $argv = Utils\parse_str_to_argv($command);
         list($args, $assoc_args, $runtime_config) = $configurator->parse_args($argv);
         if ($return) {
             ob_start();
             $existing_logger = self::$logger;
             self::$logger = new WP_CLI\Loggers\Execution();
         }
         if (!$exit_error) {
             self::$capture_exit = true;
         }
         try {
             self::get_runner()->run_command($args, $assoc_args, array('back_compat_conversions' => true));
             $return_code = 0;
         } catch (ExitException $e) {
             $return_code = $e->getCode();
         }
         if ($return) {
             $execution_logger = self::$logger;
             self::$logger = $existing_logger;
             $stdout = trim(ob_get_clean());
             $stderr = $execution_logger->stderr;
             if (true === $return || 'stdout' === $return) {
                 $retval = trim($stdout);
             } else {
                 if ('stderr' === $return) {
                     $retval = trim($stderr);
                 } else {
                     if ('return_code' === $return) {
                         $retval = $return_code;
                     } else {
                         if ('all' === $return) {
                             $retval = (object) array('stdout' => trim($stdout), 'stderr' => trim($stderr), 'return_code' => $return_code);
                         }
                     }
                 }
             }
         }
         if (!$exit_error) {
             self::$capture_exit = false;
         }
     }
     if ((true === $return || 'stdout' === $return) && 'json' === $parse) {
         $retval = json_decode($retval, true);
     }
     return $retval;
 }
Example #11
0
 private function init_config()
 {
     $configurator = \WP_CLI::get_configurator();
     $argv = array_slice($GLOBALS['argv'], 1);
     if (!empty($argv[0]) && preg_match('#' . Configurator::ALIAS_REGEX . '#', $argv[0], $matches)) {
         $this->alias = array_shift($argv);
     } else {
         $this->alias = null;
     }
     $this->global_config_path = $this->get_global_config_path();
     $this->project_config_path = $this->get_project_config_path();
     $configurator->merge_yml($this->global_config_path, $this->alias);
     $config = $configurator->to_array();
     $this->_required_files['global'] = $config[0]['require'];
     $configurator->merge_yml($this->project_config_path, $this->alias);
     $config = $configurator->to_array();
     $this->_required_files['project'] = $config[0]['require'];
     list($args, $assoc_args, $runtime_config) = $configurator->parse_args($argv);
     list($this->arguments, $this->assoc_args) = self::back_compat_conversions($args, $assoc_args);
     $configurator->merge_array($runtime_config);
     list($this->config, $this->extra_config) = $configurator->to_array();
     $this->aliases = $configurator->get_aliases();
     if (count($this->aliases) && !isset($this->aliases['@all'])) {
         $this->aliases = array_reverse($this->aliases);
         $this->aliases['@all'] = 'Run command against every registered alias.';
         $this->aliases = array_reverse($this->aliases);
     }
     $this->_required_files['runtime'] = $this->config['require'];
 }