Froze rails gems
[depot.git] / vendor / rails / railties / lib / rails / plugin / loader.rb
1 require "rails/plugin"
2
3 module Rails
4 class Plugin
5 class Loader
6 attr_reader :initializer
7
8 # Creates a new Plugin::Loader instance, associated with the given
9 # Rails::Initializer. This default implementation automatically locates
10 # all plugins, and adds all plugin load paths, when it is created. The plugins
11 # are then fully loaded (init.rb is evaluated) when load_plugins is called.
12 #
13 # It is the loader's responsibility to ensure that only the plugins specified
14 # in the configuration are actually loaded, and that the order defined
15 # is respected.
16 def initialize(initializer)
17 @initializer = initializer
18 end
19
20 # Returns the plugins to be loaded, in the order they should be loaded.
21 def plugins
22 @plugins ||= all_plugins.select { |plugin| should_load?(plugin) }.sort { |p1, p2| order_plugins(p1, p2) }
23 end
24
25 # Returns all the plugins that could be found by the current locators.
26 def all_plugins
27 @all_plugins ||= locate_plugins
28 @all_plugins
29 end
30
31 def load_plugins
32 plugins.each do |plugin|
33 plugin.load(initializer)
34 register_plugin_as_loaded(plugin)
35 end
36 ensure_all_registered_plugins_are_loaded!
37 end
38
39 # Adds the load paths for every plugin into the $LOAD_PATH. Plugin load paths are
40 # added *after* the application's <tt>lib</tt> directory, to ensure that an application
41 # can always override code within a plugin.
42 #
43 # Plugin load paths are also added to Dependencies.load_paths, and Dependencies.load_once_paths.
44 def add_plugin_load_paths
45 plugins.each do |plugin|
46 plugin.load_paths.each do |path|
47 $LOAD_PATH.insert(application_lib_index + 1, path)
48 ActiveSupport::Dependencies.load_paths << path
49 unless Rails.configuration.reload_plugins?
50 ActiveSupport::Dependencies.load_once_paths << path
51 end
52 end
53 end
54 $LOAD_PATH.uniq!
55 end
56
57 protected
58
59 # The locate_plugins method uses each class in config.plugin_locators to
60 # find the set of all plugins available to this Rails application.
61 def locate_plugins
62 configuration.plugin_locators.map { |locator|
63 locator.new(initializer).plugins
64 }.flatten
65 # TODO: sorting based on config.plugins
66 end
67
68 def register_plugin_as_loaded(plugin)
69 initializer.loaded_plugins << plugin
70 end
71
72 def configuration
73 initializer.configuration
74 end
75
76 def should_load?(plugin)
77 # uses Plugin#name and Plugin#valid?
78 enabled?(plugin) && plugin.valid?
79 end
80
81 def order_plugins(plugin_a, plugin_b)
82 if !explicit_plugin_loading_order?
83 plugin_a <=> plugin_b
84 else
85 if !explicitly_enabled?(plugin_a) && !explicitly_enabled?(plugin_b)
86 plugin_a <=> plugin_b
87 else
88 effective_order_of(plugin_a) <=> effective_order_of(plugin_b)
89 end
90 end
91 end
92
93 def effective_order_of(plugin)
94 if explicitly_enabled?(plugin)
95 registered_plugin_names.index(plugin.name)
96 else
97 registered_plugin_names.index('all')
98 end
99 end
100
101 def application_lib_index
102 $LOAD_PATH.index(File.join(RAILS_ROOT, 'lib')) || 0
103 end
104
105 def enabled?(plugin)
106 !explicit_plugin_loading_order? || registered?(plugin)
107 end
108
109 def explicit_plugin_loading_order?
110 !registered_plugin_names.nil?
111 end
112
113 def registered?(plugin)
114 explicit_plugin_loading_order? && registered_plugins_names_plugin?(plugin)
115 end
116
117 def explicitly_enabled?(plugin)
118 !explicit_plugin_loading_order? || explicitly_registered?(plugin)
119 end
120
121 def explicitly_registered?(plugin)
122 explicit_plugin_loading_order? && registered_plugin_names.include?(plugin.name)
123 end
124
125 def registered_plugins_names_plugin?(plugin)
126 registered_plugin_names.include?(plugin.name) || registered_plugin_names.include?('all')
127 end
128
129 # The plugins that have been explicitly listed with config.plugins. If this list is nil
130 # then it means the client does not care which plugins or in what order they are loaded,
131 # so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is
132 # non empty, we load the named plugins in the order specified.
133 def registered_plugin_names
134 configuration.plugins ? configuration.plugins.map(&:to_s) : nil
135 end
136
137 def loaded?(plugin_name)
138 initializer.loaded_plugins.detect { |plugin| plugin.name == plugin_name.to_s }
139 end
140
141 def ensure_all_registered_plugins_are_loaded!
142 if explicit_plugin_loading_order?
143 if configuration.plugins.detect {|plugin| plugin != :all && !loaded?(plugin) }
144 missing_plugins = configuration.plugins - (plugins + [:all])
145 raise LoadError, "Could not locate the following plugins: #{missing_plugins.to_sentence}"
146 end
147 end
148 end
149
150 end
151 end
152 end