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
|
2009-09-05 09:01:46 +02:00
|
|
|
@@cache_classes = true
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
class << self
|
2008-05-18 06:22:34 +02:00
|
|
|
def define_dispatcher_callbacks(cache_classes)
|
2009-09-05 09:01:46 +02:00
|
|
|
@@cache_classes = cache_classes
|
2008-05-18 06:22:34 +02:00
|
|
|
unless cache_classes
|
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)
|
|
|
|
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
|
2009-02-28 02:23:00 +01:00
|
|
|
|
|
|
|
def run_prepare_callbacks
|
|
|
|
if defined?(Rails) && Rails.logger
|
|
|
|
logger = Rails.logger
|
|
|
|
else
|
|
|
|
logger = Logger.new($stderr)
|
|
|
|
end
|
|
|
|
|
|
|
|
new(logger).send :run_callbacks, :prepare_dispatch
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload_application
|
|
|
|
# Run prepare callbacks before every request in development mode
|
|
|
|
run_prepare_callbacks
|
|
|
|
|
|
|
|
Routing::Routes.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup_application
|
|
|
|
# Cleanup the application before processing the current request.
|
|
|
|
ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord)
|
|
|
|
ActiveSupport::Dependencies.clear
|
|
|
|
ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord)
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
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
|
2009-09-05 09:01:46 +02:00
|
|
|
build_middleware_stack if @@cache_classes
|
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-09-05 09:01:46 +02:00
|
|
|
if @@cache_classes
|
|
|
|
@app.call(env)
|
|
|
|
else
|
|
|
|
Reloader.run do
|
|
|
|
# When class reloading is turned on, we will want to rebuild the
|
|
|
|
# middleware stack every time we process a request. If we don't
|
|
|
|
# rebuild the middleware stack, then the stack may contain references
|
|
|
|
# to old classes metal classes, which will b0rk class reloading.
|
|
|
|
build_middleware_stack
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
2009-02-04 21:26:08 +01:00
|
|
|
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 flush_logger
|
2008-10-27 07:47:01 +01:00
|
|
|
Base.logger.flush
|
|
|
|
end
|
2009-09-05 09:01:46 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
def build_middleware_stack
|
|
|
|
@app = @@middleware.build(lambda { |env| self.dup._call(env) })
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|