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