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