Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Update / ExecuteCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Update\ExecuteCommand.
6  */
7
8 namespace Drupal\Console\Command\Update;
9
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;
22
23 class ExecuteCommand extends Command
24 {
25     use CommandTrait;
26
27     /**
28      * @var Site
29      */
30     protected $site;
31
32     /**
33      * @var StateInterface
34      */
35     protected $state;
36
37     /**
38      * @var ModuleHandler
39      */
40     protected $moduleHandler;
41
42     /**
43      * @var UpdateRegistry
44      */
45     protected $postUpdateRegistry;
46
47
48     /**
49      * @var Manager
50      */
51     protected $extensionManager;
52
53     /**
54      * @var ChainQueue
55      */
56     protected $chainQueue;
57
58     /**
59      * @var String
60      */
61     private $module;
62
63     /**
64      * @var String
65      */
66     private $update_n;
67
68     /**
69      * EntitiesCommand constructor.
70      *
71      * @param Site           $site
72      * @param StateInterface $state
73      * @param ModuleHandler  $moduleHandler
74      * @param UpdateRegistry $postUpdateRegistry
75      * @param Manager        $extensionManager
76      * @param ChainQueue     $chainQueue
77      */
78     public function __construct(
79         Site $site,
80         StateInterface $state,
81         ModuleHandler $moduleHandler,
82         UpdateRegistry $postUpdateRegistry,
83         Manager $extensionManager,
84         ChainQueue $chainQueue
85     ) {
86         $this->site = $site;
87         $this->state = $state;
88         $this->moduleHandler = $moduleHandler;
89         $this->postUpdateRegistry = $postUpdateRegistry;
90         $this->extensionManager = $extensionManager;
91         $this->chainQueue = $chainQueue;
92         parent::__construct();
93     }
94
95     /**
96      * @inheritdoc
97      */
98     protected function configure()
99     {
100         $this
101             ->setName('update:execute')
102             ->setDescription($this->trans('commands.update.execute.description'))
103             ->addArgument(
104                 'module',
105                 InputArgument::OPTIONAL,
106                 $this->trans('commands.common.options.module')
107             )
108             ->addArgument(
109                 'update-n',
110                 InputArgument::OPTIONAL,
111                 $this->trans('commands.update.execute.options.update-n')
112             );
113     }
114
115     /**
116      * @inheritdoc
117      */
118     protected function execute(InputInterface $input, OutputInterface $output)
119     {
120         $io = new DrupalStyle($input, $output);
121         $this->module = $input->getArgument('module') ?: 'all';
122         $this->update_n = $input->getArgument('update-n');
123
124         $this->site->loadLegacyFile('/core/includes/install.inc');
125         $this->site->loadLegacyFile('/core/includes/update.inc');
126
127         drupal_load_updates();
128         update_fix_compatibility();
129         $updates = update_get_update_list();
130         if (!$this->checkUpdates($io, $updates)) {
131             return 1;
132         }
133
134         $maintenance_mode = $this->state->get('system.maintenance_mode', false);
135
136         if (!$maintenance_mode) {
137             $io->info($this->trans('commands.site.maintenance.description'));
138             $this->state->set('system.maintenance_mode', true);
139         }
140
141         try {
142             $complete = $this->runUpdates($io, $updates);
143
144             // Post Updates are only safe to run after all schemas have been updated.
145             if ($complete) {
146                 $this->runPostUpdates($io);
147             }
148         } catch (\Exception $e) {
149             watchdog_exception('update', $e);
150             $io->error($e->getMessage());
151             return 1;
152         }
153
154         if (!$maintenance_mode) {
155             $this->state->set('system.maintenance_mode', false);
156             $io->info($this->trans('commands.site.maintenance.messages.maintenance-off'));
157         }
158
159         $this->chainQueue
160             ->addCommand('cache:rebuild', ['cache' => 'all']);
161     }
162
163     /**
164      * @param \Drupal\Console\Core\Style\DrupalStyle $io
165      * @param array                                  $updates
166      *
167      * @return bool true if the selected module/update number exists.
168      */
169     private function checkUpdates(DrupalStyle $io, array $updates)
170     {
171         if ($this->module != 'all') {
172             if (!isset($updates[$this->module])) {
173                 $io->error(
174                     sprintf(
175                         $this->trans('commands.update.execute.messages.no-module-updates'),
176                         $this->module
177                     )
178                 );
179                 return false;
180             } else {
181                 if ($this->update_n && !isset($updates[$this->module]['pending'][$this->update_n])) {
182                     $io->error(
183                         sprintf(
184                             $this->trans('commands.update.execute.messages.module-update-function-not-found'),
185                             $this->module,
186                             $this->update_n
187                         )
188                     );
189                     return false;
190                 }
191             }
192         }
193         return true;
194     }
195
196     /**
197      * @param \Drupal\Console\Core\Style\DrupalStyle $io
198      * @param array                                  $updates
199      *
200      * @return bool True if all available updates have been run.
201      */
202     private function runUpdates(DrupalStyle $io, array $updates)
203     {
204         if ($this->module != 'all') {
205             $complete = count($updates) == 1;
206             $updates = [$this->module => $updates[$this->module]];
207         } else {
208             $complete = true;
209         }
210
211         foreach ($updates as $module_name => $module_updates) {
212             $extension = $this->extensionManager->getModule($module_name);
213             if (!$extension) {
214                 $extension = $this->extensionManager->getProfile($module_name);
215             }
216             if ($extension) {
217                 $this->site
218                     ->loadLegacyFile($extension->getPath() . '/'. $module_name . '.install', false);
219             }
220
221             if ($this->update_n > $module_updates['start']) {
222                 $io->info(
223                     $this->trans('commands.update.execute.messages.executing-required-previous-updates')
224                 );
225             }
226
227             foreach ($module_updates['pending'] as $update_number => $update) {
228                 if ($this->module != 'all' && $this->update_n !== null && $this->update_n < $update_number) {
229                     return false;
230                 }
231
232                 $io->comment(
233                     sprintf(
234                         $this->trans('commands.update.execute.messages.executing-update'),
235                         $update_number,
236                         $module_name
237                     )
238                 );
239
240                 $updateExploded = explode(" - ", $update);
241                 $updateExploded = count($updateExploded)>0?$updateExploded[1]:$updateExploded[0];
242
243                 $io->comment(trim($updateExploded));
244                 $io->newLine();
245
246                 $this->moduleHandler->invoke($module_name, 'update_'  . $update_number);
247                 drupal_set_installed_schema_version($module_name, $update_number);
248             }
249         }
250
251         return $complete;
252     }
253
254     /**
255      * @param \Drupal\Console\Core\Style\DrupalStyle $io
256      */
257     private function runPostUpdates(DrupalStyle $io)
258     {
259         $postUpdates = $this->postUpdateRegistry->getPendingUpdateInformation();
260         foreach ($postUpdates as $module_name => $module_updates) {
261             foreach ($module_updates['pending'] as $update_name => $update) {
262                 $io->info(
263                     sprintf(
264                         $this->trans('commands.update.execute.messages.executing-update'),
265                         $update_name,
266                         $module_name
267                     )
268                 );
269
270                 $function = sprintf(
271                     '%s_post_update_%s',
272                     $module_name,
273                     $update_name
274                 );
275                 drupal_flush_all_caches();
276                 $context = [];
277                 update_invoke_post_update($function, $context);
278             }
279         }
280     }
281 }