3 namespace DrupalCodeGenerator\Command\Drupal_8\Service;
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;
12 * Implements d8:service:custom command.
14 class Custom extends BaseGenerator {
16 protected $name = 'd8:service:custom';
17 protected $description = 'Generates a custom Drupal service';
18 protected $alias = 'custom-service';
19 protected $label = 'Custom service';
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.');
33 $default_class = function ($vars) {
34 return Utils::camelize($vars['service_name']);
36 $questions['class'] = new Question('Class', $default_class);
38 $this->collectVars($input, $output, $questions);
41 ->path('src/{class}.php')
42 ->template('d8/service/custom.twig');
44 $this->addServicesFile()
45 ->template('d8/service/custom.services.twig');