5 * Contains \Drupal\Console\Command\Generate\UpdateCommand.
8 namespace Drupal\Console\Command\Generate;
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\UpdateGenerator;
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\Command\Shared\CommandTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Extension\Manager;
20 use Drupal\Console\Core\Utils\ChainQueue;
21 use Drupal\Console\Utils\Site;
26 * @package Drupal\Console\Command\Generate
28 class UpdateCommand extends Command
31 use ConfirmationTrait;
37 protected $extensionManager;
40 * @var UpdateGenerator
52 protected $chainQueue;
56 * UpdateCommand constructor.
58 * @param Manager $extensionManager
59 * @param UpdateGenerator $generator
61 * @param ChainQueue $chainQueue
63 public function __construct(
64 Manager $extensionManager,
65 UpdateGenerator $generator,
67 ChainQueue $chainQueue
69 $this->extensionManager = $extensionManager;
70 $this->generator = $generator;
72 $this->chainQueue = $chainQueue;
73 parent::__construct();
76 protected function configure()
79 ->setName('generate:update')
80 ->setDescription($this->trans('commands.generate.update.description'))
81 ->setHelp($this->trans('commands.generate.update.help'))
85 InputOption::VALUE_REQUIRED,
86 $this->trans('commands.common.options.module')
91 InputOption::VALUE_REQUIRED,
92 $this->trans('commands.generate.update.options.update-n')
99 protected function execute(InputInterface $input, OutputInterface $output)
101 $io = new DrupalStyle($input, $output);
103 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
104 if (!$this->confirmGeneration($io)) {
108 $module = $input->getOption('module');
109 $updateNumber = $input->getOption('update-n');
111 $lastUpdateSchema = $this->getLastUpdate($module);
113 if ($updateNumber <= $lastUpdateSchema) {
114 throw new \InvalidArgumentException(
116 $this->trans('commands.generate.update.messages.wrong-update-n'),
122 $this->generator->generate($module, $updateNumber);
124 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
127 protected function interact(InputInterface $input, OutputInterface $output)
129 $io = new DrupalStyle($input, $output);
131 $this->site->loadLegacyFile('/core/includes/update.inc');
132 $this->site->loadLegacyFile('/core/includes/schema.inc');
134 $module = $input->getOption('module');
136 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
137 $module = $this->moduleQuestion($io);
138 $input->setOption('module', $module);
141 $lastUpdateSchema = $this->getLastUpdate($module);
142 $nextUpdateSchema = $lastUpdateSchema ? ($lastUpdateSchema + 1): 8001;
144 $updateNumber = $input->getOption('update-n');
145 if (!$updateNumber) {
146 $updateNumber = $io->ask(
147 $this->trans('commands.generate.update.questions.update-n'),
149 function ($updateNumber) use ($lastUpdateSchema) {
150 if (!is_numeric($updateNumber)) {
151 throw new \InvalidArgumentException(
153 $this->trans('commands.generate.update.messages.wrong-update-n'),
158 if ($updateNumber <= $lastUpdateSchema) {
159 throw new \InvalidArgumentException(
161 $this->trans('commands.generate.update.messages.wrong-update-n'),
166 return $updateNumber;
171 $input->setOption('update-n', $updateNumber);
176 protected function createGenerator()
178 return new UpdateGenerator();
181 protected function getLastUpdate($module)
183 $this->site->loadLegacyFile('/core/includes/update.inc');
184 $this->site->loadLegacyFile('/core/includes/schema.inc');
186 $updates = update_get_update_list();
188 if (empty($updates[$module]['pending'])) {
189 $lastUpdateSchema = drupal_get_schema_versions($module);
190 $lastUpdateSchema = $lastUpdateSchema[0];
192 $lastUpdateSchema = reset(array_keys($updates[$module]['pending'], max($updates[$module]['pending'])));
195 return $lastUpdateSchema;