Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / EntityBundleCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\Console\Command\Generate\EntityBundleCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Symfony\Component\Console\Command\Command;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Command\Shared\ConfirmationTrait;
15 use Drupal\Console\Command\Shared\ModuleTrait;
16 use Drupal\Console\Command\Shared\ServicesTrait;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Generator\EntityBundleGenerator;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Utils\Validator;
22
23 class EntityBundleCommand extends Command
24 {
25     use CommandTrait;
26     use ModuleTrait;
27     use ServicesTrait;
28     use ConfirmationTrait;
29
30     /**
31      * @var Validator
32      */
33     protected $validator;
34
35     /**
36  * @var EntityBundleGenerator
37 */
38     protected $generator;
39
40     /**
41  * @var Manager
42 */
43     protected $extensionManager;
44
45     /**
46      * EntityBundleCommand constructor.
47      *
48      * @param Validator             $validator
49      * @param EntityBundleGenerator $generator
50      * @param Manager               $extensionManager
51      */
52     public function __construct(
53         Validator $validator,
54         EntityBundleGenerator $generator,
55         Manager $extensionManager
56     ) {
57         $this->validator = $validator;
58         $this->generator = $generator;
59         $this->extensionManager = $extensionManager;
60         parent::__construct();
61     }
62
63
64     protected function configure()
65     {
66         $this
67             ->setName('generate:entity:bundle')
68             ->setDescription($this->trans('commands.generate.entity.bundle.description'))
69             ->setHelp($this->trans('commands.generate.entity.bundle.help'))
70             ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
71             ->addOption(
72                 'bundle-name',
73                 null,
74                 InputOption::VALUE_OPTIONAL,
75                 $this->trans('commands.generate.entity.bundle.options.bundle-name')
76             )
77             ->addOption(
78                 'bundle-title',
79                 null,
80                 InputOption::VALUE_OPTIONAL,
81                 $this->trans('commands.generate.entity.bundle.options.bundle-title')
82             );
83     }
84
85     /**
86      * {@inheritdoc}
87      */
88     protected function execute(InputInterface $input, OutputInterface $output)
89     {
90         $io = new DrupalStyle($input, $output);
91
92         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
93         if (!$this->confirmGeneration($io)) {
94             return 1;
95         }
96
97         $module = $input->getOption('module');
98         $bundleName = $input->getOption('bundle-name');
99         $bundleTitle = $input->getOption('bundle-title');
100
101         $generator = $this->generator;
102         //TODO:
103         //        $generator->setLearning($learning);
104         $generator->generate($module, $bundleName, $bundleTitle);
105
106         return 0;
107     }
108
109     /**
110      * {@inheritdoc}
111      */
112     protected function interact(InputInterface $input, OutputInterface $output)
113     {
114         $io = new DrupalStyle($input, $output);
115
116         // --module option
117         $module = $input->getOption('module');
118         if (!$module) {
119             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
120             $module = $this->moduleQuestion($io);
121             $input->setOption('module', $module);
122         }
123
124         // --bundle-name option
125         $bundleName = $input->getOption('bundle-name');
126         if (!$bundleName) {
127             $bundleName = $io->ask(
128                 $this->trans('commands.generate.entity.bundle.questions.bundle-name'),
129                 'default',
130                 function ($bundleName) {
131                     return $this->validator->validateClassName($bundleName);
132                 }
133             );
134             $input->setOption('bundle-name', $bundleName);
135         }
136
137         // --bundle-title option
138         $bundleTitle = $input->getOption('bundle-title');
139         if (!$bundleTitle) {
140             $bundleTitle = $io->ask(
141                 $this->trans('commands.generate.entity.bundle.questions.bundle-title'),
142                 'default',
143                 function ($bundle_title) {
144                     return $this->validator->validateBundleTitle($bundle_title);
145                 }
146             );
147             $input->setOption('bundle-title', $bundleTitle);
148         }
149     }
150 }