使用Composer,可以在多个项目之间共享依赖项吗? [英] Using Composer, can dependencies be shared between multiple projects?

查看:124
本文介绍了使用Composer,可以在多个项目之间共享依赖项吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是Composer的一名新手。我一直在尝试,因为它听起来很棒,主要是因为Zend Framework 2使用了它。

First of all, I'm a complete newbie to Composer. I've been trying it out since it's a sounds awesome and mainly because Zend Framework 2 uses it.

Zend Framework 2实际上也是该线程的原因。

Zend Framework 2 is actually also the reason for this thread.

它了解Composer的基础知识。但是在我当前的服务器设置下,我有一个请求,我似乎无法弄清楚。

It get the basics of Composer. But with my current server setup I have a request, which I can't seem to figure out if possible.

让我解释一下。

我有多个Zend Framework 2项目:

I have multiple Zend Framework 2 projects:


  • / home / morten / sites / Project-1 /

  • / home / morten / sites / Project-2 /

  • / home / morten / sites / Project-3 /

所有这些项目都应运行ZF2。在每个项目中运行composer时-每个人都可以分别下载ZF2库文件。我的设置有点多余。

All of these projects should be running ZF2. When running composer in each project - each of them get their own separate download of the ZF2 Library files. Which is a bit redundant with my setup.

我在以下位置完整下载了ZF2库:
/ var / www / shared / Zend /

I have a complete and up-to-date download of ZF2 Library located at: /var/www/shared/Zend/

我的php.ini将该路径添加到PHP的include_path中,因此,整个ZF2库可用于所有三个项目。

And my php.ini has that path added to PHP's include_path, so the whole ZF2 library is available for all the three projects.

可以在其设置中使用Composer。因为如果我尝试更改内容并尝试在composer文件中进行尝试,那么它只是重新下载Zend,因为它是其他模块的必需组件。

IS IT POSSIBLE to make Composer use in it's setup. Because if I try to change stuff and try things out in the composer files, then it just re-downloads Zend because it's a required component for other modules.

可以吗?完成了吗如果是的话,我该怎么办?

Can this be done? And if yes, how can I do it?

希望我已经向自己解释了自己,足以让你们了解我想要做的事情:)

Hope I have explained myself good enough for you guys to understand what I'm trying to do and want :)

非常感谢!

致谢,
Morten

Regards, Morten

推荐答案

您也许可以将一个 composer.json 文件存储在 / var / www / shared / Zend ,您可以在其中放置依赖项并使用Composer对其进行管理。然后所有项目都可以包含相同的自动加载器。

You might be able to have one composer.json file stored in /var/www/shared/Zend, where you would put your dependencies and use Composer do manage them. Then all projects could include the same autoloader.

但是,我不建议采用这种方法:

However, I wouldn't recommend that approach:


  • 您项目的依赖项( composer.json )应该与项目一起存储。如果要将项目安装在其他位置(例如,如果要将一个项目移动到另一台服务器),则缺少 composer.json 来安装所需的依赖项。

  • 如果不更新所有其他项目的依赖关系,就不可能更新一个项目的依赖关系。如果要在Project 1中引入新功能,而该功能需要具有特定依赖性的新版本,则所有其他项目也将获得该新版本-如果Project 2所依赖的较旧功能是不再受支持。由于每个依赖项只能有一个版本,因此两个项目不可能具有相同依赖项的不同版本。

  • Your project's dependencies (composer.json) should be stored with your project. If you want to install your project somewhere else (for instance if you want to move one project to another server), you are missing the composer.json to install the required dependencies.
  • It will not be possible to update the dependencies of one project, without updating the dependencies of all other projects. If you want to introduce a new feature in Project 1, which requires a new version of a certain dependency, all other projects will get this new version as well - which might introduce backward compatibility breaks if an older feature that Project 2 relies on, is no longer supported. Since you can only have one version of each dependency, it is not possible for two projects to have different versions of the same dependency.

如果所有项目都具有完全相同的功能和代码,则这种方法可能很有用,但是在那种情况下,您应该将它们合并到一个项目中。

This approach might be useful if all projects will have the exact same functionality and code, but in that case you should probably merge them into one project.

在所有其他情况下,我建议按照应该使用的方式使用Composer:为所有项目提供自己的 composer.json 文件,并让Composer在每个项目的<$中下载每个项目的依赖项c $ c>供应商目录。缺点是,这将花费您更多的磁盘空间,但作为回报,您将获得很大的灵活性。

In all other cases, I would suggest to use Composer the way it's supposed to be used: give all projects their own composer.json file and let Composer download the dependencies per project, in each project's vendor directory. The downside is that it'll cost you a little more disk space, but you'll get a lot of flexibility for it in return.

这篇关于使用Composer,可以在多个项目之间共享依赖项吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