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

想知道是否有可能从一个PHP包装器从浏览器执行composer ,因为我没有访问服务器的shell访问权限。

不知道你是否可以用cURL来做到这一点?

是的,你可以运行一个PHP包装器的composer php。 所有Composer源代码都可以在Phar文件中find,所以可以将其解压缩,然后在设置InputInterface以replaceComposer,并希望通过命令行传入命令后运行它。

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

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

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

 ./project/vendors/ 

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

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

虽然这是可能的,但这不是一个好主意,但是如果你不能使用一个给你ssh访问权限的主机,可能是必要的。

我强烈build议您至less为自己或您的办公室获取一个静态IP地址,然后限制访问您自己的IP,以及在服务器上运行该脚本后可能会删除该脚本,以防止意外重新运行。

Danack解决scheme的一个替代方法是在"composer/composer"包含"composer/composer"作为依赖项,并使用它的API,而不是从composer.phar中提取内容。

composer.json

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

手动运行composer install ,所以你可以在下面的脚本需要它:

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."; 

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

我认为在部署之前真正在托pipe源代码的机器上运行Composer会更好。

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

我已经成功地使用了这个function。 请记住,“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); } 

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

与Endel的答案类似,但是我需要从composer show --direct捕获数组中的输出,所以我从作曲库中的ShowCommand文件中提取了一些代码,并创build了一个composer-wrapper库,我可以这样做:

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

并得到像['composer/composer'=>'1.3.0.0']的关联数组

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

因此,通过简单地在机器上安装composer php,我能够做到:

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