使用 PHP composer 克隆 git repo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12954051/
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
Use PHP composer to clone git repo
提问by martin
I'm trying to use composerto automatically clone a git repository from github that isn't in packagistbut it's not working and I can't figure out what am I doing wrong.
我正在尝试使用composer从 github 自动克隆一个不在packagist 中的 git 存储库,但它不起作用,我无法弄清楚我做错了什么。
I think I have to include it among "repositories" like so:
我想我必须将它包含在“存储库”中,如下所示:
"repositories": [
{
"url": "https://github.com/l3pp4rd/DoctrineExtensions.git",
"type": "git"
}
],
and then probably list it in "require" section. It should be similar to this examplebut it doesn't work. It just gives this error:
然后可能将它列在“需要”部分。它应该类似于这个例子,但它不起作用。它只是给出了这个错误:
Your requirements could not be resolved to an installable set of packages.
无法将您的要求解析为一组可安装的软件包。
Have anyone tried to do something like this already?
有没有人尝试过做这样的事情?
采纳答案by Mike Graf
At the time of writing in 2013, this was one way to do it. Composer has added support for better ways: See @igorw 's answer
在 2013 年撰写本文时,这是一种方法。Composer 增加了对更好方法的支持:见@igorw 的回答
DO YOU HAVE A REPOSITORY?
你有存储库吗?
Git, Mercurial and SVN is supported by Composer.
Composer 支持 Git、Mercurial 和 SVN。
DO YOU HAVE WRITE ACCESS TO THE REPOSITORY?
你有对存储库的写访问权限吗?
Yes?
是的?
DOES THE REPOSITORY HAVE A composer.jsonFILE
存储库是否有composer.json文件
If you have a repository you can write to: Add a composer.jsonfile, or fix the existing one, and DON'T use the solution below.
如果您有一个存储库,您可以写入:添加一个composer.json文件,或修复现有文件,不要使用下面的解决方案。
Go to @igorw 's answer
转到@igorw 的回答
ONLY USE THIS IF YOU DON'T HAVE A REPOSITORY
OR IF THE REPOSITORY DOES NOT HAVE A composer.jsonAND YOU CANNOT ADD IT
仅当您没有存储库
或存储库没有 Acomposer.json并且您无法添加它时才使用它
This will override everything that Composer may be able to read from the original repository's composer.json, including the dependencies of the package and the autoloading.
这将覆盖 Composer 可能能够从原始存储库读取的所有内容composer.json,包括包的依赖项和自动加载。
Using the packagetype will transfer the burden of correctly defining everything onto you. The easier way is to have a composer.jsonfile in the repository, and just use it.
使用package类型会将正确定义所有内容的负担转移到您身上。更简单的方法是composer.json在存储库中有一个文件,然后使用它。
This solution really only is for the rare cases where you have an abandoned ZIP download that you cannot alter, or a repository you can only read, but it isn't maintained anymore.
此解决方案实际上仅适用于您拥有无法更改的废弃 ZIP 下载或只能读取但不再维护的存储库的极少数情况。
"repositories": [
{
"type":"package",
"package": {
"name": "l3pp4rd/doctrine-extensions",
"version":"master",
"source": {
"url": "https://github.com/l3pp4rd/DoctrineExtensions.git",
"type": "git",
"reference":"master"
}
}
}
],
"require": {
"l3pp4rd/doctrine-extensions": "master"
}
回答by igorw
That package in fact is available through packagist. You don't need a custom repository definition in this case. Just make sure you add a require(which is always needed) with a matching version constraint.
该软件包实际上可以通过 packagist 获得。在这种情况下,您不需要自定义存储库定义。只需确保添加require具有匹配版本约束的(始终需要)。
In general, if a package is available on packagist, do notadd a VCS repo. It will just slow things down.
通常,如果 packagist 上有可用的包,则不要添加 VCS 存储库。它只会减慢速度。
For packages that are not available via packagist, use a VCS(or git) repository, as shown in your question. When you do, make sure that:
对于无法通过 packagist 获得的软件包,请使用VCS(或 git)存储库,如您的问题所示。这样做时,请确保:
- The "repositories" field is specified in the root composer.json (it's a root-only field, repository definitions from required packages are ignored)
- The repositories definition points to a valid VCS repo
- If the type is "git" instead of "vcs" (as in your question), make sure it is in fact a git repo
- You have a
requirefor the package in question - The constraint in the
requirematches the versions provided by the VCS repo. You can usecomposer show <packagename>to find the available versions. In this case~2.3would be a good option. - The name in the
requirematches the name in the remotecomposer.json. In this case, it isgedmo/doctrine-extensions.
- “repositories”字段在根 composer.json 中指定(它是一个仅限根的字段,来自所需包的存储库定义被忽略)
- 存储库定义指向有效的 VCS 存储库
- 如果类型是“git”而不是“vcs”(如您的问题),请确保它实际上是一个 git repo
- 你有一个有
require问题的包裹 - 中的约束
require与 VCS 存储库提供的版本相匹配。您可以使用composer show <packagename>查找可用版本。在这种情况下~2.3将是一个不错的选择。 - 中的名称
require与遥控器中的名称匹配composer.json。在这种情况下,它是gedmo/doctrine-extensions。
Here is a sample composer.jsonthat installs the same package via a VCS repo:
这是composer.json通过 VCS 存储库安装相同软件包的示例:
{
"repositories": [
{
"url": "https://github.com/l3pp4rd/DoctrineExtensions.git",
"type": "git"
}
],
"require": {
"gedmo/doctrine-extensions": "~2.3"
}
}
The VCS repo docsexplain all of this quite well.
该VCS回购文档解释了这一切非常好。
If there is a git (or other VCS) repository with a composer.jsonavailable, do notuse a "package" repo. Package repos require you to provide allof the metadata in the definition and will completely ignoreany composer.jsonpresent in the provided dist and source. They also have additional limitations, such as not allowing for proper updates in most cases.
如果有可用的 git(或其他 VCS)存储库composer.json,请不要使用“包”存储库。包 repos 要求您提供定义中的所有元数据,并且将完全忽略composer.json所提供的 dist 和源中存在的任何元数据。它们还有其他限制,例如在大多数情况下不允许进行适当的更新。
Avoid package repos (see also the docs).
避免包回购(另见文档)。
回答by Edris
You can include git repository to composer.json like this:
您可以像这样将 git 存储库包含到 composer.json 中:
"repositories": [
{
"type": "package",
"package": {
"name": "example-package-name", //give package name to anything, must be unique
"version": "1.0",
"source": {
"url": "https://github.com/example-package-name.git", //git url
"type": "git",
"reference": "master" //git branch-name
}
}
}],
"require" : {
"example-package-name": "1.0"
}
回答by Josef Kufner
Just tell composer to use source if available:
如果可用,只需告诉作曲家使用源代码:
composer update --prefer-source
Or:
或者:
composer install --prefer-source
Then you will get packages as cloned repositories instead of extracted tarballs, so you can make some changes and commit them back. Of course, assuming you have write/push permissions to the repository and Composer knows about project's repository.
然后您将获得作为克隆存储库的包而不是提取的 tarball,因此您可以进行一些更改并将它们提交回来。当然,假设您对存储库具有写入/推送权限并且 Composer 知道项目的存储库。
Disclaimer: I think I may answered a little bit different question, but this was what I was looking for when I found this question, so I hope it will be useful to others as well.
免责声明:我想我可能回答了一个有点不同的问题,但这是我发现这个问题时所寻找的,所以我希望它对其他人也有用。
If Composer does not know, where the project's repository is, or the project does not have proper composer.json, situation is a bit more complicated, but others answered such scenarios already.
如果 Composer 不知道,项目的存储库在哪里,或者项目没有正确的 composer.json,情况会更复杂一些,但其他人已经回答了这种情况。
回答by Henry
I was encountering the following error: The requested package my-foo/bar could not be found in any version, there may be a typo in the package name.
我遇到以下错误: The requested package my-foo/bar could not be found in any version, there may be a typo in the package name.
If you're forking another repo to make your own changes you will end up with a new repository.
如果您要分叉另一个存储库以进行自己的更改,您最终将获得一个新存储库。
E.g:
例如:
https://github.com/foo/bar.git
=>
https://github.com/my-foo/bar.git
The new url will need to go into your repositories section of your composer.json.
新 url 需要进入 composer.json 的存储库部分。
Remember if you want refer to your fork as my-foo/barin your require section, you will have to rename the package in the composer.jsonfile inside of your new repo.
请记住,如果您想my-foo/bar在 require 部分中引用您的 fork ,则必须在composer.json新存储库内的文件中重命名该包。
{
"name": "foo/bar",
=>
{
"name": "my-foo/bar",
If you've just forked the easiest way to do this is edit it right inside github.
如果你刚刚分叉,最简单的方法就是在 github 中编辑它。
回答by Magus
In my case, I use Symfony2.3.x and the minimum-stability parameter is by default "stable" (which is good). I wanted to import a repo not in packagist but had the same issue "Your requirements could not be resolved to an installable set of packages.". It appeared that the composer.json in the repo I tried to import use a minimum-stability "dev".
就我而言,我使用 Symfony2.3.x 并且最小稳定性参数默认为“稳定”(这很好)。我想导入一个不在 packagist 中的存储库,但遇到了同样的问题“您的要求无法解析为一组可安装的软件包。”。我尝试导入的 repo 中的 composer.json 似乎使用了最低稳定性“dev”。
So to resolve this issue, don't forget to verify the minimum-stability. I solved it by requiring a dev-masterversion instead of masteras stated in this post.
因此,要解决此问题,请不要忘记验证minimum-stability. 我通过要求一个dev-master版本而不是master这篇文章中所述来解决它。
回答by Clarence
If you want to use a composer.jsonfrom GitHub you would look at this example(under the VCS section).
如果您想使用composer.json来自 GitHub 的a ,您可以查看此示例(在 VCS 部分下)。
The package section is for packages that do not have the composer.json. However, you didn't follow that example as well or it would also have worked. Do read what it says about package repositories:
包部分适用于没有composer.json. 但是,您也没有遵循该示例,否则它也会起作用。请阅读它关于包存储库的内容:
Basically, you define the same information that is included in the composer repository's
packages.json, but only for a single package. Again, the minimum required fields are name, version, and either of dist or source.
基本上,您定义包含在 composer 存储库中的相同信息
packages.json,但仅针对单个包。同样,最少需要的字段是名称、版本以及 dist 或 source。

