initializer.rb 36.2 KB
Newer Older
1
require 'logger'
2
require 'set'
3 4 5 6 7 8 9
require 'pathname'

$LOAD_PATH.unshift File.dirname(__FILE__)
require 'railties_path'
require 'rails/version'
require 'rails/plugin/locator'
require 'rails/plugin/loader'
10
require 'rails/gem_dependency'
11
require 'rails/rack'
12

13

14
RAILS_ENV = (ENV['RAILS_ENV'] || 'development').dup unless defined?(RAILS_ENV)
15 16

module Rails
D
David Heinemeier Hansson 已提交
17 18 19 20 21
  class << self
    # The Configuration instance used to configure the Rails environment
    def configuration
      @@configuration
    end
J
Joshua Peek 已提交
22

D
David Heinemeier Hansson 已提交
23 24 25
    def configuration=(configuration)
      @@configuration = configuration
    end
J
Joshua Peek 已提交
26 27 28 29 30 31 32 33 34

    def initialized?
      @initialized || false
    end

    def initialized=(initialized)
      @initialized ||= initialized
    end

D
David Heinemeier Hansson 已提交
35
    def logger
36 37 38 39 40
      if defined?(RAILS_DEFAULT_LOGGER)
        RAILS_DEFAULT_LOGGER
      else
        nil
      end
D
David Heinemeier Hansson 已提交
41
    end
J
Joshua Peek 已提交
42

43 44 45 46 47 48 49
    def backtrace_cleaner
      @@backtrace_cleaner ||= begin
        # Relies on ActiveSupport, so we have to lazy load to postpone definition until AS has been loaded
        require 'rails/backtrace_cleaner'
        Rails::BacktraceCleaner.new
      end
    end
J
Joshua Peek 已提交
50

51 52
    def root
      Pathname.new(RAILS_ROOT) if defined?(RAILS_ROOT)
D
David Heinemeier Hansson 已提交
53
    end
J
Joshua Peek 已提交
54

D
David Heinemeier Hansson 已提交
55
    def env
56
      @_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV)
D
David Heinemeier Hansson 已提交
57
    end
J
Joshua Peek 已提交
58

D
David Heinemeier Hansson 已提交
59 60 61
    def cache
      RAILS_CACHE
    end
62

63 64 65 66
    def version
      VERSION::STRING
    end

67
    def public_path
68
      @@public_path ||= self.root ? File.join(self.root, "public") : "public"
69 70 71 72 73
    end

    def public_path=(path)
      @@public_path = path
    end
74
  end
J
Joshua Peek 已提交
75

76 77 78 79
  # The Initializer is responsible for processing the Rails configuration, such
  # as setting the $LOAD_PATH, requiring the right frameworks, initializing
  # logging, and more. It can be run either as a single command that'll just
  # use the default configuration, like this:
80 81 82
  #
  #   Rails::Initializer.run
  #
83 84
  # But normally it's more interesting to pass in a custom configuration
  # through the block running:
85 86
  #
  #   Rails::Initializer.run do |config|
87
  #     config.frameworks -= [ :action_mailer ]
88 89
  #   end
  #
90 91
  # This will use the default configuration options from Rails::Configuration,
  # but allow for overwriting on select areas.
92
  class Initializer
93
    # The Configuration instance used by this Initializer instance.
94
    attr_reader :configuration
95 96 97

    # The set of loaded plugins.
    attr_reader :loaded_plugins
98 99 100 101

    # Whether or not all the gem dependencies have been met
    attr_reader :gems_dependencies_loaded

102 103 104 105 106 107 108
    # Runs the initializer. By default, this will invoke the #process method,
    # which simply executes all of the initialization routines. Alternately,
    # you can specify explicitly which initialization routine you want:
    #
    #   Rails::Initializer.run(:set_load_path)
    #
    # This is useful if you only want the load path initialized, without
P
Pratik Naik 已提交
109
    # incurring the overhead of completely loading the entire environment.
110 111
    def self.run(command = :process, configuration = Configuration.new)
      yield configuration if block_given?
112 113 114
      initializer = new configuration
      initializer.send(command)
      initializer
115
    end
116

117 118
    # Create a new Initializer instance that references the given Configuration
    # instance.
119 120
    def initialize(configuration)
      @configuration = configuration
121
      @loaded_plugins = []
122
    end
123

124
    # Sequentially step through all of the available initialization routines,
125
    # in order (view execution order in source).
126
    def process
127 128
      Rails.configuration = configuration

129
      check_ruby_version
130
      install_gem_spec_stubs
131
      set_load_path
132 133
      add_gem_load_paths

134
      require_frameworks
135
      preload_frameworks
136
      set_autoload_paths
137
      add_plugin_load_paths
138
      load_environment
139

140
      initialize_encoding
141
      initialize_database
142 143 144 145

      initialize_cache
      initialize_framework_caches

146 147
      initialize_logger
      initialize_framework_logging
148

149 150
      initialize_dependency_mechanism
      initialize_whiny_nils
151

152
      initialize_time_zone
