Elgg  Version 6.1
UpgradeCommand.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
7 use function React\Promise\all;
11 
15 class UpgradeCommand extends BaseCommand {
16 
20  protected function configure() {
21  $this->setName('upgrade')
22  ->setDescription(elgg_echo('cli:upgrade:description'))
23  ->addOption('force', 'f', InputOption::VALUE_NONE,
24  elgg_echo('cli:upgrade:option:force')
25  )
26  ->addArgument('async', InputOption::VALUE_OPTIONAL,
27  elgg_echo('cli:upgrade:argument:async')
28  );
29  }
30 
34  protected function execute(InputInterface $input, OutputInterface $output) {
35  $this->input = $input;
36  $this->output = $output;
37 
38  $async = (bool) $this->argument('async');
39  $force = $this->option('force');
40 
41  $return = self::SUCCESS;
42 
43  // run Phinx (database) migrations
44  ElggApplication::migrate();
45 
46  $app = ElggApplication::getInstance();
47  $initial_app = clone $app;
48 
49  $boot = new BootHandler($app);
50  $boot->bootServices();
51  $boot->bootPlugins();
52 
53  // check if upgrade is locked
54  $is_locked = _elgg_services()->mutex->isLocked('upgrade');
55  if ($is_locked && !$force) {
56  $this->error(elgg_echo('upgrade:locked'));
57 
58  return 1;
59  } elseif ($is_locked && $force) {
60  _elgg_services()->mutex->unlock('upgrade');
61 
62  $this->notice(elgg_echo('upgrade:unlock:success'));
63  }
64 
65  // run system upgrades
66  $upgrades = _elgg_services()->upgrades->getPendingUpgrades(false);
67  $job = _elgg_services()->upgrades->run($upgrades);
68 
69  $job->done(
70  function () {
71  $this->notice(elgg_echo('cli:upgrade:system:upgraded'));
72  },
73  function ($errors) use (&$return) {
74  $this->error(elgg_echo('cli:upgrade:system:failed'));
75 
76  if (!is_array($errors)) {
77  $errors = [$errors];
78  }
79 
80  foreach ($errors as $error) {
81  $this->error($error);
82  }
83 
84  $return = self::FAILURE;
85  }
86  );
87 
88  _elgg_services()->plugins->generateEntities();
89 
90  if ($return !== self::SUCCESS || !$async) {
91  return $return;
92  }
93 
94  // We want to reboot the application, because some of the services (e.g. dic) can bootstrap themselves again
95  $app = $initial_app;
96  ElggApplication::setInstance($initial_app);
97  $app->start();
98 
99  // run async upgrades
100  $upgrades = _elgg_services()->upgrades->getPendingUpgrades(true);
101  $job = _elgg_services()->upgrades->run($upgrades);
102 
103  $job->done(
104  function () {
105  $this->notice(elgg_echo('cli:upgrade:async:upgraded'));
106  },
107  function ($errors) use (&$return) {
108  $this->error(elgg_echo('cli:upgrade:aysnc:failed'));
109 
110  if (!is_array($errors)) {
111  $errors = [$errors];
112  }
113 
114  foreach ($errors as $error) {
115  $this->error($error);
116  }
117 
118  $return = self::FAILURE;
119  }
120  );
121 
122  return $return;
123  }
124 }
if(empty($plugins)) $errors
Adds interaction to a console command.
Definition: BaseCommand.php:18
error($message)
Print an error.
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
notice($message)
Print a notce.
$upgrades
Lists pending upgrades.
Definition: upgrades.php:11
$error
Bad request error.
Definition: 400.php:6
execute(InputInterface $input, OutputInterface $output)
{}
elgg-cli upgrade [async]
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
argument($name)
Returns argument value.
Handles application boot sequence.
Definition: BootHandler.php:12
option($name)
Returns option value.