bashdeploymentdependenciescomposer-phpphp-7.3

How to run composer install in php script via exec or shell_exec?


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!


Solution

  • 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!