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