lib/hanami/router.rb in hanami-router-2.1.0.rc3 vs lib/hanami/router.rb in hanami-router-2.1.0

- old
+ new

@@ -18,10 +18,12 @@ require "hanami/router/params" require "hanami/router/trie" require "hanami/router/block" require "hanami/router/route" require "hanami/router/url_helpers" + require "hanami/router/globbed_path" + require "hanami/router/mounted_path" # URL helpers for other Hanami integrations # # @api private # @since 2.0.0 @@ -82,12 +84,11 @@ @not_allowed = not_allowed @not_found = not_found @block_context = block_context @fixed = {} @variable = {} - @globbed = {} - @mounted = {} + @globs_and_mounts = [] @blk = blk @inspector = inspector instance_eval(&blk) if blk end @@ -422,11 +423,11 @@ # end def mount(app, at:, **constraints) path = prefixed_path(at) prefix = Segment.fabricate(path, **constraints) - @mounted[prefix] = @resolver.call(path, app) + @globs_and_mounts << MountedPath.new(prefix, @resolver.call(path, app)) if inspect? @inspector.add_route(Route.new(http_method: "*", path: at, to: app, constraints: constraints)) end end @@ -617,42 +618,23 @@ # @api private def variable(env) @variable[env[::Rack::REQUEST_METHOD]]&.find(env[::Rack::PATH_INFO]) end - # @since 2.0.0 + # @since 2.1.0 # @api private - def globbed(env) - @globbed[env[::Rack::REQUEST_METHOD]]&.each do |path, to| - if (match = path.match(env[::Rack::PATH_INFO])) - return [to, match.named_captures] - end + def globbed_or_mounted(env) + @globs_and_mounts.each do |path| + result = path.endpoint_and_params(env) + return result unless result.empty? end nil end # @since 2.0.0 # @api private - def mounted(env) - @mounted.each do |prefix, app| - next unless (match = prefix.peek_match(env[::Rack::PATH_INFO])) - - # TODO: ensure compatibility with existing env[::Rack::SCRIPT_NAME] - # TODO: cleanup this code - env[::Rack::SCRIPT_NAME] = env[::Rack::SCRIPT_NAME].to_s + prefix.to_s - env[::Rack::PATH_INFO] = env[::Rack::PATH_INFO].sub(prefix.to_s, EMPTY_STRING) - env[::Rack::PATH_INFO] = DEFAULT_PREFIX if env[::Rack::PATH_INFO] == EMPTY_STRING - - return [app, match.named_captures] - end - - nil - end - - # @since 2.0.0 - # @api private def not_allowed(env) allowed_http_methods = _not_allowed_fixed(env) || _not_allowed_variable(env) return if allowed_http_methods.nil? @not_allowed.call(env, allowed_http_methods) @@ -798,11 +780,11 @@ # @api private def lookup(env) endpoint = fixed(env) return [endpoint, {}] if endpoint - variable(env) || mounted(env) || globbed(env) + variable(env) || globbed_or_mounted(env) end # @since 2.0.0 # @api private def add_route(http_method, path, to, as, constraints, &blk) @@ -841,11 +823,10 @@ end # @since 2.0.0 # @api private def add_globbed_route(http_method, path, to, constraints) - @globbed[http_method] ||= [] - @globbed[http_method] << [Segment.fabricate(path, **constraints), to] + @globs_and_mounts << GlobbedPath.new(http_method, Segment.fabricate(path, **constraints), to) end # @since 2.0.0 # @api private def add_variable_route(http_method, path, to, constraints)