2007-12-21 08:48:59 +01:00
|
|
|
module ActionController
|
|
|
|
# Dispatches requests to the appropriate controller and takes care of
|
|
|
|
# reloading the app after each request when Dependencies.load? is true.
|
|
|
|
class Dispatcher
|
|
|
|
class << self
|
2008-05-18 06:22:34 +02:00
|
|
|
def define_dispatcher_callbacks(cache_classes)
|
|
|
|
unless cache_classes
|
|
|
|
# Development mode callbacks
|
|
|
|
before_dispatch :reload_application
|
|
|
|
after_dispatch :cleanup_application
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if defined?(ActiveRecord)
|
2008-10-27 07:47:01 +01:00
|
|
|
after_dispatch :checkin_connections
|
2008-05-18 06:22:34 +02:00
|
|
|
to_prepare(:activerecord_instantiate_observers) { ActiveRecord::Base.instantiate_observers }
|
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
after_dispatch :flush_logger if Base.logger && Base.logger.respond_to?(:flush)
|
2008-11-24 22:53:39 +01:00
|
|
|
|
|
|
|
to_prepare do
|
|
|
|
I18n.reload!
|
|
|
|
end
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# DEPRECATE: Remove CGI support
|
2007-12-21 08:48:59 +01:00
|
|
|
def dispatch(cgi = nil, session_options = CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout)
|
|
|
|
new(output).dispatch_cgi(cgi, session_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add a preparation callback. Preparation callbacks are run before every
|
|
|
|
# request in development mode, and before the first request in production
|
|
|
|
# mode.
|
2008-10-27 07:47:01 +01:00
|
|
|
#
|
2007-12-21 08:48:59 +01:00
|
|
|
# An optional identifier may be supplied for the callback. If provided,
|
|
|
|
# to_prepare may be called again with the same identifier to replace the
|
|
|
|
# existing callback. Passing an identifier is a suggested practice if the
|
|
|
|
# code adding a preparation block may be reloaded.
|
|
|
|
def to_prepare(identifier = nil, &block)
|
2008-05-18 06:22:34 +02:00
|
|
|
@prepare_dispatch_callbacks ||= ActiveSupport::Callbacks::CallbackChain.new
|
|
|
|
callback = ActiveSupport::Callbacks::Callback.new(:prepare_dispatch, block, :identifier => identifier)
|
2008-10-27 07:47:01 +01:00
|
|
|
@prepare_dispatch_callbacks.replace_or_append!(callback)
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
cattr_accessor :middleware
|
|
|
|
self.middleware = MiddlewareStack.new do |middleware|
|
|
|
|
middlewares = File.join(File.dirname(__FILE__), "middlewares.rb")
|
|
|
|
middleware.instance_eval(File.read(middlewares))
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
define_callbacks :prepare_dispatch, :before_dispatch, :after_dispatch
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# DEPRECATE: Remove arguments, since they are only used by CGI
|
2008-10-27 07:47:01 +01:00
|
|
|
def initialize(output = $stdout, request = nil, response = nil)
|
2009-02-04 21:26:08 +01:00
|
|
|
@output = output
|
|
|
|
@app = @@middleware.build(lambda { |env| self.dup._call(env) })
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def dispatch
|
2008-10-27 07:47:01 +01:00
|
|
|
begin
|
|
|
|
run_callbacks :before_dispatch
|
2009-02-04 21:26:08 +01:00
|
|
|
Routing::Routes.call(@env)
|
2008-10-27 07:47:01 +01:00
|
|
|
rescue Exception => exception
|
2009-02-04 21:26:08 +01:00
|
|
|
if controller ||= (::ApplicationController rescue Base)
|
|
|
|
controller.call_with_exception(@env, exception).to_a
|
|
|
|
else
|
|
|
|
raise exception
|
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
ensure
|
|
|
|
run_callbacks :after_dispatch, :enumerator => :reverse_each
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# DEPRECATE: Remove CGI support
|
2007-12-21 08:48:59 +01:00
|
|
|
def dispatch_cgi(cgi, session_options)
|
2009-02-04 21:26:08 +01:00
|
|
|
CGIHandler.dispatch_cgi(self, cgi, @output)
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def call(env)
|
2009-02-04 21:26:08 +01:00
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def _call(env)
|
|
|
|
@env = env
|
2008-10-27 07:47:01 +01:00
|
|
|
dispatch
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def reload_application
|
2008-05-18 06:22:34 +02:00
|
|
|
# Run prepare callbacks before every request in development mode
|
|
|
|
run_callbacks :prepare_dispatch
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
Routing::Routes.reload
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Cleanup the application by clearing out loaded classes so they can
|
|
|
|
# be reloaded on the next request without restarting the server.
|
2008-05-18 06:22:34 +02:00
|
|
|
def cleanup_application
|
|
|
|
ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord)
|
2008-09-07 07:54:05 +02:00
|
|
|
ActiveSupport::Dependencies.clear
|
2008-05-18 06:22:34 +02:00
|
|
|
ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord)
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def flush_logger
|
2008-10-27 07:47:01 +01:00
|
|
|
Base.logger.flush
|
|
|
|
end
|
|
|
|
|
|
|
|
def checkin_connections
|
|
|
|
# Don't return connection (and peform implicit rollback) if this request is a part of integration test
|
2009-02-04 21:26:08 +01:00
|
|
|
return if @env.key?("rack.test")
|
2008-10-27 07:47:01 +01:00
|
|
|
ActiveRecord::Base.clear_active_connections!
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|