lib/hanami/application.rb in hanami-2.0.0.alpha4 vs lib/hanami/application.rb in hanami-2.0.0.alpha5

- old
+ new

@@ -6,13 +6,10 @@ require "pathname" require "rack" require "zeitwerk" require_relative "slice" require_relative "application/autoloader/inflector_adapter" -require_relative "application/router" -require_relative "application/routes" -require_relative "application/settings" module Hanami # Hanami application class # # @since 2.0.0 @@ -22,14 +19,13 @@ class << self def inherited(klass) @_mutex.synchronize do klass.class_eval do @_mutex = Mutex.new - @_configuration = Hanami::Configuration.new(env: Hanami.env) + @_configuration = Hanami::Configuration.new(application_name: name, env: Hanami.env) extend ClassMethods - include InstanceMethods end klass.send :prepare_base_load_path Hanami.application = klass @@ -74,14 +70,35 @@ @inited = true self end + def boot(&block) + return self if booted? + + init + + container.finalize!(&block) + + slices.values.each(&:boot) + + @booted = true + self + end + + def shutdown + container.shutdown! + end + def inited? @inited end + def booted? + @booted + end + def autoloader raise "Application not init'ed" unless defined?(@autoloader) @autoloader end @@ -96,10 +113,22 @@ raise "Application not init'ed" unless defined?(@deps_module) @deps_module end + def router + raise "Application not init'ed" unless inited? + + @_mutex.synchronize do + @_router ||= load_router + end + end + + def rack_app + @rack_app ||= router.to_rack_app + end + def slices @slices ||= {} end def register_slice(name, **slice_args) @@ -140,42 +169,16 @@ def resolve(*args) container.resolve(*args) end - def boot(&block) - return self if booted? - - init - - load_router - - container.finalize!(&block) - - slices.values.each(&:boot) - - @booted = true - self - end - - def booted? - @booted - end - - def shutdown - container.shutdown! - end - def settings @_settings ||= load_settings end - MODULE_DELIMITER = "::" - private_constant :MODULE_DELIMITER - def namespace - inflector.constantize(name.split(MODULE_DELIMITER)[0..-2].join(MODULE_DELIMITER)) + configuration.namespace end def namespace_name namespace.name end @@ -183,11 +186,11 @@ def namespace_path inflector.underscore(namespace) end def application_name - inflector.underscore(namespace).to_sym + configuration.application_name end def root configuration.root end @@ -209,45 +212,10 @@ return unless component_name providers.detect { |provider| component_name.include?(provider.namespace.to_s) } end - def router - @_mutex.synchronize do - @_router ||= load_router - end - end - - def load_router - Router.new( - routes: routes, - resolver: resolver, - **configuration.router.options, - ) do - use Hanami.application[:rack_monitor] - - Hanami.application.config.for_each_middleware do |m, *args, &block| - use(m, *args, &block) - end - end - end - - def routes - require File.join(configuration.root, configuration.router.routes_path) - routes_class = autodiscover_application_constant(configuration.router.routes_class_name) - routes_class.routes - rescue LoadError - proc {} - end - - def resolver - config.router.resolver.new( - slices: slices, - inflector: inflector - ) - end - private def prepare_base_load_path base_path = File.join(root, "lib") $LOAD_PATH.unshift base_path unless $LOAD_PATH.include?(base_path) @@ -341,37 +309,58 @@ ) end # rubocop:enable Metrics/AbcSize, Metrics/MethodLength def load_settings + require_relative "application/settings" + prepare_base_load_path require File.join(configuration.root, configuration.settings_path) settings_class = autodiscover_application_constant(configuration.settings_class_name) settings_class.new(configuration.settings_store) rescue LoadError Settings.new end + MODULE_DELIMITER = "::" + private_constant :MODULE_DELIMITER + def autodiscover_application_constant(constants) inflector.constantize([namespace_name, *constants].join(MODULE_DELIMITER)) end - end - # rubocop:enable Metrics/ModuleLength - # Application instance interface - module InstanceMethods - # rubocop:disable Metrics/AbcSize, Metrics/MethodLength - def initialize(application = self.class) + def load_router require_relative "application/router" - application.boot + Router.new( + routes: load_routes, + resolver: router_resolver, + **configuration.router.options, + ) do + use Hanami.application[:rack_monitor] - @app = application.router.to_rack_app + Hanami.application.config.for_each_middleware do |m, *args, &block| + use(m, *args, &block) + end + end end - # rubocop:enable Metrics/AbcSize, Metrics/MethodLength - def call(env) - @app.call(env) + def load_routes + require_relative "application/routes" + + require File.join(configuration.root, configuration.router.routes_path) + routes_class = autodiscover_application_constant(configuration.router.routes_class_name) + routes_class.routes + rescue LoadError + proc {} end + + def router_resolver + config.router.resolver.new( + slices: slices, + inflector: inflector + ) + end end + # rubocop:enable Metrics/ModuleLength end end