有没有办法在我们的生产/测试环境中运行 composer update
命令?
问题是我无权访问命令行。
最佳答案
是的。有一个解决方案。但它可能需要一些服务器配置...... 由于安全风险,其中一些默认情况下是被禁止的!!
下载 composer.phar
https://getcomposer.org/download/
- 这是 PHP 存档,可以通过 Phar()
提取并作为常规库执行。
创建新的 php 文件并将其放入 web 公共(public)文件夹。即 /public/composer.php
或在https://github.com/whipsterCZ/laravel-libraries/blob/master/public/composer.php下载
配置
<?php
//TODO! Some Authorization - Whitelisted IP, Security tokens...
echo '<pre>
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ UPDATE
/_/
';
define('ROOT_DIR',realpath('../'));
define('EXTRACT_DIRECTORY', ROOT_DIR. '/composer');
define('HOME_DIRECTORY', ROOT_DIR. '/composer/home');
define('COMPOSER_INITED', file_exists(ROOT_DIR.'/vendor'));
set_time_limit(100);
ini_set('memory_limit',-1);
if (!getenv('HOME') && !getenv('COMPOSER_HOME')) {
putenv("COMPOSER_HOME=".HOME_DIRECTORY);
}
提取 Composer 库
if (file_exists(EXTRACT_DIRECTORY.'/vendor/autoload.php') == true) {
echo "Extracted autoload already exists. Skipping phar extraction as presumably it's already extracted.\n";
}
else{
$composerPhar = new Phar("../composer.phar");
//php.ini set phar.readonly=0
$composerPhar->extractTo(EXTRACT_DIRECTORY);
}
运行 Composer 命令
// change directory to root
chdir(ROOT_DIR);
//This requires the phar to have been extracted successfully.
require_once (EXTRACT_DIRECTORY.'/vendor/autoload.php');
//Use the Composer classes
use Composer\Console\Application;
use Composer\Command\UpdateCommand;
use Symfony\Component\Console\Input\ArrayInput;
//Create the commands
$args = array('command' => 'update');
if(!COMPOSER_INITED) {
echo "This is first composer run: --no-scripts option is applies\n";
$args['--no-scripts']=true; }
}
$input = new ArrayInput($args);
//Create the application and run it with the commands
$application = new Application();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
try {
//Running commdand php.ini allow_url_fopen=1 && proc_open() function available
$application->run($input);
echo 'Success';
} catch (\Exception $e) {
echo 'Error: '.$e->getMessage()."\n";
}
但是更好的是执行 composer install
,根据 composer.lock 这是从本地环境测试的最后一个依赖配置
唯一的改变是
$args = array('command' => 'install');
关于php - 如何在 PHP 服务器上运行 Composer 更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38396046/