2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-21 09:21:02 -04:00
|
|
|
require "action_view/dependency_tracker"
|
2012-10-17 18:20:51 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
module ActionView
|
|
|
|
class Digestor
|
2016-05-16 15:38:01 -04:00
|
|
|
@@digest_mutex = Mutex.new
|
2013-07-30 09:17:27 -04:00
|
|
|
|
|
|
|
class << self
|
2014-03-01 12:59:56 -05:00
|
|
|
# Supported options:
|
|
|
|
#
|
2019-02-20 04:23:57 -05:00
|
|
|
# * <tt>name</tt> - Template name
|
|
|
|
# * <tt>format</tt> - Template format
|
|
|
|
# * <tt>finder</tt> - An instance of <tt>ActionView::LookupContext</tt>
|
|
|
|
# * <tt>dependencies</tt> - An array of dependent views
|
2019-07-26 05:52:40 -04:00
|
|
|
def digest(name:, format: nil, finder:, dependencies: nil)
|
2018-09-07 14:45:01 -04:00
|
|
|
if dependencies.nil? || dependencies.empty?
|
2019-02-15 20:10:28 -05:00
|
|
|
cache_key = "#{name}.#{format}"
|
2018-09-07 14:45:01 -04:00
|
|
|
else
|
2020-09-19 14:03:05 -04:00
|
|
|
dependencies_suffix = dependencies.flatten.tap(&:compact!).join(".")
|
|
|
|
cache_key = "#{name}.#{format}.#{dependencies_suffix}"
|
2018-09-07 14:45:01 -04:00
|
|
|
end
|
2013-07-15 09:59:18 -04:00
|
|
|
|
2013-07-30 09:17:27 -04:00
|
|
|
# this is a correctly done double-checked locking idiom
|
2015-09-19 09:56:26 -04:00
|
|
|
# (Concurrent::Map's lookups have volatile semantics)
|
2016-02-18 13:47:47 -05:00
|
|
|
finder.digest_cache[cache_key] || @@digest_mutex.synchronize do
|
2016-02-18 11:23:26 -05:00
|
|
|
finder.digest_cache.fetch(cache_key) do # re-check under lock
|
2016-02-18 18:12:18 -05:00
|
|
|
partial = name.include?("/_")
|
|
|
|
root = tree(name, finder, partial)
|
2016-02-18 18:06:41 -05:00
|
|
|
dependencies.each do |injected_dep|
|
|
|
|
root.children << Injected.new(injected_dep, nil, nil)
|
2018-09-07 14:45:01 -04:00
|
|
|
end if dependencies
|
2016-02-18 18:06:41 -05:00
|
|
|
finder.digest_cache[cache_key] = root.digest(finder)
|
2013-07-30 09:17:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-07 12:44:16 -04:00
|
|
|
|
2016-02-16 18:10:25 -05:00
|
|
|
def logger
|
|
|
|
ActionView::Base.logger || NullLogger
|
|
|
|
end
|
|
|
|
|
2016-02-16 18:21:16 -05:00
|
|
|
# Create a dependency tree for template named +name+.
|
2016-02-18 18:06:41 -05:00
|
|
|
def tree(name, finder, partial = false, seen = {})
|
2016-02-16 18:21:16 -05:00
|
|
|
logical_name = name.gsub(%r|/_|, "/")
|
2020-05-16 00:31:42 -04:00
|
|
|
interpolated = name.include?("#")
|
2016-06-14 17:23:44 -04:00
|
|
|
|
2020-05-16 00:31:42 -04:00
|
|
|
if !interpolated && (template = find_template(finder, logical_name, [], partial, []))
|
2016-02-16 18:26:16 -05:00
|
|
|
if node = seen[template.identifier] # handle cycles in the tree
|
|
|
|
node
|
2016-02-16 18:21:16 -05:00
|
|
|
else
|
|
|
|
node = seen[template.identifier] = Node.create(name, logical_name, template, partial)
|
|
|
|
|
|
|
|
deps = DependencyTracker.find_dependencies(name, template, finder.view_paths)
|
|
|
|
deps.uniq { |n| n.gsub(%r|/_|, "/") }.each do |dep_file|
|
2016-02-18 18:06:41 -05:00
|
|
|
node.children << tree(dep_file, finder, true, seen)
|
2016-02-16 18:21:16 -05:00
|
|
|
end
|
|
|
|
node
|
2016-02-15 17:25:10 -05:00
|
|
|
end
|
2016-02-16 18:21:16 -05:00
|
|
|
else
|
2020-05-16 00:31:42 -04:00
|
|
|
unless interpolated # Dynamic template partial names can never be tracked
|
2017-03-22 13:09:16 -04:00
|
|
|
logger.error " Couldn't find template for digesting: #{name}"
|
|
|
|
end
|
|
|
|
|
2016-02-16 18:21:16 -05:00
|
|
|
seen[name] ||= Missing.new(name, logical_name, nil)
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
end
|
2018-04-13 14:43:12 -04:00
|
|
|
|
|
|
|
private
|
2018-05-03 03:03:26 -04:00
|
|
|
def find_template(finder, name, prefixes, partial, keys)
|
2018-04-13 14:43:12 -04:00
|
|
|
finder.disable_cache do
|
2019-02-15 20:10:28 -05:00
|
|
|
finder.find_all(name, prefixes, partial, keys).first
|
2018-04-13 14:43:12 -04:00
|
|
|
end
|
|
|
|
end
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 17:25:34 -05:00
|
|
|
class Node
|
|
|
|
attr_reader :name, :logical_name, :template, :children
|
2016-02-15 17:32:41 -05:00
|
|
|
|
|
|
|
def self.create(name, logical_name, template, partial)
|
2016-02-16 18:21:16 -05:00
|
|
|
klass = partial ? Partial : Node
|
|
|
|
klass.new(name, logical_name, template, [])
|
2016-02-15 17:32:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(name, logical_name, template, children = [])
|
2016-02-16 18:21:16 -05:00
|
|
|
@name = name
|
2016-02-16 17:25:34 -05:00
|
|
|
@logical_name = logical_name
|
2016-02-16 18:21:16 -05:00
|
|
|
@template = template
|
|
|
|
@children = children
|
2016-02-15 17:32:41 -05:00
|
|
|
end
|
|
|
|
|
2016-02-18 14:09:15 -05:00
|
|
|
def digest(finder, stack = [])
|
2017-11-29 19:27:27 -05:00
|
|
|
ActiveSupport::Digest.hexdigest("#{template.source}-#{dependency_digest(finder, stack)}")
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
|
2016-02-18 14:09:15 -05:00
|
|
|
def dependency_digest(finder, stack)
|
2016-02-15 17:25:10 -05:00
|
|
|
children.map do |node|
|
|
|
|
if stack.include?(node)
|
|
|
|
false
|
|
|
|
else
|
2016-02-18 14:09:15 -05:00
|
|
|
finder.digest_cache[node.name] ||= begin
|
|
|
|
stack.push node
|
|
|
|
node.digest(finder, stack).tap { stack.pop }
|
|
|
|
end
|
2016-02-15 17:25:10 -05:00
|
|
|
end
|
|
|
|
end.join("-")
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
2016-02-16 18:10:25 -05:00
|
|
|
|
|
|
|
def to_dep_map
|
|
|
|
children.any? ? { name => children.map(&:to_dep_map) } : name
|
|
|
|
end
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 18:21:16 -05:00
|
|
|
class Partial < Node; end
|
2016-02-15 17:25:10 -05:00
|
|
|
|
2016-02-12 20:04:56 -05:00
|
|
|
class Missing < Node
|
2016-08-06 12:48:35 -04:00
|
|
|
def digest(finder, _ = []) "" end
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
|
2016-02-15 17:25:10 -05:00
|
|
|
class Injected < Node
|
2016-02-18 14:09:15 -05:00
|
|
|
def digest(finder, _ = []) name end
|
2016-02-15 17:25:10 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 18:21:16 -05:00
|
|
|
class NullLogger
|
|
|
|
def self.debug(_); end
|
|
|
|
def self.error(_); end
|
2012-10-17 18:20:51 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-05 10:05:50 -04:00
|
|
|
end
|