如何在没有 Composer 的情况下安装 Composer PHP 软件包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40545795/
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-26 02:11:23  来源:igfitidea点击:

How do I install Composer PHP packages without Composer?

phpcomposer-php

提问by Amy Neville

I'm trying to install the Coinbase PHP API but it requires Composer:

我正在尝试安装 Coinbase PHP API,但它需要 Composer:

https://github.com/coinbase/coinbase-php

https://github.com/coinbase/coinbase-php

I'm looking for a universal PHP solution (perhaps a function) to let me install composer packages directly onto my server, without having to use Composer.

我正在寻找一个通用的 PHP 解决方案(也许是一个函数),让我可以将 Composer 包直接安装到我的服务器上,而不必使用 Composer。

I think the developers of Composer believe they are helping people, but actually there are thousands of beginner developers that are being locked out of learning web development by the 'Composer barrier'.

我认为 Composer 的开发人员相信他们在帮助人们,但实际上有成千上万的初学者开发人员被“Composer 障碍”锁定在学习 Web 开发之外。

It would really help if there was a flexible solution or some approach where we could install without Composer? How can I do this?

如果有一个灵活的解决方案或某种方法可以在没有 Composer 的情况下进行安装,那真的会有所帮助吗?我怎样才能做到这一点?

Please don't respond with some sarcastic comment. There are people that don't want to use Composer and I don't see why we should be herded into a specific third-party software in order to do web development.

请不要用一些讽刺的评论来回应。有些人不想使用 Composer,我不明白为什么我们应该被赶到特定的第三方软件中以进行 Web 开发。

采纳答案by álvaro González

The composer.jsonfilelists the dependencies. In your example:

composer.json文件列出了依赖项。在你的例子中:

"require": {
    "php": ">=5.5.0",
    "guzzlehttp/guzzle": "^6.0",
    "psr/http-message": "^1.0",
    "psr/log": "^1.0"
},

You must then find the corresponding packages in the packagistsite. Repeat the same process for each dependency: find additional dependencies in their corresponding composer.jsonfiles and search again.

然后您必须在packagist站点中找到相应的包。对每个依赖项重复相同的过程:在其对应的composer.json文件中查找其他依赖项并再次搜索。

When you finally have a complete list of the required packages, you only need to install them all one by one. For the most part, it's just a matter of dropping the files somewhere in your project directory. But you must also ensure that PHP can find the needed classes. Since you aren't using Composer's auto-loader, you need to add them to your own custom autoloader. You can figure out the information from the respective composer.jsonfiles, e.g.:

当您最终拥有所需软件包的完整列表时,您只需将它们一一安装即可。大多数情况下,只需将文件放在项目目录中的某个位置即可。但是您还必须确保 PHP 可以找到所需的类。由于您没有使用 Composer 的自动加载器,您需要将它们添加到您自己的自定义自动加载器中。您可以从相应的composer.json文件中找出信息,例如:

