-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathinstallComposer.php
67 lines (53 loc) · 1.81 KB
/
installComposer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
putenv("HOME=" . getcwd());
if (!file_exists('vendor/autoload.php') && !file_exists('logs/composerDone.txt')) {
file_put_contents('logs/composerDone.txt', '');
installComposer();
}
function installComposer()
{
$composerCmd = getComposerCommand();
$cmd = "$composerCmd install 2>&1";
$output = [];
$returnCode = 0;
exec($cmd, $output, $returnCode);
file_put_contents('logs/installLog.txt', implode(PHP_EOL, $output), FILE_APPEND);
try {
require_once 'vendor/autoload.php';
} catch (\Throwable $th) {
$returnCode = 1;
}
return ('Composer installed via: ' . $composerCmd);
}
function getComposerCommand()
{
// Check if Composer is already installed globally
exec('composer --version 2>&1', $output, $returnCode);
if ($returnCode === 0) {
return 'composer'; // Use global Composer if available
}
// If composer.phar already exists, use it
if (file_exists('composer.phar')) {
return 'php composer.phar';
}
// Composer is missing, install it locally
installComposerLocally();
return 'php composer.phar';
}
function installComposerLocally()
{
// echo "Composer is not installed. Installing locally...\n";
exec("php -r \"copy('https://getcomposer.org/installer', 'test/composer-setup.php');\"", $output, $returnCode);
if (!empty($output))
print_r($output);
if ($returnCode !== 0) {
die("Failed to download Composer setup script.\n");
}
// Run the setup script to install Composer locally
exec("php test/composer-setup.php", $output, $returnCode);
unlink('test/composer-setup.php'); // Remove setup script after installation
if ($returnCode !== 0) {
die("Failed to install Composer.\n");
}
// echo "Composer installed locally as composer.phar.\n";
}