/** * Verifies that all required constants are defined. * Constants must be of the form: "%ENV%_%NAME%" */ private static function validate_config($command, $what, $env) { /** Get the required contstants from the dependency array. */ $deps = self::$config_dependencies; $required = $deps[$command]; if (!empty($what)) { $required = array_unique(array_merge($deps[$command][$what], $deps[$command]['global'])); } /** Get all definable constants. */ $all_const = array(); foreach ($deps as $comm_deps) { foreach ($comm_deps as $item) { $const = is_array($item) ? $item : array($item); $all_const = array_merge($all_const, $const); } } $all_const = array_unique($all_const); $get_const = function ($const) use($env) { return strtoupper($env . '_' . $const); }; $errors = array(); $constants = array(); foreach ($all_const as $short_name) { /** The constants template */ $constant = $get_const($short_name); if (in_array($short_name, $required) && !defined($constant)) { $errors[] = "Required constant {$constant} is not defined."; } elseif (defined($constant)) { $constants[$short_name] = constant($constant); } } if (count($errors)) { foreach ($errors as $error) { WP_Cli::line("{$error}"); } WP_Cli::error("The missing constants are required in order to run this subcommand.\nType `wp help deploy` for more information."); } /** Add the optional constants. */ foreach ($deps['optional'] as $optional) { $const = $get_const($optional); if (defined($const)) { $constants[$optional] = constant($const); } } return $constants; }
protected static function _prepare_and_extract($args) { $out = array(); self::$_env = $args[0]; $errors = self::_validate_config(); if ($errors !== true) { foreach ($errors as $error) { WP_Cli::error($error); } return false; } $out = self::config_constants_to_array(); $out['env'] = self::$_env; $out['db_user'] = escapeshellarg($out['db_user']); $out['db_host'] = escapeshellarg($out['db_host']); $out['db_password'] = escapeshellarg($out['db_password']); $out['ssh_port'] = isset($out['ssh_port']) ? intval($out['ssh_port']) : 22; $out['excludes'] = explode(':', $out['excludes']); return $out; }