Elgg  Version 6.2
UpgradeCommand.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
10 
14 class UpgradeCommand extends BaseCommand {
15 
19  protected function configure() {
20  $this->setName('upgrade')
21  ->setDescription(elgg_echo('cli:upgrade:description'))
22  ->addOption('force', 'f', InputOption::VALUE_NONE,
23  elgg_echo('cli:upgrade:option:force')
24  )
25  ->addArgument('async', InputOption::VALUE_OPTIONAL,
26  elgg_echo('cli:upgrade:argument:async')
27  );
28  }
29 
33  protected function execute(InputInterface $input, OutputInterface $output) {
34  $this->input = $input;
35  $this->output = $output;
36 
37  $async = (bool) $this->argument('async');
38  $force = $this->option('force');
39 
40  $return = self::SUCCESS;
41 
42  // run Phinx (database) migrations
43  ElggApplication::migrate();
44 
45  $app = ElggApplication::getInstance();
46  $initial_app = clone $app;
47 
48  $boot = new BootHandler($app);
49  $boot->bootServices();
50  $boot->bootPlugins();
51 
52  // check if upgrade is locked
53  $is_locked = _elgg_services()->mutex->isLocked('upgrade');
54  if ($is_locked && !$force) {
55  $this->error(elgg_echo('upgrade:locked'));
56 
57  return 1;
58  } elseif ($is_locked && $force) {
59  _elgg_services()->mutex->unlock('upgrade');
60 
61  $this->notice(elgg_echo('upgrade:unlock:success'));
62  }
63 
64  // run system upgrades
65  $upgrades = _elgg_services()->upgrades->getPendingUpgrades(false);
66  $job = _elgg_services()->upgrades->run($upgrades);
67 
68  $job->then(
69  function () {
70  $this->notice(elgg_echo('cli:upgrade:system:upgraded'));
71  },
72  function ($errors) use (&$return) {
73  $this->error(elgg_echo('cli:upgrade:system:failed'));
74 
75  if (!is_array($errors)) {
76  $errors = [$errors];
77  }
78 
79  foreach ($errors as $error) {
80  $this->error($error);
81  }
82 
83  $return = self::FAILURE;
84  }
85  );
86 
87  _elgg_services()->plugins->generateEntities();
88 
89  if ($return !== self::SUCCESS || !$async) {
90  return $return;
91  }
92 
93  // We want to reboot the application, because some of the services (e.g. dic) can bootstrap themselves again
94  $app = $initial_app;
95  ElggApplication::setInstance($initial_app);
96  $app->start();
97 
98  // run async upgrades
99  $upgrades = _elgg_services()->upgrades->getPendingUpgrades(true);
100  $job = _elgg_services()->upgrades->run($upgrades);
101 
102  $job->then(
103  function () {
104  $this->notice(elgg_echo('cli:upgrade:async:upgraded'));
105  },
106  function ($errors) use (&$return) {
107  $this->error(elgg_echo('cli:upgrade:aysnc:failed'));
108 
109  if (!is_array($errors)) {
110  $errors = [$errors];
111  }
112 
113  foreach ($errors as $error) {
114  $this->error($error);
115  }
116 
117  $return = self::FAILURE;
118  }
119  );
120 
121  return $return;
122  }
123 }
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.