在浏览器中使用 PHP 脚本运行 composer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17219436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:30:38  来源:igfitidea点击:

Run composer with a PHP script in browser

phpcurlcomposer-php

提问by dcolumbus

Wondering if it's possible to execute composerfrom the browser with a little PHP wrapper as I don't have access to shell access to the server.

想知道是否可以composer使用一个小的 PHP 包装器从浏览器执行,因为我无权访问服务器的 shell。

Not sure if you can do this with cURL?

不确定你是否可以用 cURL 做到这一点?

采纳答案by Danack

Yes you can run Composer with a little PHP wrapper. All of the Composer source code is available in the Phar file, so it can be extracted and then you can run it after setting up an InputInterface to replace Composer expecting the commands to be passed in via the command line.

是的,您可以使用一个小的 PHP 包装器运行 Composer。所有 Composer 源代码都在 Phar 文件中,因此可以提取它,然后您可以在设置 InputInterface 后运行它,以替换 Composer,期望通过命令行传递命令。

If you setup your directory structure like this:

如果您像这样设置目录结构:

./project  
./project/composer.json
./project/composer.lock
./project/webroot/composerExtractor.php  
./project/var/

Put the code below into composerExtractor.php and then run it from a web-browser, Composer should download all the libraries into:

将下面的代码放入 composerExtractor.php 然后从网络浏览器运行它,Composer 应该将所有库下载到:

./project/vendors/

As well as generating the class-loader files in that directory as well.

以及在该目录中生成类加载器文件。

composerExtractor.php

composerExtractor.php

<?php

define('EXTRACT_DIRECTORY', "../var/extractedComposer");


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 setting phar.readonly must be set to 0
    $composerPhar->extractTo(EXTRACT_DIRECTORY);
}

//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;

// change out of the webroot so that the vendors file is not created in
// a place that will be visible to the intahwebz
chdir('../');

//Create the commands
$input = new ArrayInput(array('command' => 'update'));

//Create the application and run it with the commands
$application = new Application();
$application->run($input);

?>

Although this is possible, it's not a fantastic idea but may be necessary if you can't use a host that gives you ssh access.

尽管这是可能的,但这并不是一个绝妙的主意,但如果您不能使用提供 ssh 访问权限的主机,这可能是必要的。

I'd strongly recommend at least getting a static IP address for yourself or your office and then restricting access to just your own IP, as well as probably deleting this script after it's run on the server to prevent it being accidentally run again.

我强烈建议至少为您自己或您的办公室获取一个静态 IP 地址,然后限制对您自己 IP 的访问,以及可能在它在服务器上运行后删除此脚本以防止它再次意外运行。

回答by Endel

An alternative to Danack's solution, is to include "composer/composer"as a dependency in your composer.json, and just use it's API, instead of extracting the contents from composer.phar.

到Danack的解决方案的替代,是包含"composer/composer"在你的依赖composer.json,而只是用它的API,而不是从提取的内容composer.phar

composer.json

作曲家.json

...
"require-dev": {
  "composer/composer": "dev-master",
}
...

Run composer installmanually, so you'll be able to require it on the following script:

composer install手动运行,因此您可以在以下脚本中要求它:

composer_install.php

composer_install.php

<?php
require 'vendor/autoload.php'; // require composer dependencies

use Composer\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;

// Composer\Factory::getHomeDir() method 
// needs COMPOSER_HOME environment variable set
putenv('COMPOSER_HOME=' . __DIR__ . '/vendor/bin/composer');

// 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);

echo "Done.";

When you access the script from your browser, the command should run as expected.

当您从浏览器访问脚本时,该命令应按预期运行。

回答by Sven

I think it would be a better idea to actually run Composer on the machine that hosts your source code just before deployment.

我认为在部署之前在托管源代码的机器上实际运行 Composer 会是一个更好的主意。

You probably checkout your code from some kind of version control before you upload it to your host (or even just have it on your hard drive without). That machine should get Composer installed and execute composer installright before upload. You don't need to expose the production machine to download all the stuff.

在将代码上传到主机之前,您可能会从某种版本控制中检出代码(或者甚至只是将它放在您的硬盘驱动器上)。那台机器应该composer install在上传之前安装并执行 Composer 。你不需要暴露生产机器来下载所有的东西。

回答by Mangirdas Skripka

I've successfully used this function. Keep in mind, that 'composer-source' is a directory with content extracted from composer.phar archive.

我已经成功地使用了这个功能。请记住,“composer-source”是一个包含从 composer.phar 存档中提取的内容的目录。

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\StreamOutput;

function composerInstall() {
    //create composer.json with some content
    require_once 'composer-source/vendor/autoload.php';
    putenv('COMPOSER_HOME=' . __DIR__ . '/composer-source/bin/composer');
    chdir(__DIR__);
    $stream = fopen('php://temp', 'w+');
    $output = new StreamOutput($stream);
    $application = new Application();
    $application->setAutoExit(false);
    $code = $application->run(new ArrayInput(array('command' => 'install')), $output);
    return stream_get_contents($stream);
}

By the way, you can extract composer.phar on this site: http://unphar.com/

顺便说一句,你可以在这个网站上提取 composer.phar:http: //unphar.com/

回答by shadi

Similar to Endel's answer, but I needed to capture the output from composer show --directin an array, so I extracted some code from the ShowCommandfile in the composer repository and made a composer-wrapperlibrary, with which I can do:

与 Endel 的回答类似,但我需要从composer show --direct数组中捕获输出,因此我从composer 存储库中的ShowCommand文件中提取了一些代码并制作了一个composer-wrapper库,我可以使用它:

$cw = new \shadiakiki1986\ComposerWrapper();
$packages = $cw->showDirect();

and get an associative array like ['composer/composer'=>'1.3.0.0']

并获得一个关联数组,如 ['composer/composer'=>'1.3.0.0']

回答by Nathan Crause

I don't know if this is always done on installation, but I installed composer via Ubuntu's package, and it included "Composer" in the "/use/share/php" directory (which is in the include path).

我不知道这是否总是在安装时完成,但我通过 Ubuntu 的软件包安装了 Composer,它在“/use/share/php”目录(在包含路径中)中包含“Composer”。

Therefore, by simply having installed composer on the machine at all, I am able to do:

因此,只需在机器上安装 Composer,我就可以做到:

require_once 'Composer/autoload.php';
$application = new Composer\Console\Application();