如何在 PHP 服务器上运行作曲家更新?

2022-01-21 00:00:00 php composer-php phar server-side

有没有办法在我们的生产/测试环境中运行 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

或在 https://github.com/下载WhiststerCZ/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);
}

提取作曲家库

if (file_exists(EXTRACT_DIRECTORY.'/vendor/autoload.php') == true) {
    echo "Extracted autoload already exists. Skipping phar extraction as presumably it's already extracted.
";
}
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 ComposerConsoleApplication;
use ComposerCommandUpdateCommand;
use SymfonyComponentConsoleInputArrayInput;


//Create the commands
$args = array('command' => 'update');
if(!COMPOSER_INITED) { 
    echo "This is first composer run: --no-scripts option is applies
";
    $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()."
";
}

但是性能会更好 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');

相关文章