153 154
      initialize_i18n

155
      initialize_framework_settings
156
      initialize_framework_views
157

J
Joshua Peek 已提交
158 159
      initialize_metal

160
      add_support_load_paths
J
Jamis Buck 已提交
161

162
      load_gems
J
Jamis Buck 已提交
163
      load_plugins
164 165 166 167

      # pick up any gems that plugins depend on
      add_gem_load_paths
      load_gems
168
      check_gem_dependencies
J
Joshua Peek 已提交
169

170 171
      load_application_initializers

172 173
      # the framework is now fully initialized
      after_initialize
174

175 176 177
      # Prepare dispatcher callbacks and run 'prepare' callbacks
      prepare_dispatcher

178 179 180 181 182
      # Routing must be initialized after plugins to allow the former to extend the routes
      initialize_routing

      # Observers are loaded after plugins in case Observers or observed models are modified by plugins.
      load_observers
J
Joshua Peek 已提交
183

184 185 186
      # Load view path cache
      load_view_paths

187
      # Load application classes
188 189
      load_application_classes

190 191 192
      # Disable dependency loading during request cycle
      disable_dependency_loading

J
Joshua Peek 已提交
193 194
      # Flag initialized
      Rails.initialized = true
195
    end
196 197 198 199

    # Check for valid Ruby version
    # This is done in an external file, so we can use it
    # from the `rails` program as well without duplication.
200
    def check_ruby_version
201 202 203
      require 'ruby_version_check'
    end

204
    # If Rails is vendored and RubyGems is available, install stub GemSpecs
P
Pratik Naik 已提交
205 206
    # for Rails, Active Support, Active Record, Action Pack, Action Mailer, and
    # Active Resource. This allows Gem plugins to depend on Rails even when
207 208
    # the Gem version of Rails shouldn't be loaded.
    def install_gem_spec_stubs
209
      unless Rails.respond_to?(:vendor_rails?)
P
Pratik Naik 已提交
210
        abort %{Your config/boot.rb is outdated: Run "rake rails:update".}
211 212
      end

213 214 215 216 217 218 219 220 221 222
      if Rails.vendor_rails?
        begin; require "rubygems"; rescue LoadError; return; end

        stubs = %w(rails activesupport activerecord actionpack actionmailer activeresource)
        stubs.reject! { |s| Gem.loaded_specs.key?(s) }

        stubs.each do |stub|
          Gem.loaded_specs[stub] = Gem::Specification.new do |s|
            s.name = stub
            s.version = Rails::VERSION::STRING
223
            s.loaded_from = ""
224 225 226 227 228
          end
        end
      end
    end

229 230
    # Set the <tt>$LOAD_PATH</tt> based on the value of
    # Configuration#load_paths. Duplicates are removed.
231
    def set_load_path
232 233
      load_paths = configuration.load_paths + configuration.framework_paths
      load_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) }
234 235
      $LOAD_PATH.uniq!
    end
236

237 238
    # Set the paths from which Rails will automatically load source files, and
    # the load_once paths.
239
    def set_autoload_paths
240 241
      ActiveSupport::Dependencies.load_paths = configuration.load_paths.uniq
      ActiveSupport::Dependencies.load_once_paths = configuration.load_once_paths.uniq
242

243
      extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths
244 245
      unless extra.empty?
        abort <<-end_error
246
          load_once_paths must be a subset of the load_paths.
247 248 249
          Extra items in load_once_paths: #{extra * ','}
        end_error
      end
250

251 252
      # Freeze the arrays so future modifications will fail rather than do nothing mysteriously
      configuration.load_once_paths.freeze
253
    end
254

255
    # Requires all frameworks specified by the Configuration#frameworks
P
Pratik Naik 已提交
256 257
    # list. By default, all frameworks (Active Record, Active Support,
    # Action Pack, Action Mailer, and Active Resource) are loaded.
258 259
    def require_frameworks
      configuration.frameworks.each { |framework| require(framework.to_s) }
260
    rescue LoadError => e
261
      # Re-raise as RuntimeError because Mongrel would swallow LoadError.
262
      raise e.to_s
263
    end
264

265 266 267 268 269 270 271 272 273 274 275 276 277
    # Preload all frameworks specified by the Configuration#frameworks.
    # Used by Passenger to ensure everything's loaded before forking and
    # to avoid autoload race conditions in JRuby.
    def preload_frameworks
      if configuration.preload_frameworks
        configuration.frameworks.each do |framework|
          # String#classify and #constantize aren't available yet.
          toplevel = Object.const_get(framework.to_s.gsub(/(?:^|_)(.)/) { $1.upcase })
          toplevel.load_all!
        end
      end
    end

278 279
    # Add the load paths used by support functions such as the info controller
    def add_support_load_paths
280
    end
281

282
    # Adds all load paths from plugins to the global set of load paths, so that
283
    # code from plugins can be required (explicitly or automatically via ActiveSupport::Dependencies).
284 285 286 287
    def add_plugin_load_paths
      plugin_loader.add_plugin_load_paths
    end

