7fdadeb60edd6a1c8d9cab965c84e36af9ccd287
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginViewsFieldCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\PluginViewsFieldCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Drupal\Console\Utils\Validator;
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\Generator\PluginViewsFieldGenerator;
15 use Drupal\Console\Command\Shared\ModuleTrait;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\Console\Core\Command\Command;
18 use Drupal\Console\Extension\Manager;
19 use Drupal\Console\Core\Utils\ChainQueue;
20 use Drupal\Console\Utils\Site;
21 use Drupal\Console\Core\Utils\StringConverter;
22
23 /**
24  * Class PluginViewsFieldCommand
25  *
26  * @package Drupal\Console\Command\Generate
27  */
28 class PluginViewsFieldCommand extends Command
29 {
30     use ModuleTrait;
31     use ConfirmationTrait;
32
33     /**
34      * @var Manager
35      */
36     protected $extensionManager;
37
38     /**
39      * @var PluginViewsFieldGenerator
40      */
41     protected $generator;
42
43     /**
44      * @var Site
45      */
46     protected $site;
47
48     /**
49      * @var StringConverter
50      */
51     protected $stringConverter;
52
53     /**
54      * @var Validator
55      */
56     protected $validator;
57
58     /**
59      * @var ChainQueue
60      */
61     protected $chainQueue;
62
63     /**
64      * PluginViewsFieldCommand constructor.
65      *
66      * @param Manager                   $extensionManager
67      * @param PluginViewsFieldGenerator $generator
68      * @param Site                      $site
69      * @param StringConverter           $stringConverter
70      * @param Validator                 $validator
71      * @param ChainQueue                $chainQueue
72      */
73     public function __construct(
74         Manager $extensionManager,
75         PluginViewsFieldGenerator $generator,
76         Site $site,
77         StringConverter $stringConverter,
78         Validator $validator,
79         ChainQueue $chainQueue
80     ) {
81         $this->extensionManager = $extensionManager;
82         $this->generator = $generator;
83         $this->site = $site;
84         $this->stringConverter = $stringConverter;
85         $this->validator = $validator;
86         $this->chainQueue = $chainQueue;
87         parent::__construct();
88     }
89
90     protected function configure()
91     {
92         $this
93             ->setName('generate:plugin:views:field')
94             ->setDescription($this->trans('commands.generate.plugin.views.field.description'))
95             ->setHelp($this->trans('commands.generate.plugin.views.field.help'))
96             ->addOption(
97                 'module',
98                 null,
99                 InputOption::VALUE_REQUIRED,
100                 $this->trans('commands.common.options.module')
101             )
102             ->addOption(
103                 'class',
104                 null,
105                 InputOption::VALUE_REQUIRED,
106                 $this->trans('commands.generate.plugin.views.field.options.class')
107             )
108             ->addOption(
109                 'title',
110                 null,
111                 InputOption::VALUE_OPTIONAL,
112                 $this->trans('commands.generate.plugin.views.field.options.title')
113             )
114             ->addOption(
115                 'description',
116                 null,
117                 InputOption::VALUE_OPTIONAL,
118                 $this->trans('commands.generate.plugin.views.field.options.description')
119             )
120             ->setAliases(['gpvf']);
121     }
122
123     /**
124      * {@inheritdoc}
125      */
126     protected function execute(InputInterface $input, OutputInterface $output)
127     {
128         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
129         if (!$this->confirmOperation()) {
130             return 1;
131         }
132
133         $module = $input->getOption('module');
134         $class_name = $this->validator->validateClassName($input->getOption('class'));
135         $class_machine_name = $this->stringConverter->camelCaseToUnderscore($class_name);
136         $title = $input->getOption('title');
137         $description = $input->getOption('description');
138
139         $this->generator->generate([
140             'module' => $module,
141             'class_machine_name' => $class_machine_name,
142             'class_name' => $class_name,
143             'title' => $title,
144             'description' => $description,
145         ]);
146
147         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
148
149         return 0;
150     }
151
152     protected function interact(InputInterface $input, OutputInterface $output)
153     {
154         // --module option
155         $this->getModuleOption();
156
157         // --class option
158         $class_name = $input->getOption('class');
159         if (!$class_name) {
160             $class_name = $this->getIo()->ask(
161                 $this->trans('commands.generate.plugin.views.field.questions.class'),
162                 'CustomViewsField',
163                 function ($class_name) {
164                     return $this->validator->validateClassName($class_name);
165                 }
166             );
167         }
168         $input->setOption('class', $class_name);
169
170         // --title option
171         $title = $input->getOption('title');
172         if (!$title) {
173             $title = $this->getIo()->ask(
174                 $this->trans('commands.generate.plugin.views.field.questions.title'),
175                 $this->stringConverter->camelCaseToHuman($class_name)
176             );
177             $input->setOption('title', $title);
178         }
179
180         // --description option
181         $description = $input->getOption('description');
182         if (!$description) {
183             $description = $this->getIo()->ask(
184                 $this->trans('commands.generate.plugin.views.field.questions.description'),
185                 $this->trans('commands.generate.plugin.views.field.questions.description_default')
186             );
187             $input->setOption('description', $description);
188         }
189     }
190 }