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