288
    def add_gem_load_paths
289
      Rails::GemDependency.add_frozen_gem_path
290 291
      unless @configuration.gems.empty?
        require "rubygems"
292
        @configuration.gems.each { |gem| gem.add_load_paths }
293 294 295 296
      end
    end

    def load_gems
297
      @configuration.gems.each { |gem| gem.load }
298 299 300 301 302 303 304 305
    end

    def check_gem_dependencies
      unloaded_gems = @configuration.gems.reject { |g| g.loaded? }
      if unloaded_gems.size > 0
        @gems_dependencies_loaded = false
        # don't print if the gems rake tasks are being run
        unless $rails_gem_installer
306 307 308 309 310 311 312 313 314 315
          abort <<-end_error
Missing these required gems:
  #{unloaded_gems.map { |gem| "#{gem.name}  #{gem.requirement}" } * "\n  "}

You're running:
  ruby #{Gem.ruby_version} at #{Gem.ruby}
  rubygems #{Gem::RubyGemsVersion} at #{Gem.path * ', '}

Run `rake gems:install` to install the missing gems.
          end_error
316 317 318 319
        end
      else
        @gems_dependencies_loaded = true
      end
320 321
    end

322 323 324
    # Loads all plugins in <tt>config.plugin_paths</tt>.  <tt>plugin_paths</tt>
    # defaults to <tt>vendor/plugins</tt> but may also be set to a list of
    # paths, such as
D
David Heinemeier Hansson 已提交
325
    #   config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]
326
    #
327 328
    # In the default implementation, as each plugin discovered in <tt>plugin_paths</tt> is initialized:
    # * its +lib+ directory, if present, is added to the load path (immediately after the applications lib directory)
329
    # * <tt>init.rb</tt> is evaluated, if present
330 331
    #
    # After all plugins are loaded, duplicates are removed from the load path.
332 333
    # If an array of plugin names is specified in config.plugins, only those plugins will be loaded
    # and they plugins will be loaded in that order. Otherwise, plugins are loaded in alphabetical
334
    # order.
335 336 337
    #
    # if config.plugins ends contains :all then the named plugins will be loaded in the given order and all other
    # plugins will be loaded in alphabetical order
J
Jamis Buck 已提交
338
    def load_plugins
339 340 341 342 343
      plugin_loader.load_plugins
    end

    def plugin_loader
      @plugin_loader ||= configuration.plugin_loader.new(self)
J
Jamis Buck 已提交
344 345
    end

346
    # Loads the environment specified by Configuration#environment_path, which
347
    # is typically one of development, test, or production.
348 349
    def load_environment
      silence_warnings do
350 351
        return if @environment_loaded
        @environment_loaded = true
J
Joshua Peek 已提交
352

353
        config = configuration
354
        constants = self.class.constants
J
Joshua Peek 已提交
355

356
        eval(IO.read(configuration.environment_path), binding, configuration.environment_path)
J
Joshua Peek 已提交
357

358 359 360
        (self.class.constants - constants).each do |const|
          Object.const_set(const, self.class.const_get(const))
        end
361
      end
362
    end
363 364

    def load_observers
365
      if gems_dependencies_loaded && configuration.frameworks.include?(:active_record)
366
        ActiveRecord::Base.instantiate_observers
367
      end
368 369
    end

370
    def load_view_paths
371
      if configuration.frameworks.include?(:action_view)
372 373 374 375
        if configuration.cache_classes
          ActionController::Base.view_paths.load if configuration.frameworks.include?(:action_controller)
          ActionMailer::Base.template_root.load if configuration.frameworks.include?(:action_mailer)
        end
376
      end
377 378
    end

379
    # Eager load application classes
380
    def load_application_classes
381 382
      if configuration.cache_classes
        configuration.eager_load_paths.each do |load_path|
383
          matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
384
          Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
385
            require_dependency file.sub(matcher, '\1')
386 387
          end
        end
388 389 390
      end
    end

391 392 393 394 395 396 397
    # For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
    # multibyte safe operations. Plugin authors supporting other encodings
    # should override this behaviour and set the relevant +default_charset+
    # on ActionController::Base.
    #
    # For Ruby 1.9, this does nothing. Specify the default encoding in the Ruby
    # shebang line if you don't want UTF-8.
398
    def initialize_encoding
399
      $KCODE='u' if RUBY_VERSION < '1.9'
400 401
    end

402 403 404 405
    # This initialization routine does nothing unless <tt>:active_record</tt>
    # is one of the frameworks to load (Configuration#frameworks). If it is,
    # this sets the database configuration from Configuration#database_configuration
    # and then establishes the connection.
406
    def initialize_database
407 408 409 410
      if configuration.frameworks.include?(:active_record)
        ActiveRecord::Base.configurations = configuration.database_configuration
        ActiveRecord::Base.establish_connection
      end
411
    end
412

