Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PostUpdateCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\PostUpdateCommand.
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\PostUpdateGenerator;
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\Utils\Validator;
23
24 /**
25  * Class PostUpdateCommand
26  *
27  * @package Drupal\Console\Command\Generate
28  */
29 class PostUpdateCommand extends Command
30 {
31     use ModuleTrait;
32     use ConfirmationTrait;
33     use CommandTrait;
34
35     /**
36  * @var Manager
37 */
38     protected $extensionManager;
39
40     /**
41  * @var PostUpdateGenerator
42 */
43     protected $generator;
44
45     /**
46      * @var Site
47      */
48     protected $site;
49
50     /**
51  * @var Validator
52 */
53     protected $validator;
54
55     /**
56      * @var ChainQueue
57      */
58     protected $chainQueue;
59
60     /**
61      * PostUpdateCommand constructor.
62      *
63      * @param Manager             $extensionManager
64      * @param PostUpdateGenerator $generator
65      * @param Site                $site
66      * @param Validator           $validator
67      * @param ChainQueue          $chainQueue
68      */
69     public function __construct(
70         Manager $extensionManager,
71         PostUpdateGenerator $generator,
72         Site $site,
73         Validator $validator,
74         ChainQueue $chainQueue
75     ) {
76         $this->extensionManager = $extensionManager;
77         $this->generator = $generator;
78         $this->site = $site;
79         $this->validator = $validator;
80         $this->chainQueue = $chainQueue;
81         parent::__construct();
82     }
83
84     protected function configure()
85     {
86         $this
87             ->setName('generate:post:update')
88             ->setDescription($this->trans('commands.generate.post:update.description'))
89             ->setHelp($this->trans('commands.generate.post.update.help'))
90             ->addOption(
91                 'module',
92                 null,
93                 InputOption::VALUE_REQUIRED,
94                 $this->trans('commands.common.options.module')
95             )
96             ->addOption(
97                 'post-update-name',
98                 null,
99                 InputOption::VALUE_REQUIRED,
100                 $this->trans('commands.generate.post.update.options.post-update-name')
101             );
102     }
103
104     /**
105      * {@inheritdoc}
106      */
107     protected function execute(InputInterface $input, OutputInterface $output)
108     {
109         $io = new DrupalStyle($input, $output);
110
111         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
112         if (!$this->confirmGeneration($io)) {
113             return 1;
114         }
115
116         $module = $input->getOption('module');
117         $postUpdateName = $input->getOption('post-update-name');
118
119         $this->validatePostUpdateName($module, $postUpdateName);
120
121         $this->generator->generate($module, $postUpdateName);
122
123         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
124
125         return 0;
126     }
127
128     protected function interact(InputInterface $input, OutputInterface $output)
129     {
130         $io = new DrupalStyle($input, $output);
131
132         $this->site->loadLegacyFile('/core/includes/update.inc');
133         $this->site->loadLegacyFile('/core/includes/schema.inc');
134
135         $module = $input->getOption('module');
136         if (!$module) {
137             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
138             $module = $this->moduleQuestion($io);
139             $input->setOption('module', $module);
140         }
141
142         $postUpdateName = $input->getOption('post-update-name');
143         if (!$postUpdateName) {
144             $postUpdateName = $io->ask(
145                 $this->trans('commands.generate.post.update.questions.post-update-name'),
146                 '',
147                 function ($postUpdateName) {
148                     return $this->validator->validateSpaces($postUpdateName);
149                 }
150             );
151
152             $input->setOption('post-update-name', $postUpdateName);
153         }
154     }
155
156
157     protected function createGenerator()
158     {
159         return new PostUpdateGenerator();
160     }
161
162     protected function getLastUpdate($module)
163     {
164         $this->site->loadLegacyFile('/core/includes/update.inc');
165         $this->site->loadLegacyFile('/core/includes/schema.inc');
166
167         $updates = update_get_update_list();
168
169         if (empty($updates[$module]['pending'])) {
170             $lastUpdateSchema = drupal_get_schema_versions($module);
171         } else {
172             $lastUpdateSchema = reset(array_keys($updates[$module]['pending'], max($updates[$module]['pending'])));
173         }
174
175         return $lastUpdateSchema;
176     }
177
178     protected function validatePostUpdateName($module, $postUpdateName)
179     {
180         if (!$this->validator->validateSpaces($postUpdateName)) {
181             throw new \InvalidArgumentException(
182                 sprintf(
183                     $this->trans('commands.generate.post.update.messages.wrong-post-update-name'),
184                     $postUpdateName
185                 )
186             );
187         }
188
189         if ($this->extensionManager->validateModuleFunctionExist($module, $module . '_post_update_' . $postUpdateName, $module . '.post_update.php')) {
190             throw new \InvalidArgumentException(
191                 sprintf(
192                     $this->trans('commands.generate.post.update.messages.post-update-name-already-implemented'),
193                     $postUpdateName
194                 )
195             );
196         }
197     }
198 }