我正在尝试通过 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
我检查了此脚本运行下的用户,它与我从控制台运行此 PHP 脚本的用户匹配
我检查了脚本是否通过控制台工作 - 脚本工作并且作曲家安装了软件包
我从浏览器运行脚本,就像 GitHub webhook 一样,此时除了安装 Composer 依赖项之外,所有命令都有效
亲爱的专家,请告诉我如何实现该脚本,以便作曲家可以在从浏览器运行脚本时安装依赖项。
如何绕过这个限制?这个脚本对于部署到服务器来说是非常有必要的!
提前谢谢您!
总的来说,我解决了我的问题!
问题在于,composer 在其基本配置中没有某些需要通过 php 脚本工作的依赖包。
那么,问题的解决方案以及我采取了什么行动!
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;
完成这些步骤后,通过从浏览器运行脚本,我在composer.json中编写的依赖项已安装,没有任何问题,就像 github webhook 那样。
已检查,有效!