413 414 415 416 417 418 419 420 421 422 423 424
    def initialize_cache
      unless defined?(RAILS_CACHE)
        silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(configuration.cache_store) }
      end
    end

    def initialize_framework_caches
      if configuration.frameworks.include?(:action_controller)
        ActionController::Base.cache_store ||= RAILS_CACHE
      end
    end

P
Pratik Naik 已提交
425
    # If the RAILS_DEFAULT_LOGGER constant is already set, this initialization
426 427 428 429 430 431 432
    # routine does nothing. If the constant is not set, and Configuration#logger
    # is not +nil+, this also does nothing. Otherwise, a new logger instance
    # is created at Configuration#log_path, with a default log level of
    # Configuration#log_level.
    #
    # If the log could not be created, the log will be set to output to
    # +STDERR+, with a log level of +WARN+.
433
    def initialize_logger
434
      # if the environment has explicitly defined a logger, use it
435
      return if Rails.logger
436

437 438
      unless logger = configuration.logger
        begin
439 440
          logger = ActiveSupport::BufferedLogger.new(configuration.log_path)
          logger.level = ActiveSupport::BufferedLogger.const_get(configuration.log_level.to_s.upcase)
441 442 443
          if configuration.environment == "production"
            logger.auto_flushing = false
          end
444
        rescue StandardError => e
445 446
          logger = ActiveSupport::BufferedLogger.new(STDERR)
          logger.level = ActiveSupport::BufferedLogger::WARN
447 448 449 450 451
          logger.warn(
            "Rails Error: Unable to access log file. Please ensure that #{configuration.log_path} exists and is chmod 0666. " +
            "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
          )
        end
452
      end
453

454
      silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
455
    end
456

P
Pratik Naik 已提交
457
    # Sets the logger for Active Record, Action Controller, and Action Mailer
458 459
    # (but only for those frameworks that are to be loaded). If the framework's
    # logger is already set, it is not changed, otherwise it is set to use
P
Pratik Naik 已提交
460
    # RAILS_DEFAULT_LOGGER.
461
    def initialize_framework_logging
462
      for framework in ([ :active_record, :action_controller, :action_mailer ] & configuration.frameworks)
463
        framework.to_s.camelize.constantize.const_get("Base").logger ||= Rails.logger
464
      end
J
Joshua Peek 已提交
465

466 467
      ActiveSupport::Dependencies.logger ||= Rails.logger
      Rails.cache.logger ||= Rails.logger
468
    end
469

470
    # Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+
471
    # (but only for those frameworks that are to be loaded). If the framework's
472
    # paths have already been set, it is not changed, otherwise it is
473
    # set to use Configuration#view_path.
474
    def initialize_framework_views
475 476 477 478 479
      if configuration.frameworks.include?(:action_view)
        view_path = ActionView::PathSet::Path.new(configuration.view_path, false)
        ActionMailer::Base.template_root ||= view_path if configuration.frameworks.include?(:action_mailer)
        ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller) && ActionController::Base.view_paths.empty?
      end
480 481
    end

P
Pratik Naik 已提交
482
    # If Action Controller is not one of the loaded frameworks (Configuration#frameworks)
483 484
    # this does nothing. Otherwise, it loads the routing definitions and sets up
    # loading module used to lazily load controllers (Configuration#controller_paths).
485 486
    def initialize_routing
      return unless configuration.frameworks.include?(:action_controller)
487

488 489
      ActionController::Routing.controller_paths += configuration.controller_paths
      ActionController::Routing::Routes.add_configuration_file(configuration.routes_configuration_file)
490 491
      ActionController::Routing::Routes.reload
    end
492

493 494
    # Sets the dependency loading mechanism based on the value of
    # Configuration#cache_classes.
495
    def initialize_dependency_mechanism
496
      ActiveSupport::Dependencies.mechanism = configuration.cache_classes ? :require : :load
497
    end
498

499 500
    # Loads support for "whiny nil" (noisy warnings when methods are invoked
    # on +nil+ values) if Configuration#whiny_nils is true.
501 502 503
    def initialize_whiny_nils
      require('active_support/whiny_nil') if configuration.whiny_nils
    end
504

P
Pratik Naik 已提交
505
    # Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes.
506
    # If assigned value cannot be matched to a TimeZone, an exception will be raised.
507 508
    def initialize_time_zone
      if configuration.time_zone
509
        zone_default = Time.__send__(:get_zone, configuration.time_zone)
510

511
        unless zone_default
512 513 514
          raise \
            'Value assigned to config.time_zone not recognized.' +
            'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
515
        end
516

517
        Time.zone_default = zone_default
518

519 520 521 522 523 524 525
        if configuration.frameworks.include?(:active_record)
          ActiveRecord::Base.time_zone_aware_attributes = true
          ActiveRecord::Base.default_timezone = :utc
        end
      end
    end

J
Joshua Peek 已提交
526
    # Set the i18n configuration from config.i18n but special-case for the load_path which should be
527 528 529 530 531 532 533 534 535 536 537
    # appended to what's already set instead of overwritten.
    def initialize_i18n
      configuration.i18n.each do |setting, value|
        if setting == :load_path
          I18n.load_path += value
        else
          I18n.send("#{setting}=", value)
        end
      end
    end

