Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / Command / Drupal_8 / Service / Custom.php
index 786343395698b9ff8d6e350a8be7f76a03613349..77697bf1121f9a750dc95c99d28ca027ee73f59d 100644 (file)
@@ -6,6 +6,7 @@ use DrupalCodeGenerator\Command\BaseGenerator;
 use DrupalCodeGenerator\Utils;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Question\ConfirmationQuestion;
 use Symfony\Component\Console\Question\Question;
 
 /**
@@ -24,19 +25,22 @@ class Custom extends BaseGenerator {
   protected function interact(InputInterface $input, OutputInterface $output) {
     $questions = Utils::defaultQuestions();
     $questions['service_name'] = new Question('Service name', '{machine_name}.example');
-    $questions['service_name']->setValidator(function ($value) {
-      if (!preg_match('/^[a-z][a-z0-9_\.]*[a-z0-9]$/', $value)) {
-        throw new \UnexpectedValueException('The value is not correct service name.');
-      }
-      return $value;
-    });
+    $questions['service_name']->setValidator([Utils::class, 'validateServiceName']);
+
     $default_class = function ($vars) {
-      return Utils::camelize($vars['service_name']);
+      $service = preg_replace('/^' . $vars['machine_name'] . '/', '', $vars['service_name']);
+      return Utils::camelize($service);
     };
     $questions['class'] = new Question('Class', $default_class);
+    $questions['class']->setValidator([Utils::class, 'validateClassName']);
 
     $this->collectVars($input, $output, $questions);
 
+    $di_question = new ConfirmationQuestion('Would you like to inject dependencies?');
+    if ($this->ask($input, $output, $di_question)) {
+      $this->collectServices($input, $output);
+    }
+
     $this->addFile()
       ->path('src/{class}.php')
       ->template('d8/service/custom.twig');