如何在PHP服务器上运行composer update?

Whi*_*rCZ 7 php server-side phar composer-php

有没有办法composer update在我们的生产/测试环境中运行命令?

问题是我无法访问命令行.

Whi*_*rCZ 5

是.有一个解决方案.但它可能需要一些服务器配置......由于安全风险,其中一些是默认禁止的!

下载composer.phar https://getcomposer.org/download/ - 这是PHP存档,可以通过Phar()常规库提取并执行.

创建新的php文件并将其放在Web公共文件夹中.即/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);
}
Run Code Online (Sandbox Code Playgroud)

提取作曲家库

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);
}
Run Code Online (Sandbox Code Playgroud)

运行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";
}
Run Code Online (Sandbox Code Playgroud)

但是根据composer.lock表示Better会执行 composer install,这是从本地环境测试的最后一个依赖配置

唯一的变化是

$args = array('command' => 'install');