[英]What is the correct way of adding CSS or JS libraries as dependencies with Composer in Symfony2?
In the Symfony 2 docs it's said: 在Symfony 2文档中,它说:
A bundle should not embed third-party libraries written in JavaScript, CSS, or any other language.
捆绑包不应嵌入用JavaScript,CSS或任何其他语言编写的第三方库。
Then how should I do that? 那我该怎么做? I want to install Twitter Bootstrap, DataTables, and many other things as dependencies using Composer.
我想使用Composer安装Twitter Bootstrap,DataTables和许多其他东西作为依赖项。 But the only way I can think of is creating a bundle and embedding them.
但我能想到的唯一方法是创建一个包并嵌入它们。
What is the correct way to do this? 这样做的正确方法是什么?
EDIT: As of now, there are very good package managers for JS Libraries such as Bower, Jam or Component. 编辑:截至目前,JS库的包管理器非常好,如Bower,Jam或Component。
Semantic Versioning - Composer advises to use the semantic versioning system. 语义版本控制 - Composer建议使用语义版本控制系统。 It uses a XYZ setting, in which X is the major version, Y is the minor version and Z is the patch version.
它使用XYZ设置,其中X是主要版本,Y是次要版本,Z是补丁版本。 Y and Z should always be backwards compatible while X reflects changes in code which MIGHT break backwards compatibility.
Y和Z应始终向后兼容,而X反映代码的变化,MIGHT向后兼容。
Embeding should be read as copy and pasting the code (and binary) as part of your library, rather then requiring it as a third party (vendor) package/bundle. 嵌入应该被视为复制并粘贴代码(和二进制)作为库的一部分,而不是将其作为第三方(供应商)包/包。 Its like including query.js in a resource folder or copying and pasting propel code to a folder inside your bundle.
它包括在资源文件夹中包含query.js或将推进代码复制并粘贴到包内的文件夹中。
Why not embed 3rd party libs 为什么不嵌入第三方库
A bundle should not embed third-party libraries written in JavaScript, CSS, or any other language.
捆绑包不应嵌入用JavaScript,CSS或任何其他语言编写的第三方库。
This statement comes from a best practice point of view. 本声明来自最佳实践观点。 Embeding (as in copy/paste) third party libraries of any kind (PHP libs especially) is usually not a good idea.
嵌入(如复制/粘贴)任何类型的第三方库(特别是PHP库)通常不是一个好主意。 For instance, imagine that BUNDLE A uses LIBRARY FOO v1.4.1, and BUNDLE B also uses LIBRARY FOO but with a different version v1.5.2.
例如,假设BUNDLE A使用LIBRARY FOO v1.4.1,BUNDLE B也使用LIBRARY FOO但使用不同版本v1.5.2。 If any of the BUNDLES (A or B) embeds FOO lib, they might (most probably will) become incompatible.
如果任何BUNDLES(A或B)嵌入了FOO lib,它们可能(很可能会)变得不兼容。 For instance, php classes and functions cannot be redeclared.
例如,php类和函数不能重新声明。 Any of the bundles can, of course, use workarounds to mitigate this problem, such as namespacing their version of FOO or autoloading rules, but this can rise other problems as well besides surely increasing memory usage as there are 2 versions of the same thing parsed by PHP.
当然,任何捆绑包都可以使用变通方法来缓解这个问题,比如命名空间版本的FOO或自动加载规则,但这会增加其他问题,除了肯定会增加内存使用量,因为有两个版本的同一个东西被解析通过PHP。
If a PHP package does not follow this best practice, the errors that arise are usually easy to spot (with error: cannot redeclcare function blablabla). 如果PHP包不遵循这个最佳实践,那么出现的错误通常很容易被发现(有错误:无法重新定位函数blablabla)。 With Javascript Libraries, however, that is not true.
但是,对于Javascript库,情况并非如此。 You can redeclare functions (as they are object properties).
您可以重新声明函数(因为它们是对象属性)。 So if now FOO is a JS Lib instead, and BUNDLE A and B embed them into their libraries, when they are included, strange problems can arise.
因此,如果现在FOO是JS Lib,而BUNDLE A和B将它们嵌入到它们的库中,当它们被包含在内时,会出现奇怪的问题。 For instance, a function can be redeclared that lacks a crucial functionality for one of the bundles and break it.
例如,可以重新声明一个函数,该函数缺少其中一个包的关键功能并打破它。
It deals with PHP libraries/bundles. 它处理PHP库/包。 Symfony advises to require a library as dependency instead of embedding it since it uses Composer as a Package manager, which takes care of downloading and loading the require packages.
Symfony建议要求库作为依赖而不是嵌入它,因为它使用Composer作为包管理器,它负责下载和加载require包。 As far as I remember, when 2 bundles/packages use the same library, if they have different version requirements, the most actual is used, unless its backwards incompatible.
据我所知,当2个软件包/软件包使用相同的库时,如果它们具有不同的版本要求,则使用最实际的版本,除非它的向后不兼容。 Composer then reports a conflict that you have to manually resolve.
然后,Composer会报告您必须手动解决的冲突。
However... There is no way to handle javascript libraries properly. 但是......没有办法正确处理JavaScript库。 That's because Composer is a package for PHP libraries.
那是因为Composer是PHP库的包。 You can workaround this in two ways I can think of: (there are probably more and best ways to handle this, I just thought of these two, read them as suggestions)
您可以通过两种我能想到的方式来解决这个问题:(可能有更多更好的方法来解决这个问题,我只想到这两个,将它们作为建议阅读)
You will have to keep in mind that: 你必须记住:
Since the composer system is so appealing, specially when deploying bundles/packages/libraries to other people, my approach to using third party javascript/css libraries was to create a dependency manager specific to JS/CSS that other packages/bundles could rely on to take care of their JS/CSS dependencies without worrying about this. 由于编写器系统非常吸引人,特别是在将bundle / packages / library部署到其他人时,我使用第三方javascript / css库的方法是创建一个特定于JS / CSS的依赖管理器 ,其他包/ bundle可以依赖它照顾他们的JS / CSS依赖关系而不用担心这个问题。
If you are planing to release your project to the public, namely as a symfony bundle, you should plan carefully how to approach this. 如果您计划向公众发布项目,即作为symfony包,您应该仔细计划如何处理这个问题。 If your project is self contained (personal use or to a client, not widespread use) then this has much less relevancy since you (the programmer) have total control in what third party tools you use and include in your project.
如果您的项目是自包含的(个人使用或客户端,而不是广泛使用),则由于您(程序员)完全控制您使用的第三方工具并将其包含在项目中,因此其相关性要低得多。 These are just best practice "suggestions" to avoid future problems.
这些只是避免未来问题的最佳实践“建议”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.