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

时间:2021-11-14 21:16:30

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

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

Not sure if you can do this with cURL?

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

6 个解决方案

#1


24  

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,然后从Web浏览器运行它,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,以及可能在服务器上运行后删除此脚本以防止它再次意外运行。

#2


32  

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.json中包含“composer / composer”作为依赖项,并使用它的API,而不是从composer.phar中提取内容。

composer.json

composer.json

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

Run composer install manually, 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.

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

#3


6  

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 install right before upload. You don't need to expose the production machine to download all the stuff.

在将代码上传到主机之前,您可能会从某种版本控制中检出代码(或者甚至只是将它放在硬盘驱动器上)。该机器应该安装Composer并在上传之前执行composer install。您无需公开生产机器即可下载所有内容。

#4


2  

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/

#5


0  

Similar to Endel's answer, but I needed to capture the output from composer show --direct in an array, so I extracted some code from the ShowCommand file in the composer repository and made a composer-wrapper library, 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']

#6


0  

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”(在include路径中)。

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

因此,只需在机器上安装了作曲家,我就能做到:

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

#1


24  

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,然后从Web浏览器运行它,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,以及可能在服务器上运行后删除此脚本以防止它再次意外运行。

#2


32  

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.json中包含“composer / composer”作为依赖项,并使用它的API,而不是从composer.phar中提取内容。

composer.json

composer.json

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

Run composer install manually, 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.

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

#3


6  

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 install right before upload. You don't need to expose the production machine to download all the stuff.

在将代码上传到主机之前,您可能会从某种版本控制中检出代码(或者甚至只是将它放在硬盘驱动器上)。该机器应该安装Composer并在上传之前执行composer install。您无需公开生产机器即可下载所有内容。

#4


2  

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/

#5


0  

Similar to Endel's answer, but I needed to capture the output from composer show --direct in an array, so I extracted some code from the ShowCommand file in the composer repository and made a composer-wrapper library, 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']

#6


0  

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”(在include路径中)。

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

因此,只需在机器上安装了作曲家,我就能做到:

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