"autoload": {
    "psr-4": { "Coinbase\Wallet\": "src/" }
},

If you don't use a class auto-loader you'll need to figure out the individual require_oncestatements. You'll probably need a lot of trial and error because most library authors won't care documenting that.

如果您不使用类自动加载器,则需要找出各个require_once语句。您可能需要大量的反复试验,因为大多数库作者不会关心记录这些。

Also, and just in case there's confusion about this:

另外,以防万一对此有混淆:

  • Composer has an official GUI installer for Windows and a copy and pastecommand-line installation procedure for all platforms.
  • Composer can be run locally and its output just uploaded elsewhere. You don't need SSH in your shared hosting.
  • The command needed to install a library can be copied and pasted from the package web site—even if the package maintainer didn't care to document it, packagist.org generates it by default.
  • Composer 具有适用于 Windows 的官方 GUI 安装程序和适用于所有平台的复制和粘贴命令行安装程序。
  • Composer 可以在本地运行,其输出可以上传到其他地方。您的共享主机不需要 SSH。
  • 安装库所需的命令可以从包网站复制和粘贴——即使包维护者不关心记录它,packagist.org 默认生成它。

Composer is not perfect and it doesn't suit all use cases but, when it comes to installing a library that relies on it, it's undoubtedly the best alternative and it's a fairly decent one.

Composer 并不完美,它并不适合所有用例,但是,在安装依赖于它的库时,它无疑是最好的选择,而且相当不错。



I've checked other answers that came after mine. They mostly fall in two categories:

我已经检查了我的其他答案。它们主要分为两类:

  1. Install a library and write a custom download script with it
  2. Use an online web based interface for Composer
  1. 安装库并用它编写自定义下载脚本
  2. 为 Composer 使用基于 Web 的在线界面

Unless I'm missing something, none of them address the complaints expressed by the OP:

除非我遗漏了什么,否则他们都没有解决 OP 表达的投诉:

  • Learning curve
  • Use of third-party software
  • Possibility to develop right on the server (using SSH, I presume)
  • Potentially deep dependency tree
  • 学习曲线
  • 使用第三方软件
  • 可以在服务器上进行开发(我假设使用 SSH)
  • 潜在的深度依赖树

回答by jahajee.com

You can try https://php-download.com/which can help you download all dependency most of the time along with vendorfolder. It promises composer not required. Tried it myself. It finds and creates all required folders and zips it for download. Works perfectly !!

您可以尝试https://php-download.com/,它可以帮助您在大多数情况下下载所有依赖项以及vendor文件夹。它承诺不需要作曲家。我自己试过了。它会查找并创建所有必需的文件夹并将其压缩以供下载。完美运行!

回答by Tschallacka

I'm using shared hosting for a website and can't execute commands there. Aside from running composer via php script request that I request via browser, I usually use this workflow:

我正在为网站使用共享主机,但无法在那里执行命令。除了通过浏览器请求的 php 脚本请求运行 composer 之外,我通常使用以下工作流程:

  • Make sure you have php installed locally.
  • Make directory on desktop.
  • download composer.phar from https://getcomposer.org/download/(under header *Manual Download) and place it in the directory.
  • make a file composer.jsonpaste in it the following contents

     {
         "require": {
             "coinbase/coinbase": "~2.0"
         }
     }
    
  • Browse to the directory with the shell of your choice(bash, git-bash, cmd, windows bash)

  • type php composer.phar updatescreenshot of bash output
  • Upload the vendor directory to your webserver via ftp or whatever mechanic you use.
  • include in your php project where you load your libraries(modify path to where you uploaded the vendor dir so it will include that autoload file)

    require_once('vendor/autoload.php');
    
  • 确保您在本地安装了 php。
  • 在桌面上制作目录。
  • https://getcomposer.org/download/(在标题*Manual Download 下)下载 composer.phar并将其放在目录中。
  • 制作一个文件composer.json在其中粘贴以下内容

     {
         "require": {
             "coinbase/coinbase": "~2.0"
         }
     }
    
  • 使用您选择的 shell 浏览到目录(bash、git-bash、cmd、windows bash)

  • 类型 php composer.phar updatebash 输出截图
  • 通过 ftp 或您使用的任何机制将供应商目录上传到您的网络服务器。
  • 包含在您加载库的 php 项目中(修改上传供应商目录的路径,以便它包含该自动加载文件)

    require_once('vendor/autoload.php');
    

This way you get the benefit of dependency management and you don't have to include manually all the gazillion of files and download all the dependencies manually, and updating them is just as easy as typing php composer.phar updateand then replacing the vendor dir on your server with the new one.

通过这种方式,您可以获得依赖项管理的好处,并且您不必手动包含所有海量文件并手动下载所有依赖项,更新它们就像键入php composer.phar update然后替换服务器上的供应商目录一样简单新的一个。

回答by Adam Thompson

I had to do this for an FTP server I didn't have SSH access to. The site listed in here worked, then I realized you can just do a composer install on your own server (using your target's PHP version), then copy all the files over.

我必须为我没有 SSH 访问权限的 FTP 服务器执行此操作。此处列出的站点有效,然后我意识到您可以在自己的服务器上安装 Composer(使用目标的 PHP 版本),然后复制所有文件。

回答by helderk

This is not the ultimate solution but for me it was a big help for most of the cases: https://github.com/Wilkins/composer-file-loader

这不是最终的解决方案,但对我来说,这对大多数情况都有很大帮助:https: //github.com/Wilkins/composer-file-loader

Allow you to load composer.json file just as composer would do it. This allow you to load composer.json file without composer (so theorically PHP 5.2 is enough)

允许您像 composer 那样加载 composer.json 文件。这允许你在没有 composer 的情况下加载 composer.json 文件(所以理论上 PHP 5.2 就足够了)

I know the question is old but I hope it will help someone.

我知道这个问题很老,但我希望它会帮助某人。

回答by jsmars

An alternative solution that worked for me (since php-download was down) can be done by making your own little local composer downloader.

一个对我有用的替代解决方案(因为 php-download 已关闭)可以通过制作您自己的小型本地 composer 下载器来完成。

  1. Download and install XAMPP locally: https://www.apachefriends.org/index.html
  2. Download and install composer locally: https://getcomposer.org/download/
  3. Open commandprompt, navigate to say c:\tempand and simply type the composer dependancy, for example: composer require league/oauth2-client
  4. Copy the files from your c:\tempfolder to your web host using an FTP program
  5. Add this to the top of your php: require("vendor/autoload.php");
  1. 在本地下载并安装 XAMPP:https: //www.apachefriends.org/index.html
  2. 在本地下载并安装 composer:https: //getcomposer.org/download/
  3. 打开命令提示符,导航到 sayc:\temp并简单地输入 Composer 依赖项,例如:composer require league/oauth2-client
  4. c:\temp使用 FTP 程序将文件从您的文件夹复制到您的网络主机
  5. 将此添加到您的php的顶部: require("vendor/autoload.php");