/
resolver.rb
213 lines (176 loc) · 5.99 KB
/
resolver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# frozen_string_literal: true
require "pathname"
require "active_support/core_ext/class"
require "active_support/core_ext/module/attribute_accessors"
require "action_view/template"
require "thread"
require "concurrent/map"
module ActionView
# = Action View Resolver
class Resolver
class PathParser # :nodoc:
ParsedPath = Struct.new(:path, :details)
def build_path_regex
handlers = Regexp.union(Template::Handlers.extensions.map(&:to_s))
formats = Regexp.union(Template::Types.symbols.map(&:to_s))
available_locales = I18n.available_locales.map(&:to_s)
regular_locales = [/[a-z]{2}(?:[-_][A-Z]{2})?/]
locales = Regexp.union(available_locales + regular_locales)
variants = "[^.]*"
%r{
\A
(?:(?<prefix>.*)/)?
(?<partial>_)?
(?<action>.*?)
(?:\.(?<locale>#{locales}))??
(?:\.(?<format>#{formats}))??
(?:\+(?<variant>#{variants}))??
(?:\.(?<handler>#{handlers}))?
\z
}x
end
def parse(path)
@regex ||= build_path_regex
match = @regex.match(path)
path = TemplatePath.build(match[:action], match[:prefix] || "", !!match[:partial])
details = TemplateDetails.new(
match[:locale]&.to_sym,
match[:handler]&.to_sym,
match[:format]&.to_sym,
match[:variant]&.to_sym
)
ParsedPath.new(path, details)
end
end
cattr_accessor :caching, default: true
class << self
alias :caching? :caching
end
def clear_cache
end
# Normalizes the arguments and passes it on to find_templates.
def find_all(name, prefix = nil, partial = false, details = {}, key = nil, locals = [])
_find_all(name, prefix, partial, details, key, locals)
end
def built_templates # :nodoc:
# Used for error pages
[]
end
def all_template_paths # :nodoc:
# Not implemented by default
[]
end
private
def _find_all(name, prefix, partial, details, key, locals)
find_templates(name, prefix, partial, details, locals)
end
delegate :caching?, to: :class
# This is what child classes implement. No defaults are needed
# because Resolver guarantees that the arguments are present and
# normalized.
def find_templates(name, prefix, partial, details, locals = [])
raise NotImplementedError, "Subclasses must implement a find_templates(name, prefix, partial, details, locals = []) method"
end
end
# A resolver that loads files from the filesystem.
class FileSystemResolver < Resolver
attr_reader :path
def initialize(path)
raise ArgumentError, "path already is a Resolver class" if path.is_a?(Resolver)
@unbound_templates = Concurrent::Map.new
@path_parser = PathParser.new
@path = File.expand_path(path)
super()
end
def clear_cache
@unbound_templates.clear
@path_parser = PathParser.new
super
end
def to_s
@path.to_s
end
alias :to_path :to_s
def eql?(resolver)
self.class.equal?(resolver.class) && to_path == resolver.to_path
end
alias :== :eql?
def all_template_paths # :nodoc:
paths = template_glob("**/*")
paths.map do |filename|
filename.from(@path.size + 1).remove(/\.[^\/]*\z/)
end.uniq.map do |filename|
TemplatePath.parse(filename)
end
end
def built_templates # :nodoc:
@unbound_templates.values.flatten.flat_map(&:built_templates)
end
private
def _find_all(name, prefix, partial, details, key, locals)
requested_details = key || TemplateDetails::Requested.new(**details)
cache = key ? @unbound_templates : Concurrent::Map.new
unbound_templates =
cache.compute_if_absent(TemplatePath.virtual(name, prefix, partial)) do
path = TemplatePath.build(name, prefix, partial)
unbound_templates_from_path(path)
end
filter_and_sort_by_details(unbound_templates, requested_details).map do |unbound_template|
unbound_template.bind_locals(locals)
end
end
def source_for_template(template)
Template::Sources::File.new(template)
end
def build_unbound_template(template)
parsed = @path_parser.parse(template.from(@path.size + 1))
details = parsed.details
source = source_for_template(template)
UnboundTemplate.new(
source,
template,
details: details,
virtual_path: parsed.path.virtual,
)
end
def unbound_templates_from_path(path)
if path.name.include?(".")
return []
end
# Instead of checking for every possible path, as our other globs would
# do, scan the directory for files with the right prefix.
paths = template_glob("#{escape_entry(path.to_s)}*")
paths.map do |path|
build_unbound_template(path)
end.select do |template|
# Select for exact virtual path match, including case sensitivity
template.virtual_path == path.virtual
end
end
def filter_and_sort_by_details(templates, requested_details)
filtered_templates = templates.select do |template|
template.details.matches?(requested_details)
end
if filtered_templates.count > 1
filtered_templates.sort_by! do |template|
template.details.sort_key_for(requested_details)
end
end
filtered_templates
end
# Safe glob within @path
def template_glob(glob)
query = File.join(escape_entry(@path), glob)
path_with_slash = File.join(@path, "")
Dir.glob(query).filter_map do |filename|
filename = File.expand_path(filename)
next if File.directory?(filename)
next unless filename.start_with?(path_with_slash)
filename
end
end
def escape_entry(entry)
entry.gsub(/[*?{}\[\]]/, '\\\\\\&')
end
end
end