问题描述
我想在我的 Zend Framework PHP 项目中复制 Rails 资产管道功能.我认为可以将 Sprockets gem 作为独立服务使用,但我不确定如何正确配置它.我对将 Sprockets 移植到 PHP 或使用 Sprockets 的 PHP 移植不感兴趣.Sprockets rubygem 已经拥有我需要的一切.我只需要弄清楚如何在非 ruby 环境中进行设置.
I would like to duplicate the Rails asset pipeline feature in my Zend Framework PHP project. I think that it's possible to use the Sprockets gem as a standalone service but I am not sure how to configure it properly. I'm not interested in porting Sprockets to PHP, nor using a PHP port of Sprockets. The Sprockets rubygem already has everything I need. I just need to figure out how to set it up in a non-ruby environment.
更新:我已经知道如何将 Sprockets 作为 Rack 应用程序运行.现在我对开发和生产环境之间的差异感兴趣.Rails 视图助手如何在开发中生成所有 和
标签,并在生产中对单个文件进行指纹识别?
Update: I have figured out how to run Sprockets as a Rack application. Now I'm interested in the differences between development and production environments. How does the Rails view helper generate all the <link>
and <script>
tags in development and fingerprint a single file in production?
推荐答案
您可能只需要深入了解 Sprockets 和 Sprockets 与Rails 以便真正为您的问题提出一个好的解决方案,但我希望其中一些指针会有所帮助.
You're probably just going to have to dig into the source for Sprockets and Sprockets integration with Rails in order to really come up with a good solution for your problem, but I hope some of these pointers will help.
首先,查看Sprockets::Helpers::RailsHelper#javascript_include_tag
:
1 def javascript_include_tag(*sources)
2 options = sources.extract_options!
3 debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
4 body = options.key?(:body) ? options.delete(:body) : false
5 digest = options.key?(:digest) ? options.delete(:digest) : digest_assets?
6
7 sources.collect do |source|
8 if debug && asset = asset_paths.asset_for(source, 'js')
9 asset.to_a.map { |dep|
10 super(dep.pathname.to_s, { :src => path_to_asset(dep, :ext => 'js', :body => true, :digest => digest) }.merge!(options))
11 }
12 else
13 super(source.to_s, { :src => path_to_asset(source, :ext => 'js', :body => body, :digest => digest) }.merge!(options))
14 end
15 end.join("\n").html_safe
16 end
概括地说,此方法执行以下操作:
At a high level, this method does the following:
- 确定是将所有内容连接到一个文件中还是单独包含所有资产(第 3 行).
- 确定我们是否应该在资产文件名中包含摘要字符串(第 5 行).
- 对于每个给定的源文件,检索与该源文件对应的
Sprockets::Asset
对象(第 8 行). - 如果我们正在调试,请使用该方法所需的每个
Asset
调用超类方法.Sprockets::Asset#to_a
返回这样一个数组(第 9-10 行). - 如果我们没有调试(或者如果我们由于某种原因无法检索
Asset
对象),请使用顶级Asset
调用超类方法(或源文件名作为字符串)(第 13 行).
- Determine whether to concatenate everything into a single file or include all assets individually (line 3).
- Determine whether we should include the digest string in the asset filename (line 5).
- For each given source file, retrieve the
Sprockets::Asset
object corresponding to that source file (line 8). - If we're debugging, call the superclass method with each
Asset
required by this one.Sprockets::Asset#to_a
returns such an array (lines 9–10). - If we're not debugging (or if we fail to retrieve an
Asset
object for some reason), call the superclass method with the top-levelAsset
(or the source filename as a string) (line 13).
其中许多方法非常简单,仅取决于您设置的环境.例如,digest_assets?
:
Many of those methods are pretty simple and only depend on the environment you set up. For example, digest_assets?
:
def digest_assets?
Rails.application.config.assets.digest
end
某些配置保存在 Rails.application.assets
中,它本身就是一个 Sprockets::Environment
对象.您实际上可以在 Rails 控制台上使用它来熟悉它(我强烈推荐 awesome_print gem 如果你还不熟悉它):
Some of this configuration is saved in Rails.application.assets
, which itself is a Sprockets::Environment
object. You can actually play with this on the Rails console to get familiar with it (I'd highly recommend the awesome_print gem if you aren't already familiar with it):
1.9.3p194 :001 > Rails.application.assets.class
Sprockets::Environment < Sprockets::Base
1.9.3p194 :002 > Rails.application.assets['application.js'].class
Sprockets::BundledAsset < Sprockets::Asset
1.9.3p194 :003 > Rails.application.assets['application.js'].to_a.map(&:digest)
[
[ 0] "6bb424b2409c6a5fb28acd15cc184b16",
[ 1] "0ff3e5680ead3dadeee021c144835311",
[ 2] "4c908739f93e83bda6b5e84f7ab10a29",
[ 3] "319003f54b9408b4e41b0199e1848423",
[ 4] "3f52cd966b6bb99a8f7994f5dcd7767f",
[ 5] "c50a6aac16f6a69deeb722fe51e36c54",
# ...
]
因此,本质上,Sprockets 负责确定资产的依赖关系、连接和消化,而 Rails 只是将它与其助手联系在一起.看起来您应该能够以相当简单的方式在您的框架中复制此行为.
So essentially, Sprockets is responsible for determining dependencies, concatenation, and digesting of your assets, and Rails just ties it together with its helpers. It looks like you should be able to replicate this behavior in your framework in a reasonably straightforward manner.
这篇关于使用链轮作为 PHP 应用程序的独立服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!