J
Joshua Peek 已提交
538 539 540 541 542 543
    def initialize_metal
      Dir["#{configuration.root_path}/app/metal/*.rb"].each do |file|
        configuration.middleware.use(File.basename(file, '.rb').camelize)
      end
    end

544
    # Initializes framework-specific settings for each of the loaded frameworks
545 546
    # (Configuration#frameworks). The available settings map to the accessors
    # on each of the corresponding Base classes.
547
    def initialize_framework_settings
548 549
      configuration.frameworks.each do |framework|
        base_class = framework.to_s.camelize.constantize.const_get("Base")
550

551 552 553
        configuration.send(framework).each do |setting, value|
          base_class.send("#{setting}=", value)
        end
554
      end
555 556 557
      configuration.active_support.each do |setting, value|
        ActiveSupport.send("#{setting}=", value)
      end
558
    end
559 560

    # Fires the user-supplied after_initialize block (Configuration#after_initialize)
561
    def after_initialize
562
      if gems_dependencies_loaded
563 564 565
        configuration.after_initialize_blocks.each do |block|
          block.call
        end
566
      end
567
    end
568

569
    def load_application_initializers
570
      if gems_dependencies_loaded
571
        Dir["#{configuration.root_path}/config/initializers/**/*.rb"].sort.each do |initializer|
572
          load(initializer)
573
        end
574 575
      end
    end
576

577
    def prepare_dispatcher
578
      return unless configuration.frameworks.include?(:action_controller)
579 580
      require 'dispatcher' unless defined?(::Dispatcher)
      Dispatcher.define_dispatcher_callbacks(configuration.cache_classes)
581
      Dispatcher.new(Rails.logger).send :run_callbacks, :prepare_dispatch
582
    end
583 584 585 586 587 588

    def disable_dependency_loading
      if configuration.cache_classes && !configuration.dependency_loading
        ActiveSupport::Dependencies.unhook!
      end
    end
589
  end
590

591 592 593 594 595 596
  # The Configuration class holds all the parameters for the Initializer and
  # ships with defaults that suites most Rails applications. But it's possible
  # to overwrite everything. Usually, you'll create an Configuration file
  # implicitly through the block running on the Initializer, but it's also
  # possible to create the Configuration instance in advance and pass it in
  # like this:
597 598 599
  #
  #   config = Rails::Configuration.new
  #   Rails::Initializer.run(:process, config)
600
  class Configuration
601 602 603
    # The application's base directory.
    attr_reader :root_path

P
Pratik Naik 已提交
604
    # A stub for setting options on ActionController::Base.
605
    attr_accessor :action_controller
606

P
Pratik Naik 已提交
607
    # A stub for setting options on ActionMailer::Base.
608
    attr_accessor :action_mailer
609

P
Pratik Naik 已提交
610
    # A stub for setting options on ActionView::Base.
611
    attr_accessor :action_view
612

P
Pratik Naik 已提交
613
    # A stub for setting options on ActiveRecord::Base.
614
    attr_accessor :active_record
615

P
Pratik Naik 已提交
616
    # A stub for setting options on ActiveResource::Base.
617 618
    attr_accessor :active_resource

P
Pratik Naik 已提交
619
    # A stub for setting options on ActiveSupport.
620 621
    attr_accessor :active_support

622 623 624
    # Whether to preload all frameworks at startup.
    attr_accessor :preload_frameworks

625 626 627
    # Whether or not classes should be cached (set to false if you want
    # application classes to be reloaded on each request)
    attr_accessor :cache_classes
628

629
    # The list of paths that should be searched for controllers. (Defaults
J
Jeremy Kemper 已提交
630
    # to <tt>app/controllers</tt>.)
631 632 633 634 635
    attr_accessor :controller_paths

    # The path to the database configuration file to use. (Defaults to
    # <tt>config/database.yml</tt>.)
    attr_accessor :database_configuration_file
J
Joshua Peek 已提交
636

637 638 639
    # The path to the routes configuration file to use. (Defaults to
    # <tt>config/routes.rb</tt>.)
    attr_accessor :routes_configuration_file
640

641 642 643
    # The list of rails framework components that should be loaded. (Defaults
    # to <tt>:active_record</tt>, <tt>:action_controller</tt>,
    # <tt>:action_view</tt>, <tt>:action_mailer</tt>, and
D
David Heinemeier Hansson 已提交
644
    # <tt>:active_resource</tt>).
645
    attr_accessor :frameworks
646

647 648 649
    # An array of additional paths to prepend to the load path. By default,
    # all +app+, +lib+, +vendor+ and mock paths are included in this list.
    attr_accessor :load_paths
650

651
    # An array of paths from which Rails will automatically load from only once.
652
    # All elements of this array must also be in +load_paths+.
653
    attr_accessor :load_once_paths
654

655 656 657 658 659
    # An array of paths from which Rails will eager load on boot if cache
    # classes is enabled. All elements of this array must also be in
    # +load_paths+.
    attr_accessor :eager_load_paths

