php Composer 需要本地包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29994088/
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
Composer require local package
提问by Sammitch
I've got a couple of libraries [Foo and Bar] that I'm developing in concert, but are still technically separate things. Previously I've just re-defined the autoloader to like "Foo\\": "../Foo/src"
, but now that I've added a Guzzle dependency to Foo, Bar flips it's lid because it's not one of its dependencies.
我有几个正在协同开发的库 [Foo 和 Bar],但在技术上仍然是独立的。以前我刚刚将自动加载器重新定义为 like "Foo\\": "../Foo/src"
,但现在我已经向 Foo 添加了 Guzzle 依赖项,Bar 翻转了它的盖子,因为它不是它的依赖项之一。
Directory structure:
目录结构:
/home/user/src/
Foo/
src/
FooClient.php
composer.json
Bar/
src/
BarClient.php
composer.json
Theoretical Autoload Statement: [in Bar/composer.json]
理论自动加载声明:[在 Bar/composer.json 中]
"require": {
"local": "../Foo/composer.json"
}
Example code:
示例代码:
require('vendor/autoload.php');
$f = new \Bar\BarClient(new \Foo\FooClient());
How can I resolve this without setting up a local Composer repo? I want to maintain these as separate packages, just that one requires the other, and therefor processes the other's dependencies.
如何在不设置本地 Composer 存储库的情况下解决此问题?我想将这些作为单独的包维护,只是一个需要另一个,因此处理另一个的依赖项。
post-answer edit:
回复后编辑:
Thanks to infomaniac I've done the following:
感谢 infomaniac 我做了以下事情:
Initialized the git repo:
初始化git repo:
cd ~/src/Foo && git init && echo -e "vendor\ncomposer.lock" > .gitignore && git add ./ && git commit -m "Initial Commit"
Added the composer config:
添加了作曲家配置:
"require": {
"sammitch/foo": "dev-master"
},
"repositories": [{
"type": "vcs",
"url": "/home/sammitch/src/Foo"
}],
And then composer update
!
然后composer update
!
采纳答案by infomaniac
You can use Composer's repositoriesfeature
您可以使用 Composer 的存储库功能
https://getcomposer.org/doc/05-repositories.md#loading-a-package-from-a-vcs-repository
https://getcomposer.org/doc/05-repositories.md#loading-a-package-from-a-vcs-repository
Instead of using the http format, specify a file path on disk.
不使用 http 格式,而是指定磁盘上的文件路径。
回答by Dhiraj Gupta
The way to link to a local, in-development package is to first add in your main project's composer.json
a repository, like this:
该方式链接到一个地方,在开发包是在您的主项目的第一个附加的composer.json
一个库,就像这样:
"repositories": [
{
"type": "path",
"url": "/full/or/relative/path/to/development/package"
}
]
You also need to either have a version specified in your development package's composer.json
or the way I do it is to require the package using @dev
, like this:
您还需要在您的开发包中指定一个版本,composer.json
或者我这样做的方式是要求使用包@dev
,如下所示:
composer require "vendorname/packagename @dev"
It should output:
它应该输出:
- Installing vendor/packagename (dev-develop)
Symlinked from /full/or/relative/path/to/development/package
The @dev
in the require command is important, composer uses this to pickup the source code and symlink it to your new package.
将@dev
在需要指令重要,作曲家用这个皮卡的源代码,并将其符号链接到您的新包。
It's a stability flag added to the version constraint (see package link).
这是添加到版本约束的稳定性标志(参见包链接)。
These allow you to further restrict or expand the stability of a package beyond the scope of the minimum-stabilitysetting.
这些允许您进一步限制或扩展包的稳定性超出最小稳定性设置的范围。
The minimum-stability flags are:
最低稳定性标志是:
Available options (in order of stability) are
dev
,alpha
,beta
,RC
, andstable
.
可用的选项(在稳定的顺序)
dev
,alpha
,beta
,RC
,和stable
。
回答by whyer
After spending some time, I finally understood the solution. Maybe it'll be useful for someone like me and will save you some time, so I've decided that I have to share it here.
花了一些时间,我终于明白了解决方案。也许它对像我这样的人有用并且会为您节省一些时间,所以我决定必须在这里分享。
Assuming that you have the following directory structure (relative to your project root directory):
假设您具有以下目录结构(相对于您的项目根目录):
composer.json
config
config/composition-root.php
local
local/bar-project
local/bar-project/composer.json
local/bar-project/src
local/bar-project/src/Bar.php
public
public/index.php
src
src/Foo.php
In this example you may see that the local
folder is meant for nested projects of your company, e.g. bar-project
. But you could configure any other layout, if you wish.
在此示例中,您可能会看到该local
文件夹用于您公司的嵌套项目,例如bar-project
. 但是,如果您愿意,您可以配置任何其他布局。
Each project has to have its own composer.json
file, e.g. root composer.json
and local/bar-project/composer.json
. Then their contents would be as follows:
每个项目都必须有自己的composer.json
文件,例如 rootcomposer.json
和local/bar-project/composer.json
. 那么它们的内容如下:
(root composer.json
:)
(根composer.json
:)
{
"name": "your-company/foo-project",
"require": {
"php": "^7",
"your-company/bar-project": "@dev"
},
"autoload": {
"psr-4": {
"YourCompany\FooProject\": "src/"
}
},
"repositories": [
{
"type": "path",
"url": "local/bar-project"
}
]
}
(local/bar-project/composer.json
:)
( local/bar-project/composer.json
:)
{
"name": "your-company/bar-project",
"autoload": {
"psr-4": {
"YourCompany\BarProject\": "src/"
}
}
}
If, for example, you wish to locate each project in a separate sibling directory, as follows:
例如,如果您希望将每个项目定位在单独的同级目录中,如下所示:
your-company
your-company/foo-project
your-company/foo-project/composer.json
your-company/foo-project/config
your-company/foo-project/config/composition-root.php
your-company/foo-project/public
your-company/foo-project/public/index.php
your-company/foo-project/src
your-company/foo-project/src/Foo.php
your-company/bar-project
your-company/bar-project/composer.json
your-company/bar-project/src
your-company/bar-project/src/Bar.php
- then you need to link to respective directory in repositories
section:
- 然后您需要链接到repositories
部分中的相应目录:
"repositories": [
{
"type": "path",
"url": "../bar-project"
}
]
After that don't forget to composer update
(or even rm -rf vendor && composer update -v
as the docs suggest)!
Under the hood, composer will create a vendor/your-company/bar-project
symlink that targets to local/bar-project
(or ../bar-project
respectively).
在那之后不要忘记composer update
(甚至rm -rf vendor && composer update -v
如文档所建议的那样)!在幕后,作曲家将创建一个vendor/your-company/bar-project
指向local/bar-project
(或../bar-project
分别)的符号链接。
Assuming that your public/index.php
is just a front controller
, e.g.:
假设你public/index.php
只是一个front controller
,例如:
<?php
require_once __DIR__ . '/../config/composition-root.php';
Then your config/composition-root.php
would be:
那么你config/composition-root.php
将是:
<?php
declare(strict_types=1);
use YourCompany\BarProject\Bar;
use YourCompany\FooProject\Foo;
require_once __DIR__ . '/../vendor/autoload.php';
$bar = new Bar();
$foo = new Foo($bar);
$foo->greet();