5 * Contains \Drupal\Console\Command\Update\ExecuteCommand.
8 namespace Drupal\Console\Command\Update;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Core\State\StateInterface;
15 use Drupal\Core\Extension\ModuleHandler;
16 use Drupal\Core\Update\UpdateRegistry;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Utils\Site;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Core\Utils\ChainQueue;
23 class ExecuteCommand extends Command
40 protected $moduleHandler;
45 protected $postUpdateRegistry;
51 protected $extensionManager;
56 protected $chainQueue;
69 * EntitiesCommand constructor.
72 * @param StateInterface $state
73 * @param ModuleHandler $moduleHandler
74 * @param UpdateRegistry $postUpdateRegistry
75 * @param Manager $extensionManager
76 * @param ChainQueue $chainQueue
78 public function __construct(
80 StateInterface $state,
81 ModuleHandler $moduleHandler,
82 UpdateRegistry $postUpdateRegistry,
83 Manager $extensionManager,
84 ChainQueue $chainQueue
87 $this->state = $state;
88 $this->moduleHandler = $moduleHandler;
89 $this->postUpdateRegistry = $postUpdateRegistry;
90 $this->extensionManager = $extensionManager;
91 $this->chainQueue = $chainQueue;
92 parent::__construct();
98 protected function configure()
101 ->setName('update:execute')
102 ->setDescription($this->trans('commands.update.execute.description'))
105 InputArgument::OPTIONAL,
106 $this->trans('commands.common.options.module')
110 InputArgument::OPTIONAL,
111 $this->trans('commands.update.execute.options.update-n')
118 protected function execute(InputInterface $input, OutputInterface $output)
120 $io = new DrupalStyle($input, $output);
121 $this->module = $input->getArgument('module') ?: 'all';
122 $this->update_n = $input->getArgument('update-n');
124 $this->site->loadLegacyFile('/core/includes/install.inc');
125 $this->site->loadLegacyFile('/core/includes/update.inc');
127 drupal_load_updates();
128 update_fix_compatibility();
129 $updates = update_get_update_list();
130 if (!$this->checkUpdates($io, $updates)) {
134 $maintenance_mode = $this->state->get('system.maintenance_mode', false);
136 if (!$maintenance_mode) {
137 $io->info($this->trans('commands.site.maintenance.description'));
138 $this->state->set('system.maintenance_mode', true);
142 $complete = $this->runUpdates($io, $updates);
144 // Post Updates are only safe to run after all schemas have been updated.
146 $this->runPostUpdates($io);
148 } catch (\Exception $e) {
149 watchdog_exception('update', $e);
150 $io->error($e->getMessage());
154 if (!$maintenance_mode) {
155 $this->state->set('system.maintenance_mode', false);
156 $io->info($this->trans('commands.site.maintenance.messages.maintenance-off'));
160 ->addCommand('cache:rebuild', ['cache' => 'all']);
164 * @param \Drupal\Console\Core\Style\DrupalStyle $io
165 * @param array $updates
167 * @return bool true if the selected module/update number exists.
169 private function checkUpdates(DrupalStyle $io, array $updates)
171 if ($this->module != 'all') {
172 if (!isset($updates[$this->module])) {
175 $this->trans('commands.update.execute.messages.no-module-updates'),
181 if ($this->update_n && !isset($updates[$this->module]['pending'][$this->update_n])) {
184 $this->trans('commands.update.execute.messages.module-update-function-not-found'),
197 * @param \Drupal\Console\Core\Style\DrupalStyle $io
198 * @param array $updates
200 * @return bool True if all available updates have been run.
202 private function runUpdates(DrupalStyle $io, array $updates)
204 if ($this->module != 'all') {
205 $complete = count($updates) == 1;
206 $updates = [$this->module => $updates[$this->module]];
211 foreach ($updates as $module_name => $module_updates) {
212 $extension = $this->extensionManager->getModule($module_name);
214 $extension = $this->extensionManager->getProfile($module_name);
218 ->loadLegacyFile($extension->getPath() . '/'. $module_name . '.install', false);
221 if ($this->update_n > $module_updates['start']) {
223 $this->trans('commands.update.execute.messages.executing-required-previous-updates')
227 foreach ($module_updates['pending'] as $update_number => $update) {
228 if ($this->module != 'all' && $this->update_n !== null && $this->update_n < $update_number) {
234 $this->trans('commands.update.execute.messages.executing-update'),
240 $updateExploded = explode(" - ", $update);
241 $updateExploded = count($updateExploded)>0?$updateExploded[1]:$updateExploded[0];
243 $io->comment(trim($updateExploded));
246 $this->moduleHandler->invoke($module_name, 'update_' . $update_number);
247 drupal_set_installed_schema_version($module_name, $update_number);
255 * @param \Drupal\Console\Core\Style\DrupalStyle $io
257 private function runPostUpdates(DrupalStyle $io)
259 $postUpdates = $this->postUpdateRegistry->getPendingUpdateInformation();
260 foreach ($postUpdates as $module_name => $module_updates) {
261 foreach ($module_updates['pending'] as $update_name => $update) {
264 $this->trans('commands.update.execute.messages.executing-update'),
275 drupal_flush_all_caches();
277 update_invoke_post_update($function, $context);