Version 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                 '',
93                 InputOption::VALUE_REQUIRED,
94                 $this->trans('commands.common.options.module')
95             )
96             ->addOption(
97                 'post-update-name',
98                 '',
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;
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
126     protected function interact(InputInterface $input, OutputInterface $output)
127     {
128         $io = new DrupalStyle($input, $output);
129
130         $this->site->loadLegacyFile('/core/includes/update.inc');
131         $this->site->loadLegacyFile('/core/includes/schema.inc');
132
133         $module = $input->getOption('module');
134         if (!$module) {
135             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
136             $module = $this->moduleQuestion($io);
137             $input->setOption('module', $module);
138         }
139
140         $postUpdateName = $input->getOption('post-update-name');
141         if (!$postUpdateName) {
142             $postUpdateName = $io->ask(
143                 $this->trans('commands.generate.post.update.questions.post-update-name'),
144                 '',
145                 function ($postUpdateName) {
146                     return $this->validator->validateSpaces($postUpdateName);
147                 }
148             );
149
150             $input->setOption('post-update-name', $postUpdateName);
151         }
152     }
153
154
155     protected function createGenerator()
156     {
157         return new PostUpdateGenerator();
158     }
159
160     protected function getLastUpdate($module)
161     {
162         $this->site->loadLegacyFile('/core/includes/update.inc');
163         $this->site->loadLegacyFile('/core/includes/schema.inc');
164
165         $updates = update_get_update_list();
166
167         if (empty($updates[$module]['pending'])) {
168             $lastUpdateSchema = drupal_get_schema_versions($module);
169         } else {
170             $lastUpdateSchema = reset(array_keys($updates[$module]['pending'], max($updates[$module]['pending'])));
171         }
172
173         return $lastUpdateSchema;
174     }
175
176     protected function validatePostUpdateName($module, $postUpdateName)
177     {
178         if (!$this->validator->validateSpaces($postUpdateName)) {
179             throw new \InvalidArgumentException(
180                 sprintf(
181                     $this->trans('commands.generate.post.update.messages.wrong-post-update-name'),
182                     $postUpdateName
183                 )
184             );
185         }
186
187         if ($this->extensionManager->validateModuleFunctionExist($module, $module . '_post_update_' . $postUpdateName, $module . '.post_update.php')) {
188             throw new \InvalidArgumentException(
189                 sprintf(
190                     $this->trans('commands.generate.post.update.messages.post-update-name-already-implemented'),
191                     $postUpdateName
192                 )
193             );
194         }
195     }
196 }