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']);
129 protected function interact(InputInterface $input, OutputInterface $output)
131 $io = new DrupalStyle($input, $output);
133 $this->site->loadLegacyFile('/core/includes/update.inc');
134 $this->site->loadLegacyFile('/core/includes/schema.inc');
136 $module = $input->getOption('module');
138 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
139 $module = $this->moduleQuestion($io);
140 $input->setOption('module', $module);
143 $lastUpdateSchema = $this->getLastUpdate($module);
144 $nextUpdateSchema = $lastUpdateSchema ? ($lastUpdateSchema + 1): 8001;
146 $updateNumber = $input->getOption('update-n');
147 if (!$updateNumber) {
148 $updateNumber = $io->ask(
149 $this->trans('commands.generate.update.questions.update-n'),
151 function ($updateNumber) use ($lastUpdateSchema) {
152 if (!is_numeric($updateNumber)) {
153 throw new \InvalidArgumentException(
155 $this->trans('commands.generate.update.messages.wrong-update-n'),
160 if ($updateNumber <= $lastUpdateSchema) {
161 throw new \InvalidArgumentException(
163 $this->trans('commands.generate.update.messages.wrong-update-n'),
168 return $updateNumber;
173 $input->setOption('update-n', $updateNumber);
178 protected function createGenerator()
180 return new UpdateGenerator();
183 protected function getLastUpdate($module)
185 $this->site->loadLegacyFile('/core/includes/update.inc');
186 $this->site->loadLegacyFile('/core/includes/schema.inc');
188 $updates = update_get_update_list();
190 if (empty($updates[$module]['pending'])) {
191 $lastUpdateSchema = drupal_get_schema_versions($module);
192 $lastUpdateSchema = $lastUpdateSchema[0];
194 $lastUpdateSchema = reset(array_keys($updates[$module]['pending'], max($updates[$module]['pending'])));
197 return $lastUpdateSchema;