如何通过exec或shell_exec在php脚本中运行composer install?

问题描述 投票:0回答:1

我正在尝试通过 php exec 或 shell_exec 运行 Composer update --lock -n

$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 已全局安装在服务器上

问题是这个脚本在部署到服务器时使用,它执行除了安装composer包之外的所有命令!

默认使用composer.json

  1. 我检查了此脚本运行下的用户,它与我从控制台运行此 PHP 脚本的用户匹配

  2. 我检查了脚本是否通过控制台工作 - 脚本工作并且作曲家安装了软件包

  3. 我从浏览器运行脚本,就像 GitHub webhook 一样,此时除了安装 Composer 依赖项之外,所有命令都有效

亲爱的专家,请告诉我如何实现该脚本,以便作曲家可以在从浏览器运行脚本时安装依赖项。

如何绕过这个限制?这个脚本对于部署到服务器来说是非常有必要的!

提前谢谢您!

bash deployment dependencies composer-php php-7.3
1个回答
0
投票

总的来说,我解决了我的问题!

问题在于,composer 在其基本配置中没有某些需要通过 php 脚本工作的依赖包。

那么,问题的解决方案以及我采取了什么行动!

  1. 我创建了一个酒店项目,在其中初始化了作曲家而没有安装软件包。只是一个标准的composer.json和composer.lock
  2. 接下来,我使用命令安装了 composer/composer 依赖项
composer require composer/composer
  1. 之后,我将生成的 vendor 文件夹拖到我的主项目中,并将其包含在服务器上部署的脚本中。
  2. 我把这个文件夹扔了,我把它拖进gitignore,以免堵塞git分支
  3. 在脚本中,我添加了以下代码行,以便作曲家安装依赖项
<?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;

完成这些步骤后,通过从浏览器运行脚本,我在composer.json中编写的依赖项已安装,没有任何问题,就像 github webhook 那样。

已检查,有效!

© www.soinside.com 2019 - 2024. All rights reserved.