I am trying to run composer update --lock -n via php exec or shell_exec
$commands = array(
'echo $PWD',
'whoami',
'git fetch',
'git reset --hard HEAD',
'git pull',
'git status',
'composer update --lock -n',
'npm update --lockfile-version 3 --package-lock-only',
'composer install -n',
'npm install',
'npm run build'
);
foreach ($commands as $command) {
exec("$command", $output2 , $status );
}
echo $output2;
Composer is installed globally on the server
The problem is that this script is used when deploying to the server and it executes all commands except installing composer packages!
Default is used composer.json
I checked the user under this script run, it matches the one under which I ran this PHP script from the console
I checked if the script works through the console - the script works and the composer installs the packages
I ran the script from the browser just like a GitHub webhook would do, and at that moment all the commands worked except for installing composer dependencies
Dear experts, please tell me how can I implement the script so that the composer can install dependencies when running the script from the browser.
How to get around this limitation? This script is very necessary for deployment to the server!
Thank you in advance!
In general, I solved my problem!
The problem was that the composer does not have in its basic configuration certain dependency packages that it needs to work through a php script.
So, the very solution to the problem and what actions I did!
composer require composer/composer
<?php
require 'config/deploy/vendor/autoload.php'; // the path to the file in the vendor folder that was dragged in step 3 above
use Composer\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
$commands = array(
'echo $PWD',
'whoami',
'git fetch',
'git reset --hard HEAD',
'git pull',
'git status',
'npm update --lockfile-version 3 --package-lock-only',
'npm install',
'npm run build'
);
foreach ($commands as $command) {
exec("$command", $output2 , $status );
}
putenv('COMPOSER_HOME=' . __DIR__ . 'config/deploy/vendor/bin/composer'); // path to the file in the vendor folder where the composer/composer dependency is installed
// call `composer install` command programmatically
$input = new ArrayInput(array('command' => 'install'));
$application = new Application();
$application->setAutoExit(false); // prevent `$application->run` method from exitting the script
$application->run($input);
exit;
After these steps, my dependencies written in composer.json were installed without any problems by running the script from the browser, as github webhook would do.
Checked, it works!