660 661 662 663
    # The log level to use for the default Rails logger. In production mode,
    # this defaults to <tt>:info</tt>. In development mode, it defaults to
    # <tt>:debug</tt>.
    attr_accessor :log_level
664

665 666 667
    # The path to the log file to use. Defaults to log/#{environment}.log
    # (e.g. log/development.log or log/production.log).
    attr_accessor :log_path
668

669 670 671 672 673
    # The specific logger to use. By default, a logger will be created and
    # initialized using #log_path and #log_level, but a programmer may
    # specifically set the logger to use via this accessor and it will be
    # used directly.
    attr_accessor :logger
674

675 676 677
    # The specific cache store to use. By default, the ActiveSupport::Cache::Store will be used.
    attr_accessor :cache_store

678 679
    # The root of the application's views. (Defaults to <tt>app/views</tt>.)
    attr_accessor :view_path
680

681 682 683
    # Set to +true+ if you want to be warned (noisily) when you try to invoke
    # any method of +nil+. Set to +false+ for the standard Ruby behavior.
    attr_accessor :whiny_nils
684

685 686 687
    # The list of plugins to load. If this is set to <tt>nil</tt>, all plugins will
    # be loaded. If this is set to <tt>[]</tt>, no plugins will be loaded. Otherwise,
    # plugins will be loaded in the order specified.
688 689 690 691
    attr_reader :plugins
    def plugins=(plugins)
      @plugins = plugins.nil? ? nil : plugins.map { |p| p.to_sym }
    end
692

693 694 695
    # The path to the root of the plugins directory. By default, it is in
    # <tt>vendor/plugins</tt>.
    attr_accessor :plugin_paths
696

697 698 699 700 701
    # The classes that handle finding the desired plugins that you'd like to load for
    # your application. By default it is the Rails::Plugin::FileSystemLocator which finds
    # plugins to load in <tt>vendor/plugins</tt>. You can hook into gem location by subclassing
    # Rails::Plugin::Locator and adding it onto the list of <tt>plugin_locators</tt>.
    attr_accessor :plugin_locators
702 703

    # The class that handles loading each plugin. Defaults to Rails::Plugin::Loader, but
704 705 706
    # a sub class would have access to fine grained modification of the loading behavior. See
    # the implementation of Rails::Plugin::Loader for more details.
    attr_accessor :plugin_loader
J
Joshua Peek 已提交
707

708
    # Enables or disables plugin reloading.  You can get around this setting per plugin.
P
Pratik Naik 已提交
709 710
    # If <tt>reload_plugins?</tt> is false, add this to your plugin's <tt>init.rb</tt>
    # to make it reloadable:
711
    #
712
    #   ActiveSupport::Dependencies.load_once_paths.delete lib_path
713
    #
P
Pratik Naik 已提交
714 715
    # If <tt>reload_plugins?</tt> is true, add this to your plugin's <tt>init.rb</tt>
    # to only load it once:
716
    #
717
    #   ActiveSupport::Dependencies.load_once_paths << lib_path
718 719 720 721 722 723 724
    #
    attr_accessor :reload_plugins

    # Returns true if plugin reloading is enabled.
    def reload_plugins?
      !!@reload_plugins
    end
725

726 727 728 729 730 731 732 733 734 735 736
    # Enables or disables dependency loading during the request cycle. Setting
    # <tt>dependency_loading</tt> to true will allow new classes to be loaded
    # during a request. Setting it to false will disable this behavior.
    #
    # Those who want to run in a threaded environment should disable this
    # option and eager load or require all there classes on initialization.
    #
    # If <tt>cache_classes</tt> is disabled, dependency loaded will always be
    # on.
    attr_accessor :dependency_loading

737 738 739 740 741
    # An array of gems that this rails application depends on.  Rails will automatically load
    # these gems during installation, and allow you to install any missing gems with:
    #
    #   rake gems:install
    #
R
Rick Olson 已提交
742
    # You can add gems with the #gem method.
743 744
    attr_accessor :gems

B
Ben Sandofsky 已提交
745 746
    # Adds a single Gem dependency to the rails application. By default, it will require
    # the library with the same name as the gem. Use :lib to specify a different name.
747 748 749 750 751 752
    #
    #   # gem 'aws-s3', '>= 0.4.0'
    #   # require 'aws/s3'
    #   config.gem 'aws-s3', :lib => 'aws/s3', :version => '>= 0.4.0', \
    #     :source => "http://code.whytheluckystiff.net"
    #
B
Ben Sandofsky 已提交
753 754 755
    # To require a library be installed, but not attempt to load it, pass :lib => false
    #
    #   config.gem 'qrp', :version => '0.4.1', :lib => false
756 757 758
    def gem(name, options = {})
      @gems << Rails::GemDependency.new(name, options)
    end
J
Joshua Peek 已提交
759

