Projet

Général

Profil

Paste
Télécharger (25,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / includes / rules.plugins.inc @ d719f12f

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file Contains plugin info and implementations not needed for rule evaluation.
5
 */
6
7
8
/**
9
 * Implements a rules action.
10
 */
11
class RulesAction extends RulesAbstractPlugin implements RulesActionInterface {
12
13
  protected $itemName = 'action';
14
15
  /**
16
   * Execute the callback and update/save data as specified by the action.
17
   */
18
  protected function executeCallback(array $args, RulesState $state = NULL) {
19
    rules_log('Evaluating the action %name.', array('%name' => $this->elementName), RulesLog::INFO, $this);
20
    $return = $this->__call('execute', empty($this->info['named parameter']) ? $args : array($args));
21
    // Get the (partially) wrapped arguments.
22
    $args = $state->currentArguments;
23
24
    if (is_array($return)) {
25
      foreach ($return as $name => $data) {
26
        // Add provided variables.
27
        if (isset($this->info['provides'][$name])) {
28
          $var_name = isset($this->settings[$name . ':var']) ? $this->settings[$name . ':var'] : $name;
29
          if (!$state->varInfo($var_name)) {
30
            $state->addVariable($var_name, $data, $this->info['provides'][$name]);
31
            rules_log('Added the provided variable %name of type %type', array('%name' => $var_name, '%type' => $this->info['provides'][$name]['type']), RulesLog::INFO, $this);
32
            if (!empty($this->info['provides'][$name]['save']) && $state->variables[$var_name] instanceof EntityMetadataWrapper) {
33
              $state->saveChanges($var_name, $state->variables[$var_name]);
34
            }
35
          }
36
        }
37
        // Support updating variables by returning the values.
38
        elseif (!isset($this->info['provides'][$name])) {
39
          // Update the data value using the wrapper.
40
          if (isset($args[$name]) && $args[$name] instanceof EntityMetadataWrapper) {
41
            try {
42
              $args[$name]->set($data);
43
            }
44
            catch (EntityMetadataWrapperException $e) {
45
              throw new RulesEvaluationException('Unable to update the argument for parameter %name: %error', array('%name' => $name, '%error' => $e->getMessage()), $this);
46
            }
47
          }
48
          elseif (array_key_exists($name, $args)) {
49
            // Map back to the source variable name and update it.
50
            $var_name = !empty($this->settings[$name . ':select']) ? str_replace('-', '_', $this->settings[$name . ':select']) : $name;
51
            $state->variables[$var_name] = $data;
52
          }
53
        }
54
      }
55
    }
56
    // Save parameters as defined in the parameter info.
57
    if ($return !== FALSE) {
58
      foreach ($this->info['parameter'] as $name => $info) {
59
        if (!empty($info['save']) && $args[$name] instanceof EntityMetadataWrapper) {
60
          if (isset($this->settings[$name . ':select'])) {
61
            $state->saveChanges($this->settings[$name . ':select'], $args[$name]);
62
          }
63
          else {
64
            // Wrapper has been configured via direct input, so just save.
65
            rules_log('Saved argument of type %type for parameter %name.', array('%name' => $name, '%type' => $args[$name]->type()));
66
            $args[$name]->save();
67
          }
68
        }
69
      }
70
    }
71
  }
72
}
73
74
/**
75
 * Implements a rules condition.
76
 */
77
class RulesCondition extends RulesAbstractPlugin implements RulesConditionInterface {
78
79
  protected $itemName = 'condition';
80
  protected $negate = FALSE;
81
82
  public function providesVariables() {
83
    return array();
84
  }
85
86
  public function negate($negate = TRUE) {
87
    $this->negate = (bool) $negate;
88
    return $this;
89
  }
90
91
  public function isNegated() {
92
    return $this->negate;
93
  }
94
95
  protected function executeCallback(array $args, RulesState $state = NULL) {
96
    $return = (bool) $this->__call('execute', empty($this->info['named parameter']) ? $args : array($args));
97
    rules_log('The condition %name evaluated to %bool', array('%name' => $this->elementName, '%bool' => $return ? 'TRUE' : 'FALSE'), RulesLog::INFO, $this);
98
    return $this->negate ? !$return : $return;
99
  }
100
101
  public function __sleep() {
102
    return parent::__sleep() + array('negate' => 'negate');
103
  }
104
105
  /**
106
   * Just return the boolean result.
107
   */
108
  protected function returnVariables(RulesState $state, $result = NULL) {
109
    return $result;
110
  }
111
112
  protected function exportToArray() {
113
    $not = $this->negate ? 'NOT ' : '';
114
    $export = $this->exportSettings();
115
    // Abbreviate the export making "USING" implicit.
116
    return array($not . $this->elementName => isset($export['USING']) ? $export['USING'] : array());
117
  }
118
119
  public function import(array $export) {
120
    $this->elementName = rules_array_key($export);
121
    if (strpos($this->elementName, 'NOT ') === 0) {
122
      $this->elementName = substr($this->elementName, 4);
123
      $this->negate = TRUE;
124
    }
125
    // After setting the element name, setup the element again so the right
126
    // element info is loaded.
127
    $this->setUp();
128
129
    // Re-add 'USING' which has been removed for abbreviation.
130
    $this->importSettings(array('USING' => reset($export)));
131
  }
132
133
  public function label() {
134
    $label = parent::label();
135
    return $this->negate ? t('NOT @condition', array('@condition' => $label)) : $label;
136
  }
137
}
138
139
/**
140
 * An actual rule.
141
 * Note: A rule also implements the RulesActionInterface (inherited).
142
 */
143
class Rule extends RulesActionContainer {
144
145
  protected $conditions = NULL;
146
  protected $itemName = 'rule';
147
148
  public $label = 'unlabeled';
149
150
  public function __construct($variables = array(), $providesVars = array()) {
151
    parent::__construct($variables, $providesVars);
152
153
    // Initialize the conditions container.
154
    if (!isset($this->conditions)) {
155
      $this->conditions = rules_and();
156
      // Don't use setParent() to avoid having it added to the children.
157
      $this->conditions->parent = $this;
158
    }
159
  }
160
161
  /**
162
   * Get an iterator over all contained conditions. Note that this iterator also
163
   * implements the ArrayAcces interface.
164
   *
165
   * @return RulesRecursiveElementIterator
166
   */
167
  public function conditions() {
168
    return $this->conditions->getIterator();
169
  }
170
171
  /**
172
   * Returns the "And" condition container, which contains all conditions of
173
   * this rule.
174
   *
175
   * @return RulesAnd
176
   */
177
  public function conditionContainer() {
178
    return $this->conditions;
179
  }
180
181
  public function __sleep() {
182
    return parent::__sleep() + drupal_map_assoc(array('conditions', 'label'));
183
  }
184
185
  /**
186
   * Get an iterator over all contained actions. Note that this iterator also
187
   * implements the ArrayAccess interface.
188
   *
189
   * @return RulesRecursiveElementIterator
190
   */
191
  public function actions() {
192
    return parent::getIterator();
193
  }
194
195
  /**
196
   * Add a condition. Pass either an instance of the RulesConditionInterface
197
   * or the arguments as needed by rules_condition().
198
   *
199
   * @return Rule
200
   *   Returns $this to support chained usage.
201
   */
202
  public function condition($name, $settings = array()) {
203
    $this->conditions->condition($name, $settings);
204
    return $this;
205
  }
206
207
  public function sortChildren($deep = FALSE) {
208
    $this->conditions->sortChildren($deep);
209
    parent::sortChildren($deep);
210
  }
211
212
  public function evaluate(RulesState $state) {
213
    rules_log('Evaluating conditions of rule %label.', array('%label' => $this->label), RulesLog::INFO, $this);
214
    if ($this->conditions->evaluate($state)) {
215
      rules_log('Rule %label fires.', array('%label' => $this->label), RulesLog::INFO, $this, TRUE);
216
      parent::evaluate($state);
217
      rules_log('Rule %label has fired.', array('%label' => $this->label), RulesLog::INFO, $this, FALSE);
218
    }
219
  }
220
221
  /**
222
   * Fires the rule, i.e. evaluates the rule without checking its conditions.
223
   *
224
   * @see RulesPlugin::evaluate()
225
   */
226
  public function fire(RulesState $state) {
227
    rules_log('Firing rule %label.', array('%label' => $this->label), RulesLog::INFO, $this);
228
    parent::evaluate($state);
229
  }
230
231
  public function integrityCheck() {
232
    parent::integrityCheck();
233
    $this->conditions->integrityCheck();
234
    return $this;
235
  }
236
237
  public function access() {
238
    return (!isset($this->conditions) || $this->conditions->access()) && parent::access();
239
  }
240
241
  public function dependencies() {
242
    return array_keys(array_flip($this->conditions->dependencies()) + array_flip(parent::dependencies()));
243
  }
244
245
  public function destroy() {
246
    $this->conditions->destroy();
247
    parent::destroy();
248
  }
249
250
  /**
251
   * @return RulesRecursiveElementIterator
252
   */
253
  public function getIterator() {
254
    $array = array_merge(array($this->conditions), $this->children);
255
    return new RulesRecursiveElementIterator($array);
256
  }
257
258
  protected function stateVariables($element = NULL) {
259
    // Don't add in provided action variables for the conditions.
260
    if (isset($element) && $element === $this->conditions) {
261
      return $this->availableVariables();
262
    }
263
    $vars = parent::stateVariables($element);
264
    // Take variable info assertions of conditions into account.
265
    if ($assertions = $this->conditions->variableInfoAssertions()) {
266
      $vars = RulesData::addMetadataAssertions($vars, $assertions);
267
    }
268
    return $vars;
269
  }
270
271
  protected function exportFlat() {
272
    return $this->isRoot();
273
  }
274
275
  protected function exportToArray() {
276
    $export = parent::exportToArray();
277
    if (!$this->isRoot()) {
278
      $export[strtoupper($this->plugin())]['LABEL'] = $this->label;
279
    }
280
    return $export;
281
  }
282
283
  protected function exportChildren($key = NULL) {
284
    $export = array();
285
    if ($this->conditions->children) {
286
      $export = $this->conditions->exportChildren('IF');
287
    }
288
    return $export + parent::exportChildren('DO');
289
  }
290
291
  public function import(array $export) {
292
    if (!$this->isRoot() && isset($export[strtoupper($this->plugin())]['LABEL'])) {
293
      $this->label = $export[strtoupper($this->plugin())]['LABEL'];
294
    }
295
    parent::import($export);
296
  }
297
298
  protected function importChildren($export, $key = NULL) {
299
    if (!empty($export['IF'])) {
300
      $this->conditions->importChildren($export, 'IF');
301
    }
302
    parent::importChildren($export, 'DO');
303
  }
304
305
  public function __clone() {
306
    parent::__clone();
307
    $this->conditions = clone $this->conditions;
308
    $this->conditions->parent = $this;
309
  }
310
311
  /**
312
   * Rules may not provided any variable info assertions, as Rules are only
313
   * conditionally executed.
314
   */
315
  protected function variableInfoAssertions() {
316
    return array();
317
  }
318
319
  /**
320
   * Overridden to ensure the whole Rule is deleted at once.
321
   */
322
  public function delete($keep_children = FALSE) {
323
    parent::delete($keep_children);
324
  }
325
326
  /**
327
   * Overriden to expose the variables of all actions for embedded rules.
328
   */
329
  public function providesVariables() {
330
    $provides = parent::providesVariables();
331
    if (!$this->isRoot()) {
332
      foreach ($this->actions() as $action) {
333
        $provides += $action->providesVariables();
334
      }
335
    }
336
    return $provides;
337
  }
338
339
  public function resetInternalCache() {
340
    parent::resetInternalCache();
341
    $this->conditions->resetInternalCache();
342
  }
343
}
344
345
/**
346
 * Represents rules getting triggered by events.
347
 */
348
class RulesReactionRule extends Rule implements RulesTriggerableInterface {
349
350
  protected $itemName = 'reaction rule';
351
  protected $events = array(), $eventSettings = array();
352
353
  /**
354
   * Implements RulesTriggerableInterface::events().
355
   */
356
  public function events() {
357
    return $this->events;
358
  }
359
360
  /**
361
   * Implements RulesTriggerableInterface::removeEvent().
362
   */
363
  public function removeEvent($event) {
364
    if (($id = array_search($event, $this->events)) !== FALSE) {
365
      unset($this->events[$id]);
366
    }
367
    return $this;
368
  }
369
370
  /**
371
   * Implements RulesTriggerableInterface::event().
372
   */
373
  public function event($event_name, array $settings = NULL) {
374
    // Process any settings and determine the configured event's name.
375
    if ($settings) {
376
      $handler = rules_get_event_handler($event_name, $settings);
377
      if ($suffix = $handler->getEventNameSuffix()) {
378
        $event_name .= '--' . $suffix;
379
        $this->eventSettings[$event_name] = $settings;
380
      }
381
      else {
382
        // Do not store settings if there is no suffix.
383
        unset($this->eventSettings[$event_name]);
384
      }
385
    }
386
    if (array_search($event_name, $this->events) === FALSE) {
387
      $this->events[] = $event_name;
388
    }
389
    return $this;
390
  }
391
392
  /**
393
   * Implements RulesTriggerableInterface::getEventSettings().
394
   */
395
  public function getEventSettings($event_name) {
396
    if (isset($this->eventSettings[$event_name])) {
397
      return $this->eventSettings[$event_name];
398
    }
399
  }
400
401
  public function integrityCheck() {
402
    parent::integrityCheck();
403
    // Check integrity of the configured events.
404
    foreach ($this->events as $event_name) {
405
      $handler = rules_get_event_handler($event_name, $this->getEventSettings($event_name));
406
      $handler->validate();
407
    }
408
    return $this;
409
  }
410
411
  /**
412
   * Reaction rules can't add variables to the parent scope, so clone $state.
413
   */
414
  public function evaluate(RulesState $state) {
415
    // Implement recursion prevention for reaction rules.
416
    if ($state->isBlocked($this)) {
417
      return rules_log('Not evaluating @plugin %label to prevent recursion.', array('%label' => $this->label(), '@plugin' => $this->plugin()), RulesLog::INFO, $this);
418
    }
419
    $state->block($this);
420
    $copy = clone $state;
421
    parent::evaluate($copy);
422
    $state->unblock($this);
423
  }
424
425
  public function access() {
426
    foreach ($this->events as $event_name) {
427
      $event_info = rules_get_event_info($event_name);
428
      if (!empty($event_info['access callback']) && !call_user_func($event_info['access callback'], 'event', $event_info['name'])) {
429
        return FALSE;
430
      }
431
    }
432
    return parent::access();
433
  }
434
435
  public function dependencies() {
436
    $modules = array_flip(parent::dependencies());
437
    foreach ($this->events as $event_name) {
438
      $event_info = rules_get_event_info($event_name);
439
      if (isset($event_info['module'])) {
440
        $modules[$event_info['module']] = TRUE;
441
      }
442
    }
443
    return array_keys($modules);
444
  }
445
446
  public function providesVariables() {
447
    return array();
448
  }
449
450
  public function parameterInfo($optional = FALSE) {
451
    // If executed directly, all variables as defined by the event need to
452
    // be passed.
453
    return rules_filter_array($this->availableVariables(), 'handler', FALSE);
454
  }
455
456
  public function availableVariables() {
457
    if (!isset($this->availableVariables)) {
458
      if (isset($this->parent)) {
459
        // Return the event variables provided by the event set, once cached.
460
        $this->availableVariables = $this->parent->stateVariables();
461
      }
462
      else {
463
        // The intersection of the variables provided by the events are
464
        // available.
465
        foreach ($this->events as $event_name) {
466
          $handler = rules_get_event_handler($event_name, $this->getEventSettings($event_name));
467
468
          if (isset($this->availableVariables)) {
469
            $event_vars = $handler->availableVariables();
470
            // Merge variable info by intersecting the variable-info keys also,
471
            // so we have only metadata available that is valid for all of the
472
            // provided variables.
473
            foreach (array_intersect_key($this->availableVariables, $event_vars) as $name => $variable_info) {
474
              $this->availableVariables[$name] = array_intersect_key($variable_info, $event_vars[$name]);
475
            }
476
          }
477
          else {
478
            $this->availableVariables = $handler->availableVariables();
479
          }
480
        }
481
        $this->availableVariables = isset($this->availableVariables) ? RulesState::defaultVariables() + $this->availableVariables : RulesState::defaultVariables();
482
      }
483
    }
484
    return $this->availableVariables;
485
  }
486
487
  public function __sleep() {
488
    return parent::__sleep() + drupal_map_assoc(array('events', 'eventSettings'));
489
  }
490
491
  protected function exportChildren($key = 'ON') {
492
    foreach ($this->events as $event_name) {
493
      $export[$key][$event_name] = (array) $this->getEventSettings($event_name);
494
    }
495
    return $export + parent::exportChildren();
496
  }
497
498
  protected function importChildren($export, $key = 'ON') {
499
    // Detect and support old-style exports: a numerically indexed array of
500
    // event names.
501
    if (is_string(reset($export[$key])) && is_numeric(key($export[$key]))) {
502
      $this->events = $export[$key];
503
    }
504
    else {
505
      $this->events = array_keys($export[$key]);
506
      $this->eventSettings = array_filter($export[$key]);
507
    }
508
    parent::importChildren($export);
509
  }
510
511
  /**
512
   * Overrides optimize().
513
   */
514
  public function optimize() {
515
    parent::optimize();
516
    // No need to keep event settings for evaluation.
517
    $this->eventSettings = array();
518
  }
519
}
520
521
/**
522
 * A logical AND.
523
 */
524
class RulesAnd extends RulesConditionContainer {
525
526
  protected $itemName = 'and';
527
528
  public function evaluate(RulesState $state) {
529
    foreach ($this->children as $condition) {
530
      if (!$condition->evaluate($state)) {
531
        rules_log('AND evaluated to FALSE.');
532
        return $this->negate;
533
      }
534
    }
535
    rules_log('AND evaluated to TRUE.');
536
    return !$this->negate;
537
  }
538
539
  public function label() {
540
    return !empty($this->label) ? $this->label : ($this->negate ? t('NOT AND') : t('AND'));
541
  }
542
}
543
544
/**
545
 * A logical OR.
546
 */
547
class RulesOr extends RulesConditionContainer {
548
549
  protected $itemName = 'or';
550
551
  public function evaluate(RulesState $state) {
552
    foreach ($this->children as $condition) {
553
      if ($condition->evaluate($state)) {
554
        rules_log('OR evaluated to TRUE.');
555
        return !$this->negate;
556
      }
557
    }
558
    rules_log('OR evaluated to FALSE.');
559
    return $this->negate;
560
  }
561
562
  public function label() {
563
    return !empty($this->label) ? $this->label : ($this->negate ? t('NOT OR') : t('OR'));
564
  }
565
566
  /**
567
   * Overridden to exclude all variable assertions as in an OR we cannot assert
568
   * the children are successfully evaluated.
569
   */
570
  protected function stateVariables($element = NULL) {
571
    $vars = $this->availableVariables();
572
    if (isset($element)) {
573
      // Add in variables provided by siblings executed before the element.
574
      foreach ($this->children as $child) {
575
        if ($child === $element) {
576
          break;
577
        }
578
        $vars += $child->providesVariables();
579
      }
580
    }
581
    return $vars;
582
  }
583
}
584
585
/**
586
 * A loop element.
587
 */
588
class RulesLoop extends RulesActionContainer {
589
590
  protected $itemName = 'loop';
591
  protected $listItemInfo;
592
593
  public function __construct($settings = array(), $variables = NULL) {
594
    $this->setUp();
595
    $this->settings = (array) $settings + array(
596
      'item:var' => 'list_item',
597
      'item:label' => t('Current list item'),
598
    );
599
    if (!empty($variables)) {
600
      $this->info['variables'] = $variables;
601
    }
602
  }
603
604
  public function pluginParameterInfo() {
605
    $info['list'] = array(
606
      'type' => 'list',
607
      'restriction' => 'selector',
608
      'label' => t('List'),
609
      'description' => t('The list to loop over. The loop will step through each item in the list, allowing further actions on them. See <a href="@url"> the online handbook</a> for more information on how to use loops.',
610
        array('@url' => rules_external_help('loops'))),
611
    );
612
    return $info;
613
  }
614
615
  public function integrityCheck() {
616
    parent::integrityCheck();
617
    $this->checkVarName($this->settings['item:var']);
618
  }
619
620
  public function listItemInfo() {
621
    if (!isset($this->listItemInfo)) {
622
      if ($info = $this->getArgumentInfo('list')) {
623
        // Pass through the variable info keys like property info.
624
        $this->listItemInfo = array_intersect_key($info, array_flip(array('type', 'property info', 'bundle')));
625
        $this->listItemInfo['type'] = isset($info['type']) ? entity_property_list_extract_type($info['type']) : 'unknown';
626
      }
627
      else {
628
        $this->listItemInfo = array('type' => 'unknown');
629
      }
630
      $this->listItemInfo['label'] = $this->settings['item:label'];
631
    }
632
    return $this->listItemInfo;
633
  }
634
635
  public function evaluate(RulesState $state) {
636
    try {
637
      $param_info = $this->pluginParameterInfo();
638
      $list = $this->getArgument('list', $param_info['list'], $state);
639
      $item_var_info = $this->listItemInfo();
640
      $item_var_name = $this->settings['item:var'];
641
642
      if (isset($this->settings['list:select'])) {
643
        rules_log('Looping over the list items of %selector', array('%selector' => $this->settings['list:select']), RulesLog::INFO, $this);
644
      }
645
646
      // Loop over the list and evaluate the children for each list item.
647
      foreach ($list as $key => $item) {
648
        // Use a separate state so variables are available in the loop only.
649
        $state2 = clone $state;
650
        $state2->addVariable($item_var_name, $list[$key], $item_var_info);
651
        parent::evaluate($state2);
652
653
        // Update variables from parent scope.
654
        foreach ($state->variables as $var_key => &$var_value) {
655
          if (array_key_exists($var_key, $state2->variables)) {
656
            $var_value = $state2->variables[$var_key];
657
          }
658
        }
659
      }
660
    }
661
    catch (RulesEvaluationException $e) {
662
      rules_log($e->msg, $e->args, $e->severity);
663
      rules_log('Unable to evaluate %name.', array('%name' => $this->getPluginName()), RulesLog::WARN, $this);
664
    }
665
  }
666
667
  protected function stateVariables($element = NULL) {
668
    return array($this->settings['item:var'] => $this->listItemInfo()) + parent::stateVariables($element);
669
  }
670
671
  public function label() {
672
    return !empty($this->label) ? $this->label : t('Loop');
673
  }
674
675
  protected function exportChildren($key = 'DO') {
676
    return parent::exportChildren($key);
677
  }
678
679
  protected function importChildren($export, $key = 'DO') {
680
    parent::importChildren($export, $key);
681
  }
682
683
  protected function exportSettings() {
684
    $export = parent::exportSettings();
685
    $export['ITEM'][$this->settings['item:var']] = $this->settings['item:label'];
686
    return $export;
687
  }
688
689
  protected function importSettings($export) {
690
    parent::importSettings($export);
691
    if (isset($export['ITEM'])) {
692
      $this->settings['item:var'] = rules_array_key($export['ITEM']);
693
      $this->settings['item:label'] = reset($export['ITEM']);
694
    }
695
  }
696
}
697
698
/**
699
 * An action set component.
700
 */
701
class RulesActionSet extends RulesActionContainer {
702
703
  protected $itemName = 'action set';
704
705
}
706
707
/**
708
 * A set of rules to execute upon defined variables.
709
 */
710
class RulesRuleSet extends RulesActionContainer {
711
712
  protected $itemName = 'rule set';
713
714
  /**
715
   * @return RulesRuleSet
716
   */
717
  public function rule($rule) {
718
    return $this->action($rule);
719
  }
720
721
  protected function exportChildren($key = 'RULES') {
722
    return parent::exportChildren($key);
723
  }
724
725
  protected function importChildren($export, $key = 'RULES') {
726
    parent::importChildren($export, $key);
727
  }
728
}
729
730
/**
731
 * This class is used for caching the rules to be evaluated per event.
732
 */
733
class RulesEventSet extends RulesRuleSet {
734
735
  protected $itemName = 'event set';
736
  // Event sets may recurse as we block recursions on rule-level.
737
  public $recursion = TRUE;
738
739
  public function __construct($info = array()) {
740
    $this->setup();
741
    $this->info = $info;
742
  }
743
744
  public function executeByArgs($args = array()) {
745
    rules_log('Reacting on event %label.', array('%label' => $this->info['label']), RulesLog::INFO, NULL, TRUE);
746
    $state = $this->setUpState($args);
747
    module_invoke_all('rules_config_execute', $this);
748
    $this->evaluate($state);
749
    $state->cleanUp($this);
750
    rules_log('Finished reacting on event %label.', array('%label' => $this->info['label']), RulesLog::INFO, NULL, FALSE);
751
  }
752
753
  /**
754
   * Cache event-sets per event to allow efficient usage via rules_invoke_event().
755
   *
756
   * @see rules_get_cache()
757
   * @see rules_invoke_event()
758
   */
759
  public static function rebuildEventCache() {
760
    // Set up the per-event cache.
761
    $events = rules_fetch_data('event_info');
762
    $sets = array();
763
    // Add all rules associated with this event to an EventSet for caching.
764
    $rules = rules_config_load_multiple(FALSE, array('plugin' => 'reaction rule', 'active' => TRUE));
765
766
    foreach ($rules as $name => $rule) {
767
      foreach ($rule->events() as $event_name) {
768
        $event_base_name = rules_get_event_base_name($event_name);
769
        // Skip not defined events.
770
        if (empty($events[$event_base_name])) {
771
          continue;
772
        }
773
        // Create an event set if not yet done.
774
        if (!isset($sets[$event_name])) {
775
          $handler = rules_get_event_handler($event_name, $rule->getEventSettings($event_name));
776
777
          // Start the event dispatcher for this event, if any.
778
          if ($handler instanceof RulesEventDispatcherInterface && !$handler->isWatching()) {
779
            $handler->startWatching();
780
          }
781
782
          // Update the event info with the variables available based on the
783
          // event settings.
784
          $event_info = $events[$event_base_name];
785
          $event_info['variables'] = $handler->availableVariables();
786
          $sets[$event_name] = new RulesEventSet($event_info);
787
          $sets[$event_name]->name = $event_name;
788
        }
789
790
        // If a rule is marked as dirty, check if this still applies.
791
        if ($rule->dirty) {
792
          rules_config_update_dirty_flag($rule);
793
        }
794
        if (!$rule->dirty) {
795
          // Clone the rule to avoid modules getting the changed version from
796
          // the static cache.
797
          $sets[$event_name]->rule(clone $rule);
798
        }
799
      }
800
    }
801
802
    // Create cache items for all created sets.
803
    foreach ($sets as $event_name => $set) {
804
      $set->sortChildren();
805
      $set->optimize();
806
      // Allow modules to alter the cached event set.
807
      drupal_alter('rules_event_set', $event_name, $set);
808
      rules_set_cache('event_' . $event_name, $set);
809
    }
810
    // Cache a whitelist of configured events so we can use it to speed up later
811
    // calls. See rules_invoke_event().
812 c22e192e Assos Assos
    rules_set_cache('rules_event_whitelist', array_flip(array_keys($sets)));
813 85ad3d82 Assos Assos
  }
814
815
  protected function stateVariables($element = NULL) {
816
    return $this->availableVariables();
817
  }
818
819
  /**
820
   * Do not save since this class is for caching purposes only.
821
   *
822
   * @see RulesPlugin::save()
823
   */
824
  public function save($name = NULL, $module = 'rules') {
825
    return FALSE;
826
  }
827
}