setValidator([Utils::class, 'validateRequired']); $questions['machine_name'] = new Question('Module machine name'); $questions['machine_name']->setValidator([Utils::class, 'validateMachineName']); return $questions; } /** * Returns default questions for plugin generators. * * @return \Symfony\Component\Console\Question\Question[] * Array of default questions. */ public static function defaultPluginQuestions() { $questions = Utils::defaultQuestions(); $questions['plugin_label'] = new Question('Plugin label', 'Example'); $questions['plugin_label']->setValidator([Utils::class, 'validateRequired']); $questions['plugin_id'] = new Question('Plugin ID', [Utils::class, 'defaultPluginId']); $questions['plugin_id']->setValidator([Utils::class, 'validateMachineName']); return $questions; } /** * Returns extension root. * * @return string|bool * Extension root directory or false if it was not found. */ public static function getExtensionRoot($directory) { $extension_root = FALSE; for ($i = 1; $i <= 5; $i++) { $info_file = $directory . '/' . basename($directory) . '.info'; if ((file_exists($info_file) && basename($directory) !== 'drush') || file_exists($info_file . '.yml')) { $extension_root = $directory; break; } $directory = dirname($directory); } return $extension_root; } /** * Removes a given number of lines from the beginning of the string. */ public static function removeHeader($content, $header_size) { return implode("\n", array_slice(explode("\n", $content), $header_size)); } /** * Return the user's home directory. */ public static function getHomeDirectory() { return isset($_SERVER['HOME']) ? $_SERVER['HOME'] : getenv('HOME'); } /** * Replaces all tokens in a given string with appropriate values. * * @param string $text * A string potentially containing replaceable tokens. * @param array $data * An array where keys are token names and values are replacements. * * @return string * Text with tokens replaced. */ public static function tokenReplace($text, array $data) { $tokens = []; foreach ($data as $var_name => $var) { if (is_string($var)) { $tokens['{' . $var_name . '}'] = $var; } } return str_replace(array_keys($tokens), array_values($tokens), $text); } /** * Pluralizes a noun. * * @param string $string * A noun to pluralize. * * @return string * The pluralized noun. */ public static function pluralize($string) { switch (substr($string, -1)) { case 'y': return substr($string, 0, -1) . 'ies'; case 's': return $string . 'es'; default: return $string . 's'; } } /** * Prepares choices. * * @param array $raw_choices * The choices to be prepared. * * @return array * The prepared choices. */ public static function prepareChoices(array $raw_choices) { // The $raw_choices can be an associative array. $choices = array_values($raw_choices); // Start choices list form '1'. array_unshift($choices, NULL); unset($choices[0]); return $choices; } }