760 761 762 763 764 765 766 767 768
    # Deprecated options:
    def breakpoint_server(_ = nil)
      $stderr.puts %(
      *******************************************************************
      * config.breakpoint_server has been deprecated and has no effect. *
      *******************************************************************
      )
    end
    alias_method :breakpoint_server=, :breakpoint_server
769

P
Pratik Naik 已提交
770 771 772
    # Sets the default +time_zone+.  Setting this will enable +time_zone+
    # awareness for Active Record models and set the Active Record default
    # timezone to <tt>:utc</tt>.
773 774
    attr_accessor :time_zone

775 776 777
    # Accessor for i18n settings.
    attr_accessor :i18n

778 779
    # Create a new Configuration instance, initialized with the default
    # values.
780
    def initialize
781 782
      set_root_path!

783 784
      self.frameworks                   = default_frameworks
      self.load_paths                   = default_load_paths
785
      self.load_once_paths              = default_load_once_paths
786
      self.eager_load_paths             = default_eager_load_paths
787 788 789 790
      self.log_path                     = default_log_path
      self.log_level                    = default_log_level
      self.view_path                    = default_view_path
      self.controller_paths             = default_controller_paths
791
      self.preload_frameworks           = default_preload_frameworks
792
      self.cache_classes                = default_cache_classes
793
      self.dependency_loading           = default_dependency_loading
794
      self.whiny_nils                   = default_whiny_nils
795
      self.plugins                      = default_plugins
796
      self.plugin_paths                 = default_plugin_paths
797
      self.plugin_locators              = default_plugin_locators
798
      self.plugin_loader                = default_plugin_loader
799
      self.database_configuration_file  = default_database_configuration_file
800
      self.routes_configuration_file    = default_routes_configuration_file
801
      self.gems                         = default_gems
802
      self.i18n                         = default_i18n
803

804
      for framework in default_frameworks
805
        self.send("#{framework}=", Rails::OrderedOptions.new)
806
      end
807
      self.active_support = Rails::OrderedOptions.new
808
    end
809

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
    # Set the root_path to RAILS_ROOT and canonicalize it.
    def set_root_path!
      raise 'RAILS_ROOT is not set' unless defined?(::RAILS_ROOT)
      raise 'RAILS_ROOT is not a directory' unless File.directory?(::RAILS_ROOT)

      @root_path =
        # Pathname is incompatible with Windows, but Windows doesn't have
        # real symlinks so File.expand_path is safe.
        if RUBY_PLATFORM =~ /(:?mswin|mingw)/
          File.expand_path(::RAILS_ROOT)

        # Otherwise use Pathname#realpath which respects symlinks.
        else
          Pathname.new(::RAILS_ROOT).realpath.to_s
        end
J
Joshua Peek 已提交
825

826
      Object.const_set(:RELATIVE_RAILS_ROOT, ::RAILS_ROOT.dup) unless defined?(::RELATIVE_RAILS_ROOT)
827
      ::RAILS_ROOT.replace @root_path
828 829
    end

830 831 832 833
    # Enable threaded mode. Allows concurrent requests to controller actions and
    # multiple database connections. Also disables automatic dependency loading
    # after boot
    def threadsafe!
834
      self.preload_frameworks = true
835 836 837 838 839 840
      self.cache_classes = true
      self.dependency_loading = false
      self.action_controller.allow_concurrency = true
      self
    end

841 842 843
    # Loads and returns the contents of the #database_configuration_file. The
    # contents of the file are processed via ERB before being sent through
    # YAML::load.
844
    def database_configuration
845
      require 'erb'
846
      YAML::load(ERB.new(IO.read(database_configuration_file)).result)
847
    end
848

P
Pratik Naik 已提交
849
    # The path to the current environment's file (<tt>development.rb</tt>, etc.). By
850
    # default the file is at <tt>config/environments/#{environment}.rb</tt>.
851
    def environment_path
852
      "#{root_path}/config/environments/#{environment}.rb"
853
    end
J
Jamis Buck 已提交
854

855
    # Return the currently selected environment. By default, it returns the
P
Pratik Naik 已提交
856
    # value of the RAILS_ENV constant.
857 858 859
    def environment
      ::RAILS_ENV
    end
860

861
    # Adds a block which will be executed after rails has been fully initialized.
862
    # Useful for per-environment configuration which depends on the framework being
863 864
    # fully initialized.
    def after_initialize(&after_initialize_block)
865
      after_initialize_blocks << after_initialize_block if after_initialize_block
866
    end
867

868 869 870
    # Returns the blocks added with Configuration#after_initialize
    def after_initialize_blocks
      @after_initialize_blocks ||= []
871
    end
872

N
Nicholas Seckar 已提交
873 874
    # Add a preparation callback that will run before every request in development
    # mode, or before the first request in production.
875
    #
N
Nicholas Seckar 已提交
876 877
    # See Dispatcher#to_prepare.
    def to_prepare(&callback)
J
Joshua Peek 已提交
878
      after_initialize do
879 880 881
        require 'dispatcher' unless defined?(::Dispatcher)
        Dispatcher.to_prepare(&callback)
      end
N
Nicholas Seckar 已提交
882
    end
