简体   繁体   English

在Symfony2中使用Composer添加CSS或JS库作为依赖项的正确方法是什么?

[英]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? 这样做的正确方法是什么?

You should use Bower by Twitter. 你应该通过Twitter使用Bower It is a package manager for HTML, CSS and Javascript. 它是HTML,CSS和Javascript的包管理器。 It was created to address this very issue you are having. 它的创建是为了解决您遇到的这个问题。

EDIT: As of now, there are very good package managers for JS Libraries such as Bower, Jam or Component. 编辑:截至目前,JS库的包管理器非常好,如Bower,Jam或Component。

Versioning system 版本控制系统

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 Embeding

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. 例如,可以重新声明一个函数,该函数缺少其中一个包的关键功能并打破它。

Symfony is a PHP framework. Symfony是一个PHP框架。

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) 您可以通过两种我能想到的方式来解决这个问题:(可能有更多更好的方法来解决这个问题,我只想到这两个,将它们作为建议阅读)

  1. Create a PHP wrapper around the javascript library and including it (although this potentially creates the same problem if another bundle decides to do the same thing but giving the package a different name) 围绕javascript库创建一个PHP包装器并将其包含在内(尽管如果另一个bundle决定做同样的事情但给包提供不同的名称,这可能会产生同样的问题)
  2. Creating a bundle which requires the javascript library as a third party dependency through composer. 通过composer创建一个需要javascript库作为第三方依赖的bundle。 Since the javascript library won't probably have a composer.json file in it's repository (sometimes they live as a standalone minified file), this can be accomplished by creating a custom composer installer , forking the javascript repository (in gitHub for instance) adding a composer.json to it, etc... However, you will need to constantly maintain and upgrade the said library, which can be troublesome. 由于javascript库可能在其存储库中没有composer.json文件(有时它们作为独立的缩小文件存在),这可以通过创建自定义编写器安装程序 ,分叉javascript存储库(例如在gitHub中)来实现一个composer.json,等等...但是,你需要不断维护和升级所述库,这可能很麻烦。

You will have to keep in mind that: 你必须记住:

  1. JS and CSS libs have to be exposed publicly, so that the client can access it (security considerations) 必须公开公开JS和CSS库,以便客户端可以访问它(安全注意事项)
  2. Symfony is a PHP framework and deals with server-side packages. Symfony是一个PHP框架,处理服务器端软件包。 JS/CSS are client side. JS / CSS是客户端。 This as to be taken in consideration so it works properly. 考虑到这一点,它可以正常工作。
  3. One of the main ideas behind symfony (as with other PHP Frameworks) is code reusability within and between projects. symfony背后的主要思想之一(与其他PHP框架一样)是项目内部和项目之间的代码可重用性。 Pure Javascript Library are reusable in themselves. 纯Javascript库本身可以重用。 They are usually self contained. 它们通常是独立的。 Besides, there is no real gain in "bundling" a JS library from the server side. 此外,从服务器端“捆绑”JS库并没有真正的好处。 You don't need any kind of bundle to achieve reusability. 您不需要任何类型的捆绑来实现可重用性。

My Approach 我的方法

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依赖关系而不用担心这个问题。

My sugestion 我的消化

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM