Rails启动过程

[color=red][size=medium]Rails启动过程[/size][/color]

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

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


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


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


查看root.rb文件,它做了这样几件事情。一:设置环境变量
代码:
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模块
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb

RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)

module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end

def booted?
defined? Rails::Initializer
end

def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end

def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end

# FIXME : Ruby 1.9
def preinitialize
load(preinitializer_path) if File.exists?(preinitializer_path)
end

def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end

class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end

class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
end
end

class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end

def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$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.)
exit 1
end

class << self
def rubygems_version
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
end

def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end

def load_rubygems
require 'rubygems'

unless rubygems_version >= '0.9.4'
$stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end

rescue LoadError
$stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end

def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end

private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end

# All that for this:
Rails.boot!

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

Initializer 的run方法

def self.run(command = :process, configuration = Configuration.new)
yield configuration if block_given?
initializer = new configuration
initializer.send(command)
initializer
end


Configuration类会加载应用程序中相关的目录,并加载相关的文件
如:
def default_load_paths
paths = []

# Add the old mock paths only if the directories exists
paths.concat(Dir["#{root_path}/test/mocks/#{environment}"]) if File.exists?("#{root_path}/test/mocks/#{environment}")

# Add the app's controller directory
paths.concat(Dir["#{root_path}/app/controllers/"])

# Then components subdirectories.
paths.concat(Dir["#{root_path}/components/[_a-z]*"])

# Followed by the standard includes.
paths.concat %w(
app
app/models
app/controllers
app/helpers
app/services
components
config
lib
vendor
).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }

paths.concat builtin_directories
end

你也可以做相应的修改。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值