rails的初始化过程

130 篇文章 0 订阅
Rails启动过程

在每个应用程序的/public目录下,都含有:dispatch.cgi、dispatch.fcgi、dispatch.rb 3个分发文件。系统会根据我们的配置执行其中相应的文件,调用不同的处理方式(CGI,FastCGI或是Ruby方式),同时该文件会加载整个rails环境。3个文件中的内容基本一样,仅对其中一个分发文件进行探讨!

这几个分发文件,首先通过如下代码读入/config目录下的environment.rb文件。
代码:
Java代码   收藏代码
  1. require File.dirname(_FIFE_)+"/../config/enviroment" unless defined?(RAILS_ROOT)  


而在enviroment.rb文件中,一般可以查看Rails版本。
代码:
Java代码   收藏代码
  1. RAILS_GEM_VERSION = '2.0.2' # unless defined? RAILS_GEM_VERSION  


由于没有定RAILS_ROOT会继续调用/config目录下的boot.rb文件:
代码:
Java代码   收藏代码
  1. require File.join(File.dirname(__FILE__), 'boot')  


查看root.rb文件,它做了这样几件事情。一:设置环境变量
代码:
Java代码   收藏代码
  1. RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)  


二:boot.rb接下来会检查是否存在#{RAILS_ROOT}/vendor/rails目录。因为rails应用程序的运行环境和版本关系比较大。如果存在boot.rb会启动该目录下的Rails初始化程序。否则boot.rb会加载rubygems并搜索environment.rb文件。如果不存在常量,boot.rb会尝试初始化系统最近安装的rails版本。

三:定义了正确的初始化程序路径。boot.rb会调用Rails模块下的Initializer类中的类方法run。
代码:Rails模块
Java代码   收藏代码
  1. # Don't change this file!  
  2. # Configure your app in config/environment.rb and config/environments/*.rb  
  3.   
  4. RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)  
  5.   
  6. module Rails  
  7.   class << self  
  8.     def boot!  
  9.       unless booted?  
  10.         preinitialize  
  11.         pick_boot.run  
  12.       end  
  13.     end  
  14.   
  15.     def booted?  
  16.       defined? Rails::Initializer  
  17.     end  
  18.   
  19.     def pick_boot  
  20.       (vendor_rails? ? VendorBoot : GemBoot).new  
  21.     end  
  22.   
  23.     def vendor_rails?  
  24.       File.exist?("#{RAILS_ROOT}/vendor/rails")  
  25.     end  
  26.   
  27.     # FIXME : Ruby 1.9  
  28.     def preinitialize  
  29.       load(preinitializer_path) if File.exists?(preinitializer_path)  
  30.     end  
  31.   
  32.     def preinitializer_path  
  33.       "#{RAILS_ROOT}/config/preinitializer.rb"  
  34.     end  
  35.   end  
  36.   
  37.   class Boot  
  38.     def run  
  39.       load_initializer  
  40.       Rails::Initializer.run(:set_load_path)  
  41.     end  
  42.   end  
  43.   
  44.   class VendorBoot < Boot  
  45.     def load_initializer  
  46.       require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"  
  47.     end  
  48.   end  
  49.   
  50.   class GemBoot < Boot  
  51.     def load_initializer  
  52.       self.class.load_rubygems  
  53.       load_rails_gem  
  54.       require 'initializer'  
  55.     end  
  56.   
  57.     def load_rails_gem  
  58.       if version = self.class.gem_version  
  59.         gem 'rails', version  
  60.       else  
  61.         gem 'rails'  
  62.       end  
  63.     rescue Gem::LoadError => load_error  
  64.       $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)  
  65.       exit 1  
  66.     end  
  67.   
  68.     class << self  
  69.       def rubygems_version  
  70.         Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion  
  71.       end  
  72.   
  73.       def gem_version  
  74.         if defined? RAILS_GEM_VERSION  
  75.           RAILS_GEM_VERSION  
  76.         elsif ENV.include?('RAILS_GEM_VERSION')  
  77.           ENV['RAILS_GEM_VERSION']  
  78.         else  
  79.           parse_gem_version(read_environment_rb)  
  80.         end  
  81.       end  
  82.   
  83.       def load_rubygems  
  84.         require 'rubygems'  
  85.   
  86.         unless rubygems_version >= '0.9.4'  
  87.           $stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)  
  88.           exit 1  
  89.         end  
  90.   
  91.       rescue LoadError  
  92.         $stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)  
  93.         exit 1  
  94.       end  
  95.   
  96.       def parse_gem_version(text)  
  97.         $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/  
  98.       end  
  99.   
  100.       private  
  101.         def read_environment_rb  
  102.           File.read("#{RAILS_ROOT}/config/environment.rb")  
  103.         end  
  104.     end  
  105.   end  
  106. end  
  107.   
  108. # All that for this:  
  109. Rails.boot!  

模块有两个类:Initializer和Configuration。
Initializer类负责处理Rails的配置选项,并设置Rails的加载文件的路径。
Configuration维护Rails环境的配置参数。

Initializer 的run方法
Java代码   收藏代码
  1. def self.run(command = :process, configuration = Configuration.new)  
  2.      yield configuration if block_given?  
  3.      initializer = new configuration  
  4.      initializer.send(command)  
  5.      initializer  
  6. end  


Configuration类会加载应用程序中相关的目录,并加载相关的文件
如:
Java代码   收藏代码
  1. def default_load_paths  
  2.         paths = []  
  3.   
  4.         # Add the old mock paths only if the directories exists  
  5.         paths.concat(Dir["#{root_path}/test/mocks/#{environment}"]) if File.exists?("#{root_path}/test/mocks/#{environment}")  
  6.   
  7.         # Add the app's controller directory  
  8.         paths.concat(Dir["#{root_path}/app/controllers/"])  
  9.   
  10.         # Then components subdirectories.  
  11.         paths.concat(Dir["#{root_path}/components/[_a-z]*"])  
  12.   
  13.         # Followed by the standard includes.  
  14.         paths.concat %w(  
  15.           app  
  16.           app/models  
  17.           app/controllers  
  18.           app/helpers  
  19.           app/services  
  20.           components  
  21.           config  
  22.           lib  
  23.           vendor  
  24.         ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }  
  25.   
  26.         paths.concat builtin_directories  
  27.       end  

你也可以做相应的修改。

初始化过程结束返回到enviroment.rb文件。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值