diff vendor/chi-teck/drupal-code-generator/src/Utils.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
line wrap: on
line diff
--- a/vendor/chi-teck/drupal-code-generator/src/Utils.php	Thu Feb 28 13:11:55 2019 +0000
+++ b/vendor/chi-teck/drupal-code-generator/src/Utils.php	Thu May 09 15:34:47 2019 +0100
@@ -9,6 +9,8 @@
  */
 class Utils {
 
+  use LegacyUtilsTrait;
+
   /**
    * Creates default plugin ID.
    */
@@ -99,46 +101,32 @@
   }
 
   /**
-   * Returns normalized file path.
+   * Returns a validator for allowed options.
    *
-   * @codeCoverageIgnore
-   * @deprecated
+   * @param array $options
+   *   Allowed values.
+   *
+   * @return callable
+   *   Question validator.
    */
-  public static function normalizePath($path) {
-    $parts = [];
-    $path = str_replace('\\', '/', $path);
-    $path = preg_replace('/\/+/', '/', $path);
-    $segments = explode('/', $path);
-    foreach ($segments as $segment) {
-      if ($segment != '.') {
-        $test = array_pop($parts);
-        if (is_null($test)) {
-          $parts[] = $segment;
-        }
-        elseif ($segment == '..') {
-          if ($test == '..') {
-            $parts[] = $test;
-          }
-          if ($test == '..' || $test == '') {
-            $parts[] = $segment;
-          }
-        }
-        else {
-          $parts[] = $test;
-          $parts[] = $segment;
-        }
+  public static function getOptionsValidator(array $options) {
+    return function ($value) use ($options) {
+      if (!in_array($value, $options)) {
+        $options_formatted = implode(', ', $options);
+        $error_message = sprintf('The value should be one of the following: %s.', $options_formatted);
+        throw new \UnexpectedValueException($error_message);
       }
-    }
-    return implode('/', $parts);
+      return $value;
+    };
   }
 
   /**
    * Returns default questions for module generators.
    *
    * @return \Symfony\Component\Console\Question\Question[]
-   *   Array of default questions.
+   *   Array of module questions.
    */
-  public static function defaultQuestions() {
+  public static function moduleQuestions() {
     $questions['name'] = new Question('Module name');
     $questions['name']->setValidator([Utils::class, 'validateRequired']);
     $questions['machine_name'] = new Question('Module machine name');
@@ -150,18 +138,29 @@
    * Returns default questions for plugin generators.
    *
    * @return \Symfony\Component\Console\Question\Question[]
-   *   Array of default questions.
+   *   Array of plugin questions.
    */
-  public static function defaultPluginQuestions() {
-    $questions = Utils::defaultQuestions();
+  public static function pluginQuestions($class_suffix = '') {
     $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']);
+    $questions['class'] = static::pluginClassQuestion($class_suffix);
     return $questions;
   }
 
   /**
+   * Creates plugin class question.
+   */
+  public static function pluginClassQuestion($suffix = '') {
+    $default_class = function ($vars) use ($suffix) {
+      $unprefixed_plugin_id = preg_replace('/^' . $vars['machine_name'] . '_/', '', $vars['plugin_id']);
+      return Utils::camelize($unprefixed_plugin_id) . $suffix;
+    };
+    return new Question('Plugin class', $default_class);
+  }
+
+  /**
    * Returns extension root.
    *
    * @return string|bool
@@ -205,7 +204,7 @@
    * @return string
    *   Text with tokens replaced.
    */
-  public static function tokenReplace($text, array $data) {
+  public static function replaceTokens($text, array $data) {
     $tokens = [];
     foreach ($data as $var_name => $var) {
       if (is_string($var)) {