Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / Command / Drupal_8 / Service / Custom.php
1 <?php
2
3 namespace DrupalCodeGenerator\Command\Drupal_8\Service;
4
5 use DrupalCodeGenerator\Command\BaseGenerator;
6 use DrupalCodeGenerator\Utils;
7 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Output\OutputInterface;
9 use Symfony\Component\Console\Question\Question;
10
11 /**
12  * Implements d8:service:custom command.
13  */
14 class Custom extends BaseGenerator {
15
16   protected $name = 'd8:service:custom';
17   protected $description = 'Generates a custom Drupal service';
18   protected $alias = 'custom-service';
19   protected $label = 'Custom service';
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function interact(InputInterface $input, OutputInterface $output) {
25     $questions = Utils::defaultQuestions();
26     $questions['service_name'] = new Question('Service name', '{machine_name}.example');
27     $questions['service_name']->setValidator(function ($value) {
28       if (!preg_match('/^[a-z][a-z0-9_\.]*[a-z0-9]$/', $value)) {
29         throw new \UnexpectedValueException('The value is not correct service name.');
30       }
31       return $value;
32     });
33     $default_class = function ($vars) {
34       return Utils::camelize($vars['service_name']);
35     };
36     $questions['class'] = new Question('Class', $default_class);
37
38     $this->collectVars($input, $output, $questions);
39
40     $this->addFile()
41       ->path('src/{class}.php')
42       ->template('d8/service/custom.twig');
43
44     $this->addServicesFile()
45       ->template('d8/service/custom.services.twig');
46   }
47
48 }