Version 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', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
92             ->addOption(
93                 'class',
94                 '',
95                 InputOption::VALUE_REQUIRED,
96                 $this->trans('commands.generate.plugin.views.field.options.class')
97             )
98             ->addOption(
99                 'title',
100                 '',
101                 InputOption::VALUE_OPTIONAL,
102                 $this->trans('commands.generate.plugin.views.field.options.title')
103             )
104             ->addOption(
105                 'description',
106                 '',
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;
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
135     protected function interact(InputInterface $input, OutputInterface $output)
136     {
137         $io = new DrupalStyle($input, $output);
138
139         // --module option
140         $module = $input->getOption('module');
141         if (!$module) {
142             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
143             $module = $this->moduleQuestion($io);
144             $input->setOption('module', $module);
145         }
146
147         // --class option
148         $class_name = $input->getOption('class');
149         if (!$class_name) {
150             $class_name = $io->ask(
151                 $this->trans('commands.generate.plugin.views.field.questions.class'),
152                 'CustomViewsField'
153             );
154         }
155         $input->setOption('class', $class_name);
156
157         // --title option
158         $title = $input->getOption('title');
159         if (!$title) {
160             $title = $io->ask(
161                 $this->trans('commands.generate.plugin.views.field.questions.title'),
162                 $this->stringConverter->camelCaseToHuman($class_name)
163             );
164             $input->setOption('title', $title);
165         }
166
167         // --description option
168         $description = $input->getOption('description');
169         if (!$description) {
170             $description = $io->ask(
171                 $this->trans('commands.generate.plugin.views.field.questions.description'),
172                 $this->trans('commands.generate.plugin.views.field.questions.description_default')
173             );
174             $input->setOption('description', $description);
175         }
176     }
177
178     protected function createGenerator()
179     {
180         return new PluginViewsFieldGenerator();
181     }
182 }