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