1#!/usr/bin/env php
2<?php
3if (PHP_SAPI !== 'cli') {
4	echo "You must use the command line to run this script." . PHP_EOL;
5	die(1);
6}
7
8// Load dependencies
9// Check various installation paths, which may vary depending on how Elgg was installed
10$files = [
11	__DIR__ . '/../../autoload.php', // Elgg in Composer project
12	__DIR__ . '/../vendor/autoload.php',
13	__DIR__ . '/vendor/autoload.php', // Elgg as base path
14	__DIR__ . '/../autoload.php', // from Composer bin directory
15];
16
17foreach ($files as $file) {
18	if (file_exists($file)) {
19		require_once $file;
20	}
21}
22
23if (!class_exists('\Elgg\Application')) {
24	fwrite(STDERR, "Composer dependencies are not installed "
25		. "or you are trying to run the script outside of an Elgg installation's root directory." . PHP_EOL);
26	die(2);
27}
28
29$settings_file = \Elgg\Project\Paths::settingsFile();
30
31$installed = is_file($settings_file);
32if (!$installed) {
33	$cli = new Symfony\Component\Console\Application();
34	$cli->add(new \Elgg\Cli\InstallCommand());
35	$cli->run();
36	return;
37}
38
39$app = \Elgg\Application::getInstance();
40
41$services = $app->_services;
42
43$logger = $services->logger;
44
45$cli = $services->cli;
46$cli->setLogger($logger);
47
48$argv = $services->request->server->get('argv');
49
50if ($argv[1] === 'upgrade') {
51	// To run an upgrade successfully, we must first migrate
52	// the application before booting it
53	$cli->add(\Elgg\Cli\UpgradeCommand::class);
54	$cli->run(false);
55	return;
56}
57
58// For other commands, we just boot an application
59try {
60	$app->start();
61	$cli->run();
62} catch (\Throwable $throw) {
63	fwrite(STDERR, "An error occured during the execution of the command '{$argv[0]}': {$throw->getMessage()}" . PHP_EOL);
64	die(3);
65}
66