883

884 885 886 887 888
    def middleware
      require 'action_controller'
      ActionController::Dispatcher.middleware
    end

889 890 891 892
    def builtin_directories
      # Include builtins only in the development environment.
      (environment == 'development') ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
    end
893

894
    def framework_paths
895
      paths = %w(railties railties/lib activesupport/lib)
896
      paths << 'actionpack/lib' if frameworks.include?(:action_controller) || frameworks.include?(:action_view)
J
Joshua Peek 已提交
897

898
      [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
899
        paths << "#{framework.to_s.gsub('_', '')}/lib" if frameworks.include?(framework)
900
      end
J
Joshua Peek 已提交
901

902
      paths.map { |dir| "#{framework_root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
903
    end
904

905
    private
906 907 908 909
      def framework_root_path
        defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root_path}/vendor/rails"
      end

910
      def default_frameworks
911
        [ :active_record, :action_controller, :action_view, :action_mailer, :active_resource ]
912
      end
913

914
      def default_load_paths
915
        paths = []
J
Joshua Peek 已提交
916

917 918
        # 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}")
919

N
Nicholas Seckar 已提交
920 921
        # Add the app's controller directory
        paths.concat(Dir["#{root_path}/app/controllers/"])
922 923 924

        # Followed by the standard includes.
        paths.concat %w(
925
          app
J
Joshua Peek 已提交
926
          app/metal
927
          app/models
928 929 930
          app/controllers
          app/helpers
          app/services
931
          lib
932
          vendor
933
        ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
934

935
        paths.concat builtin_directories
936
      end
937

938
      # Doesn't matter since plugins aren't in load_paths yet.
939
      def default_load_once_paths
940
        []
941
      end
942

943 944
      def default_eager_load_paths
        %w(
J
Joshua Peek 已提交
945
          app/metal
946 947 948 949 950 951
          app/models
          app/controllers
          app/helpers
        ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
      end

952
      def default_log_path
953
        File.join(root_path, 'log', "#{environment}.log")
954
      end
955

956
      def default_log_level
957
        environment == 'production' ? :info : :debug
958
      end
959

960
      def default_database_configuration_file
961
        File.join(root_path, 'config', 'database.yml')
962
      end
963

964 965 966 967
      def default_routes_configuration_file
        File.join(root_path, 'config', 'routes.rb')
      end

968
      def default_view_path
969
        File.join(root_path, 'app', 'views')
970
      end
971

972
      def default_controller_paths
973
        paths = [File.join(root_path, 'app', 'controllers')]
974 975
        paths.concat builtin_directories
        paths
976
      end
977

978 979
      def default_dependency_loading
        true
980
      end
981

982 983 984 985
      def default_preload_frameworks
        false
      end

986
      def default_cache_classes
987
        true
988
      end
989

990 991 992
      def default_whiny_nils
        false
      end
993

994
      def default_plugins
995
        nil
996 997
      end

998 999 1000
      def default_plugin_paths
        ["#{root_path}/vendor/plugins"]
      end
1001

1002
      def default_plugin_locators
1003 1004 1005
        locators = []
        locators << Plugin::GemLocator if defined? Gem
        locators << Plugin::FileSystemLocator
1006
      end
1007

1008 1009 1010
      def default_plugin_loader
        Plugin::Loader
      end
J
Joshua Peek 已提交
1011

1012 1013 1014 1015 1016 1017 1018
      def default_cache_store
        if File.exist?("#{root_path}/tmp/cache/")
          [ :file_store, "#{root_path}/tmp/cache/" ]
        else
          :memory_store
        end
      end
J
Joshua Peek 已提交
1019

1020 1021 1022
      def default_gems
        []
      end
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034

      def default_i18n
        i18n = Rails::OrderedOptions.new
        i18n.load_path = []

        if File.exist?(File.join(RAILS_ROOT, 'config', 'locales'))
          i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
          i18n.load_path.flatten!
        end

        i18n
      end
1035
  end
1036
end
1037

1038
# Needs to be duplicated from Active Support since its needed before Active
1039
# Support is available. Here both Options and Hash are namespaced to prevent
P
Pratik Naik 已提交
1040
# conflicts with other implementations AND with the classes residing in Active Support.
1041 1042 1043 1044
class Rails::OrderedOptions < Array #:nodoc:
  def []=(key, value)
    key = key.to_sym

1045 1046 1047 1048 1049 1050 1051 1052
    if pair = find_pair(key)
      pair.pop
      pair << value
    else
      self << [key, value]
    end
  end

1053
  def [](key)
1054 1055
    pair = find_pair(key.to_sym)
    pair ? pair.last : nil
1056 1057 1058 1059 1060 1061 1062 1063 1064
  end

  def method_missing(name, *args)
    if name.to_s =~ /(.*)=$/
      self[$1.to_sym] = args.first
    else
      self[name]
    end
  end
1065 1066 1067 1068 1069 1070

  private
    def find_pair(key)
      self.each { |i| return i if i.first == key }
      return false
    end
1071
end