function RulesEventSet::rebuildEventCache

Rebuilds the event cache.

We cache event-sets per event in order to allow efficient usage via rules_invoke_event().

See also

rules_get_cache()

rules_invoke_event()

5 calls to RulesEventSet::rebuildEventCache()
RulesEventDispatcherTestCase::testStartAndStop in tests/rules.test
Tests start and stop functionality.
RulesEventDispatcherTestCase::testStartAndStopMultiple in tests/rules.test
Tests start and stop functionality when used with multiple events.
rules_admin_settings_cache_rebuild_submit in rules_admin/rules_admin.inc
Form submit callback: Rebuild the Rules' cache.
rules_get_cache in ./rules.module
Gets a rules cache entry.
rules_update_7214 in ./rules.install
Switch out the rules_event_whitelist variable for a cache equivalent.

File

includes/rules.plugins.inc, line 851

Class

RulesEventSet
This class is used for caching the rules to be evaluated per event.

Code

public static function rebuildEventCache() {
  // Set up the per-event cache.
  $events = rules_fetch_data('event_info');
  $sets = array();
  // Add all rules associated with this event to an EventSet for caching.
  $rules = rules_config_load_multiple(FALSE, array(
    'plugin' => 'reaction rule',
    'active' => TRUE,
  ));
  foreach ($rules as $name => $rule) {
    foreach ($rule->events() as $event_name) {
      $event_base_name = rules_get_event_base_name($event_name);
      // Skip not defined events.
      if (empty($events[$event_base_name])) {
        continue;
      }
      // Create an event set if not yet done.
      if (!isset($sets[$event_name])) {
        $handler = rules_get_event_handler($event_name, $rule->getEventSettings($event_name));
        // Start the event dispatcher for this event, if any.
        if ($handler instanceof RulesEventDispatcherInterface && !$handler->isWatching()) {
          $handler->startWatching();
        }
        // Update the event info with the variables available based on the
        // event settings.
        $event_info = $events[$event_base_name];
        $event_info['variables'] = $handler->availableVariables();
        $sets[$event_name] = new RulesEventSet($event_info);
        $sets[$event_name]->name = $event_name;
      }
      // If a rule is marked as dirty, check if this still applies.
      if ($rule->dirty) {
        rules_config_update_dirty_flag($rule);
      }
      if (!$rule->dirty) {
        // Clone the rule to avoid modules getting the changed version from
        // the static cache.
        $sets[$event_name]->rule(clone $rule);
      }
    }
  }
  // Create cache items for all created sets.
  foreach ($sets as $event_name => $set) {
    $set->sortChildren();
    $set->optimize();
    // Allow modules to alter the cached event set.
    drupal_alter('rules_event_set', $event_name, $set);
    rules_set_cache('event_' . $event_name, $set);
  }
  // Cache a whitelist of configured events so we can use it to speed up later
  // calls. See rules_invoke_event().
  rules_set_cache('rules_event_whitelist', array_flip(array_keys($sets)));
}