/** * Update a single config option based on a change in composer.json * @param {String} the name of the option to be changed * @param {String} the new value of the option to be changed */ public static function updateConfigOption($optionName, $optionValue) { if (is_string($optionValue) && strpos($optionValue, "<prompt>") !== false) { // prompt for input using the supplied query $prompt = str_replace("</prompt>", "", str_replace("<prompt>", "", $optionValue)); $options = ""; $input = Console::promptInput($prompt, $options, false); self::writeUpdateConfigOption($optionName, $input); Console::writeTag("ok", "config option " . $optionName . " updated...", false, true); } else { if (!isset(self::$options[$optionName]) || self::$options["overrideConfig"] == "a") { // if the option isn't set or the config is always to override update the config self::writeUpdateConfigOption($optionName, $optionValue); } else { if (self::$options["overrideConfig"] == "q") { // standardize the values for comparison $currentOptionValue = is_array(self::$options[$optionName]) ? implode(", ", self::$options[$optionName]) : self::$options[$optionName]; $newOptionValue = is_array($optionValue) ? implode(", ", $optionValue) : $optionValue; if ($currentOptionValue != $newOptionValue) { // prompt for input $prompt = "update the config option <desc>" . $optionName . " (" . $currentOptionValue . ")</desc> with the value <desc>" . $newOptionValue . "</desc>?"; $options = "Y/n"; $input = Console::promptInput($prompt, $options); if ($input == "y") { self::writeUpdateConfigOption($optionName, $optionValue); Console::writeInfo("config option " . $optionName . " updated...", false, true); } else { Console::writeWarning("config option <desc>" . $optionName . "</desc> not updated...", false, true); } } } } } }
/** * Prompt the user for some input * @param {String} the text for the prompt * @param {String} the text for the options * @param {Boolean} if we should lowercase the input before sending it back * @param {String} the tag that should be used when drawing the content * * @return {String} trimmed input given by the user */ public static function promptInput($prompt = "", $options = "", $lowercase = true, $tag = "info") { // check prompt if (empty($prompt)) { Console::writeError("an input prompt requires prompt text..."); } // if there are suggested options add them if (!empty($options)) { $prompt .= " <options>" . $options . "</options> >"; } // make sure no end-of-line is added $prompt .= " <nophpeol>"; // open the terminal and wait for feedback $stdin = fopen("php://stdin", "r"); Console::writeTag($tag, $prompt); $input = trim(fgets($stdin)); fclose($stdin); // check to see if it should be lowercased before sending back return $lowercase ? strtolower($input) : $input; }
/** * Check to see if the path already exists. If it does prompt the user to double-check it should be overwritten * @param {String} the package name * @param {String} path to be checked * * @return {Boolean} if the path exists and should be overwritten */ protected static function pathExists($packageName, $path) { $fs = new Filesystem(); if ($fs->exists($path)) { // set-up a human readable prompt $humanReadablePath = str_replace(Config::getOption("baseDir"), "./", $path); // set if the prompt should fire $prompt = true; // are we checking a directory? if (is_dir($path)) { // see if the directory is essentially empty $files = scandir($path); foreach ($files as $key => $file) { $ignore = array("..", ".", ".gitkeep", "README", ".DS_Store"); $file = explode("/", $file); if (in_array($file[count($file) - 1], $ignore)) { unset($files[$key]); } } if (empty($files)) { $prompt = false; } } if ($prompt) { // prompt for input using the supplied query $prompt = "the path <path>" . $humanReadablePath . "</path> already exists. merge or replace with the contents of <path>" . $packageName . "</path> package?"; $options = "M/r"; $input = Console::promptInput($prompt, $options); if ($input == "m") { Console::writeTag("ok", "contents of <path>" . $humanReadablePath . "</path> have been merged with the package's content...", false, true); return false; } else { Console::writeWarning("contents of <path>" . $humanReadablePath . "</path> have been replaced by the package's content...", false, true); return true; } } return false; } return false; }
/** * Update a single config option based on a change in composer.json * @param {String} the name of the option to be changed * @param {String} the new value of the option to be changed * @param {Boolean} whether to force the update of the option */ public static function updateConfigOption($optionName, $optionValue, $force = false) { if (is_string($optionValue) && strpos($optionValue, "<prompt>") !== false) { // prompt for input using the supplied query $options = ""; $default = ""; $prompt = str_replace("</prompt>", "", str_replace("<prompt>", "", $optionValue)); if (strpos($prompt, "<default>") !== false) { $default = explode("<default>", $prompt); $default = explode("</default>", $default[1]); $default = $default[0]; } $input = Console::promptInput($prompt, $options, $default, false); self::writeUpdateConfigOption($optionName, $input); Console::writeTag("ok", "config option " . $optionName . " updated...", false, true); } else { if (!isset(self::$options[$optionName]) || self::$options["overrideConfig"] == "a" || $force) { // if the option isn't set or the config is always to override update the config self::writeUpdateConfigOption($optionName, $optionValue); } else { if (self::$options["overrideConfig"] == "q") { // standardize the values for comparison $currentOption = self::getOption($optionName); $currentOptionValue = $currentOption; $newOptionValue = $optionValue; $optionNameOutput = $optionName; // dive into plugins to do a proper comparison to if ($optionName == "plugins") { // replace the data in anticipation of it being used $optionValue = array_replace_recursive($currentOption, $newOptionValue); // get the key of the plugin that is being added/updated reset($newOptionValue); $newOptionKey = key($newOptionValue); if (!array_key_exists($newOptionKey, $currentOptionValue)) { // if the key doesn't exist just write out the new config and move on self::writeUpdateConfigOption($optionName, $optionValue); return; } else { // see if the existing configs for the plugin exists. if so just return with no changes if ($newOptionValue[$newOptionKey] == $currentOptionValue[$newOptionKey]) { return; } else { $optionNameOutput = $optionName . "." . $newOptionKey; } } } if ($currentOptionValue != $newOptionValue) { // prompt for input if (is_array($currentOptionValue)) { $prompt = "update the config option <desc>" . $optionNameOutput . "</desc> with the value from the package install?"; } else { $prompt = "update the config option <desc>" . $optionNameOutput . " (" . $currentOptionValue . ")</desc> with the value <desc>" . $newOptionValue . "</desc>?"; } $options = "Y/n"; $input = Console::promptInput($prompt, $options, "Y"); if ($input == "y") { // update the config option self::writeUpdateConfigOption($optionName, $optionValue); Console::writeInfo("config option " . $optionNameOutput . " updated...", false, true); } else { Console::writeWarning("config option <desc>" . $optionNameOutput . "</desc> not updated...", false, true); } } } } } }