0

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

  1. I checked the user under this script run, it matches the one under which I ran this PHP script from the console

  2. I checked if the script works through the console - the script works and the composer installs the packages

  3. 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!

Xab Ion
1,3311 gold badge14 silver badges22 bronze badges
asked Aug 27, 2023 at 5:58
3
  • What happens instead? What have you tried to resolve the problem? How is this related to PHP 7.3 (which is pretty outdated and shouldn't be used for years!) Commented Aug 27, 2023 at 11:18
  • the package dependency installation command does not take any action if you run the script from the browser. The project uses php version 7.3, although this is not particularly important in my situation. If I run the script from the console as the same user, then the composer installs the dependencies. And I had a question how to fix this and make the command to install composer packages work when running the script from the browser? Commented Aug 27, 2023 at 14:35
  • I also tried to use the commands not through the globally installed composer but through composer.phar. The result is the same, nothing is set if you run it through a script through a browser, but everything works through the console! Commented Aug 28, 2023 at 8:36

1 Answer 1

0

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!

  1. I created a hotel project in which I initialized the composer without installing packages. Just a standard composer.json and composer.lock
  2. Next, I installed the composer/composer dependency with the command
composer require composer/composer
  1. After that, I dragged the resulting vendor folder into my main project into the folder with the scripts for deploying on the server.
  2. I threw this folder, which I dragged into gitignore, so as not to clog git branches
  3. in the script, I added the following lines of code so that the composer would install dependencies
<?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!

answered Aug 28, 2023 at 16:03
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.