如何在PHP服务器上运行composer更新? [英] How to run composer update on PHP server?
问题描述
是否可以在生产/测试环境中运行composer update
命令?
Is there a way to run composer update
command on our production/test environment?
问题是我无权访问命令行.
Problem is that i don't have access for Command Line.
推荐答案
是.有一个解决方案.但这可能需要一些服务器配置... 由于安全隐患,其中一些默认情况下被禁止!
Yes. there is a solution. but it could demands some server configuration... and some of these are forbidden by default due to security risks!!
下载composer.phar
https://getcomposer.org/download/
-这是PHP存档,可以通过Phar()
提取并作为常规库执行.
download composer.phar
https://getcomposer.org/download/
- this is PHP Archive which can be extracted via Phar()
and executed as regular library.
创建新的php文件并将其放置到Web公用文件夹中.即/public/composer.php
create new php file and place it to web public folder. i.e. /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);
}
提取作曲家库
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命令
running Composer Command
// 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进行的,这是在本地环境中测试的最后一个依赖项配置
But Better will be performing composer install
, according to composer.lock which is last dependency configuration tested from local environment
唯一的改变是
$args = array('command' => 'install');
这篇关于如何在PHP服务器上运行composer更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!