|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Alchemy |
| 4 | + class UrlPatternMatcher |
| 5 | + attr_reader :urlname, :language_code, :page, :params |
| 6 | + |
| 7 | + def initialize(urlname, language_code: Current.language.code) |
| 8 | + @urlname = urlname |
| 9 | + @language_code = language_code |
| 10 | + @params = {} |
| 11 | + @page = find_page |
| 12 | + end |
| 13 | + |
| 14 | + private |
| 15 | + |
| 16 | + # Walks the page tree level by level, matching URL segments against page slugs or url_patterns. |
| 17 | + # |
| 18 | + # @return [Alchemy::Page, nil] the matched page or nil if no match |
| 19 | + def find_page |
| 20 | + return if urlname.blank? || pattern_layout_names.empty? |
| 21 | + |
| 22 | + segments = urlname.split("/") |
| 23 | + root = Page.language_roots.find_by(language_code: language_code) |
| 24 | + return unless root |
| 25 | + |
| 26 | + parent_id = root.id |
| 27 | + path_prefix = nil |
| 28 | + page = nil |
| 29 | + |
| 30 | + until segments.empty? |
| 31 | + children = Page.contentpages.where(language_code: language_code, parent_id: parent_id) |
| 32 | + |
| 33 | + # Try an exact slug match first |
| 34 | + expected_urlname = [path_prefix, segments.first].compact.join("/") |
| 35 | + page = children.find_by(urlname: expected_urlname) |
| 36 | + if page |
| 37 | + segments = segments.drop(1) |
| 38 | + parent_id = page.id |
| 39 | + path_prefix = expected_urlname |
| 40 | + next |
| 41 | + end |
| 42 | + |
| 43 | + # Try children with url_patterns attributes |
| 44 | + children.where(page_layout: pattern_layout_names).each do |child| |
| 45 | + page_definition = PageDefinition.get(child.page_layout) |
| 46 | + url_pattern = page_definition.url_pattern |
| 47 | + |
| 48 | + # a url pattern can have multiple segments, e.g. ":year/:slug" |
| 49 | + segment_count = url_pattern.count("/") + 1 |
| 50 | + candidate_url = segments.first(segment_count).join("/") |
| 51 | + extracted_parameters = match_url_pattern(candidate_url, url_pattern) |
| 52 | + |
| 53 | + # skip if the url does not match the pattern or constraints |
| 54 | + next unless extracted_parameters |
| 55 | + next unless satisfies_constraints?(extracted_parameters, page_definition.url_constraints) |
| 56 | + |
| 57 | + @params.merge!(extracted_parameters) |
| 58 | + |
| 59 | + # prepare the data for the next segment loop |
| 60 | + path_prefix = [path_prefix, child.slug].compact.join("/") |
| 61 | + segments = segments.drop(segment_count) |
| 62 | + parent_id = child.id |
| 63 | + page = child |
| 64 | + break |
| 65 | + end |
| 66 | + return unless page |
| 67 | + end |
| 68 | + |
| 69 | + page |
| 70 | + end |
| 71 | + |
| 72 | + # Matches a URL segment against a pattern like ":year/:slug" and returns |
| 73 | + # the extracted named captures as a hash, e.g. { year: "2024", slug: "my-post" }. |
| 74 | + # |
| 75 | + # @param url [String] the URL segment(s) to match, e.g. "2024/my-post" |
| 76 | + # @param pattern [String] the url_pattern from the page definition, e.g. ":year/:slug" |
| 77 | + # @return [Hash<Symbol, String>, nil] extracted params or nil if no match |
| 78 | + def match_url_pattern(url, pattern) |
| 79 | + regex_str = Regexp.escape(pattern).gsub(/:([a-zA-Z_][a-zA-Z0-9_]*)/) do |
| 80 | + "(?<#{$1}>[^/]+)" |
| 81 | + end |
| 82 | + |
| 83 | + regex = Regexp.new("\\A#{regex_str}\\z") |
| 84 | + match_data = regex.match(url) |
| 85 | + return nil unless match_data |
| 86 | + |
| 87 | + match_data.named_captures.transform_keys(&:to_sym) |
| 88 | + end |
| 89 | + |
| 90 | + # Validates extracted params against url_constraints from the page definition. |
| 91 | + # Constraints can be a simple string or a hash mapping param names to types. |
| 92 | + # |
| 93 | + # @param params [Hash<Symbol, String>] extracted params |
| 94 | + # @param constraints [String, Hash, nil] constraint definitions |
| 95 | + # @return [Boolean] |
| 96 | + def satisfies_constraints?(params, constraints) |
| 97 | + return true if constraints.blank? |
| 98 | + |
| 99 | + format_matchers = Alchemy.config.format_matchers |
| 100 | + normalize_constraints(constraints, params).all? do |key, type| |
| 101 | + if type.is_a?(Regexp) |
| 102 | + params[key]&.match?(type) |
| 103 | + else |
| 104 | + next true unless format_matchers.respond_to?(type.to_sym) |
| 105 | + |
| 106 | + params[key]&.match?(format_matchers.public_send(type.to_sym)) |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + # Normalizes constraints into a hash mapping param names to type strings. |
| 112 | + # A simple string constraint applies to all params, e.g. url_constraints: "integer" |
| 113 | + # |
| 114 | + # @param constraints [String, Hash] constraint definitions |
| 115 | + # @param params [Hash<Symbol, String>] extracted params |
| 116 | + # @return [Hash<Symbol, String>] |
| 117 | + def normalize_constraints(constraints, params) |
| 118 | + if constraints.is_a?(String) |
| 119 | + params.keys.to_h { |key| [key, constraints] } |
| 120 | + else |
| 121 | + constraints.transform_keys(&:to_sym) |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + # Returns the names of all page layouts that have a url_pattern defined. |
| 126 | + # |
| 127 | + # @return [Array<String>] layout names with url_patterns |
| 128 | + def pattern_layout_names |
| 129 | + @_pattern_layout_names ||= PageDefinition.all.select { |d| d.url_pattern.present? }.map(&:name) |
| 130 | + end |
| 131 | + end |
| 132 | +end |
0 commit comments