Révision 950416da
Ajouté par Assos Assos il y a plus de 5 ans
drupal7/sites/all/modules/rules/includes/rules.state.inc | ||
---|---|---|
1 | 1 |
<?php |
2 | 2 |
|
3 | 3 |
/** |
4 |
* @file Contains the state and data related stuff. |
|
4 |
* @file |
|
5 |
* Contains the state and data related stuff. |
|
5 | 6 |
*/ |
6 | 7 |
|
7 | 8 |
/** |
... | ... | |
14 | 15 |
|
15 | 16 |
/** |
16 | 17 |
* Globally keeps the ids of rules blocked due to recursion prevention. |
18 |
* |
|
19 |
* @var array |
|
17 | 20 |
*/ |
18 | 21 |
static protected $blocked = array(); |
19 | 22 |
|
20 | 23 |
/** |
21 | 24 |
* The known variables. |
25 |
* |
|
26 |
* @var array |
|
22 | 27 |
*/ |
23 | 28 |
public $variables = array(); |
24 | 29 |
|
25 | 30 |
/** |
26 | 31 |
* Holds info about the variables. |
32 |
* |
|
33 |
* @var array |
|
27 | 34 |
*/ |
28 | 35 |
protected $info = array(); |
29 | 36 |
|
... | ... | |
33 | 40 |
protected $save; |
34 | 41 |
|
35 | 42 |
/** |
36 |
* Holds the arguments while an element is executed. May be used by the |
|
37 |
* element to easily access the wrapped arguments. |
|
43 |
* Holds the arguments while an element is executed. |
|
44 |
* |
|
45 |
* May be used by the element to easily access the wrapped arguments. |
|
38 | 46 |
*/ |
39 | 47 |
public $currentArguments; |
40 | 48 |
|
... | ... | |
43 | 51 |
*/ |
44 | 52 |
protected $currentlyBlocked; |
45 | 53 |
|
46 |
|
|
54 |
/** |
|
55 |
* Constructs a RulesState object. |
|
56 |
*/ |
|
47 | 57 |
public function __construct() { |
48 | 58 |
// Use an object in order to ensure any cloned states reference the same |
49 | 59 |
// save information. |
... | ... | |
131 | 141 |
* |
132 | 142 |
* If necessary, the specified handler is invoked to fetch the variable. |
133 | 143 |
* |
134 |
* @param $name |
|
144 |
* @param string $name
|
|
135 | 145 |
* The name of the variable to return. |
136 | 146 |
* |
137 | 147 |
* @return |
... | ... | |
164 | 174 |
* |
165 | 175 |
* @param $selector |
166 | 176 |
* The data selector of the wrapper to save or just a variable name. |
167 |
* @param $immediate |
|
177 |
* @param $wrapper |
|
178 |
* @param bool $immediate |
|
168 | 179 |
* Pass FALSE to postpone saving to later on. Else it's immediately saved. |
169 | 180 |
*/ |
170 | 181 |
public function saveChanges($selector, $wrapper, $immediate = FALSE) { |
... | ... | |
234 | 245 |
} |
235 | 246 |
|
236 | 247 |
/** |
237 |
* Merges the info about to be saved variables form the given state into the |
|
238 |
* existing state. Therefor we can aggregate saves from invoked components. |
|
239 |
* Merged in saves are removed from the given state, but not mergable saves |
|
248 |
* Merges info from the given state into the existing state. |
|
249 |
* |
|
250 |
* Merges the info about to-be-saved variables from the given state into the |
|
251 |
* existing state. Therefore we can aggregate saves from invoked components. |
|
252 |
* Merged-in saves are removed from the given state, but not-mergeable saves |
|
240 | 253 |
* remain there. |
241 | 254 |
* |
242 | 255 |
* @param $state |
... | ... | |
267 | 280 |
/** |
268 | 281 |
* Returns an entity metadata wrapper as specified in the selector. |
269 | 282 |
* |
270 |
* @param $selector |
|
283 |
* @param string $selector
|
|
271 | 284 |
* The selector string, e.g. "node:author:mail". |
272 |
* @param $langcode |
|
285 |
* @param string $langcode
|
|
273 | 286 |
* (optional) The language code used to get the argument value if the |
274 | 287 |
* argument value should be translated. Defaults to LANGUAGE_NONE. |
275 | 288 |
* |
... | ... | |
291 | 304 |
try { |
292 | 305 |
foreach (explode(':', $parts[1]) as $name) { |
293 | 306 |
if ($wrapper instanceof EntityListWrapper || $wrapper instanceof EntityStructureWrapper) { |
294 |
// Make sure we are usign the right language. Wrappers might be cached
|
|
307 |
// Make sure we are using the right language. Wrappers might be cached
|
|
295 | 308 |
// and have previous langcodes set, so always set the right language. |
296 | 309 |
if ($wrapper instanceof EntityStructureWrapper) { |
297 | 310 |
$wrapper->language($langcode); |
... | ... | |
312 | 325 |
|
313 | 326 |
/** |
314 | 327 |
* Magic method. Only serialize variables and their info. |
328 |
* |
|
315 | 329 |
* Additionally we remember currently blocked configs, so we can restore them |
316 | 330 |
* upon deserialization using restoreBlocks(). |
317 | 331 |
*/ |
318 |
public function __sleep () {
|
|
332 |
public function __sleep() { |
|
319 | 333 |
$this->currentlyBlocked = self::$blocked; |
320 | 334 |
return array('info', 'variables', 'currentlyBlocked'); |
321 | 335 |
} |
322 | 336 |
|
337 |
/** |
|
338 |
* Magic method. Unserialize variables and their info. |
|
339 |
*/ |
|
323 | 340 |
public function __wakeup() { |
324 | 341 |
$this->save = new ArrayObject(); |
325 | 342 |
} |
326 | 343 |
|
327 | 344 |
/** |
328 |
* Restore the before serialization blocked configurations.
|
|
345 |
* Restores the before-serialization blocked configurations.
|
|
329 | 346 |
* |
330 | 347 |
* Warning: This overwrites any possible currently blocked configs. Thus |
331 |
* do not invoke this method, if there might be evaluations active.
|
|
348 |
* do not invoke this method if there might be evaluations active. |
|
332 | 349 |
*/ |
333 | 350 |
public function restoreBlocks() { |
334 | 351 |
self::$blocked = $this->currentlyBlocked; |
335 | 352 |
} |
336 | 353 |
|
337 | 354 |
/** |
338 |
* Defines always available variables. |
|
355 |
* Defines always-available variables. |
|
356 |
* |
|
357 |
* @param $key |
|
358 |
* (optional) |
|
339 | 359 |
*/ |
340 | 360 |
public static function defaultVariables($key = NULL) { |
341 | 361 |
// Add a variable for accessing site-wide data properties. |
... | ... | |
350 | 370 |
); |
351 | 371 |
return isset($key) ? $vars[$key] : $vars; |
352 | 372 |
} |
373 |
|
|
353 | 374 |
} |
354 | 375 |
|
355 | 376 |
/** |
356 | 377 |
* A class holding static methods related to data. |
357 | 378 |
*/ |
358 |
class RulesData {
|
|
379 |
class RulesData { |
|
359 | 380 |
|
360 | 381 |
/** |
361 | 382 |
* Returns whether the type match. They match if type1 is compatible to type2. |
... | ... | |
364 | 385 |
* The name of the type to check for whether it is compatible to type2. |
365 | 386 |
* @param $param_info |
366 | 387 |
* The type expression to check for. |
367 |
* @param $ancestors |
|
368 |
* Whether sub-type relationships for checking type compatibility should be
|
|
369 |
* taken into account. Defaults to TRUE. |
|
388 |
* @param bool $ancestors
|
|
389 |
* (optional) Whether sub-type relationships for checking type compatibility
|
|
390 |
* should be taken into account. Defaults to TRUE.
|
|
370 | 391 |
* |
371 |
* @return |
|
392 |
* @return bool
|
|
372 | 393 |
* Whether the types match. |
373 | 394 |
*/ |
374 | 395 |
public static function typesMatch($var_info, $param_info, $ancestors = TRUE) { |
... | ... | |
395 | 416 |
$cache = &rules_get_cache(); |
396 | 417 |
self::typeCalcAncestors($cache, $var_type); |
397 | 418 |
// If one of the types is an ancestor return TRUE. |
398 |
return (bool)array_intersect_key($cache['data_info'][$var_type]['ancestors'], array_flip($valid_types)); |
|
419 |
return (bool) array_intersect_key($cache['data_info'][$var_type]['ancestors'], array_flip($valid_types));
|
|
399 | 420 |
} |
400 | 421 |
return FALSE; |
401 | 422 |
} |
... | ... | |
417 | 438 |
} |
418 | 439 |
|
419 | 440 |
/** |
420 |
* Returns matching data variables or properties for the given info and the to |
|
421 |
* be configured parameter. |
|
441 |
* Returns data for the given info and the to-be-configured parameter. |
|
442 |
* |
|
443 |
* Returns matching data variables or properties for the given info and the |
|
444 |
* to-be-configured parameter. |
|
422 | 445 |
* |
423 | 446 |
* @param $source |
424 | 447 |
* Either an array of info about available variables or a entity metadata |
425 | 448 |
* wrapper. |
426 | 449 |
* @param $param_info |
427 | 450 |
* The information array about the to be configured parameter. |
428 |
* @param $prefix |
|
451 |
* @param string $prefix
|
|
429 | 452 |
* An optional prefix for the data selectors. |
430 |
* @param $recursions |
|
453 |
* @param int $recursions
|
|
431 | 454 |
* The number of recursions used to go down the tree. Defaults to 2. |
432 |
* @param $suggestions |
|
455 |
* @param bool $suggestions
|
|
433 | 456 |
* Whether possibilities to recurse are suggested as soon as the deepest |
434 | 457 |
* level of recursions is reached. Defaults to TRUE. |
435 | 458 |
* |
436 |
* @return |
|
437 |
* An array of info about matching variables or properties that match, keyed |
|
438 |
* with the data selector. |
|
459 |
* @return array
|
|
460 |
* An array of info about matching variables or properties that match, keyed
|
|
461 |
* with the data selector.
|
|
439 | 462 |
*/ |
440 | 463 |
public static function matchingDataSelector($source, $param_info, $prefix = '', $recursions = 2, $suggestions = TRUE) { |
441 | 464 |
// If an array of info is given, get entity metadata wrappers first. |
... | ... | |
467 | 490 |
$matches += self::matchingDataSelector($wrapper, $param_info, $prefix . $name . ':', $recursions - 1, $suggestions); |
468 | 491 |
} |
469 | 492 |
elseif ($suggestions) { |
470 |
// We may not recurse any more, but indicate the possibility to recurse. |
|
493 |
// We may not recurse any more, |
|
494 |
// but indicate the possibility to recurse. |
|
471 | 495 |
$matches[$prefix . $name . ':'] = $wrapper->info(); |
472 | 496 |
if (!is_array($source) && $source instanceof EntityListWrapper) { |
473 | 497 |
// Add some more possible list items. |
... | ... | |
482 | 506 |
} |
483 | 507 |
|
484 | 508 |
/** |
485 |
* Adds asserted metadata to the variable info. In case there are already |
|
486 |
* assertions for a variable, the assertions are merged such that both apply. |
|
509 |
* Adds asserted metadata to the variable info. |
|
510 |
* |
|
511 |
* In case there are already assertions for a variable, the assertions are |
|
512 |
* merged such that both apply. |
|
487 | 513 |
* |
488 | 514 |
* @see RulesData::applyMetadataAssertions() |
489 | 515 |
*/ |
... | ... | |
546 | 572 |
} |
547 | 573 |
|
548 | 574 |
/** |
549 |
* Property info alter callback for the entity metadata wrapper for applying |
|
550 |
* the rules metadata assertions. |
|
575 |
* Property info alter callback for the entity metadata wrapper. |
|
576 |
* |
|
577 |
* Used for applying the rules metadata assertions. |
|
551 | 578 |
* |
552 | 579 |
* @see RulesData::addMetadataAssertions() |
553 | 580 |
*/ |
... | ... | |
585 | 612 |
$property_info['properties'][$key]['rules assertion'] = $assertion[$key]; |
586 | 613 |
$property_info['properties'][$key]['property info alter'] = array('RulesData', 'applyMetadataAssertions'); |
587 | 614 |
|
588 |
// Apply any 'type' and 'bundle' assertion directly to the propertyinfo. |
|
615 |
// Apply any 'type' and 'bundle' assertion directly to the property |
|
616 |
// info. |
|
589 | 617 |
if (isset($assertion[$key]['#info']['type'])) { |
590 | 618 |
$type = $assertion[$key]['#info']['type']; |
591 | 619 |
// Support asserting a type in case of generic entity references only. |
... | ... | |
607 | 635 |
} |
608 | 636 |
|
609 | 637 |
/** |
610 |
* Property info alter callback for the entity metadata wrapper to inject |
|
611 |
* metadata for the 'site' variable. In contrast to doing this via |
|
612 |
* hook_rules_data_info() this callback makes use of the already existing |
|
638 |
* Property info alter callback for the entity metadata wrapper. |
|
639 |
* |
|
640 |
* Used to inject metadata for the 'site' variable. In contrast to doing this |
|
641 |
* via hook_rules_data_info() this callback makes use of the already existing |
|
613 | 642 |
* property info cache for site information of entity metadata. |
614 | 643 |
* |
615 | 644 |
* @see RulesPlugin::availableVariables() |
... | ... | |
621 | 650 |
// have specified further metadata. |
622 | 651 |
return RulesData::applyMetadataAssertions($wrapper, $property_info); |
623 | 652 |
} |
653 |
|
|
624 | 654 |
} |
625 | 655 |
|
626 | 656 |
/** |
... | ... | |
652 | 682 |
* The type of the passed data. |
653 | 683 |
* @param $data |
654 | 684 |
* Optional. The data to wrap or its identifier. |
655 |
* @param $info |
|
685 |
* @param array $info
|
|
656 | 686 |
* Optional. Used internally to pass info about properties down the tree. |
657 | 687 |
*/ |
658 | 688 |
public function __construct($type, $data = NULL, $info = array()) { |
... | ... | |
721 | 751 |
} |
722 | 752 |
|
723 | 753 |
/** |
724 |
* Prepare for serializiation.
|
|
754 |
* Prepare for serialization. |
|
725 | 755 |
*/ |
726 | 756 |
public function __sleep() { |
727 | 757 |
$vars = parent::__sleep(); |
... | ... | |
733 | 763 |
return $vars; |
734 | 764 |
} |
735 | 765 |
|
766 |
/** |
|
767 |
* Prepare for unserialization. |
|
768 |
*/ |
|
736 | 769 |
public function __wakeup() { |
737 | 770 |
if ($this->id !== FALSE) { |
738 | 771 |
// Make sure data is set, so the data will be loaded when needed. |
... | ... | |
755 | 788 |
* The loaded data object, or FALSE if loading failed. |
756 | 789 |
*/ |
757 | 790 |
abstract protected function load($id); |
791 |
|
|
758 | 792 |
} |
759 | 793 |
|
760 | 794 |
/** |
761 |
* Interface that allows custom wrapper classes to declare that they are savable.
|
|
795 |
* Used to declare custom wrapper classes as savable.
|
|
762 | 796 |
*/ |
763 | 797 |
interface RulesDataWrapperSavableInterface { |
764 | 798 |
|
... | ... | |
766 | 800 |
* Save the currently wrapped data. |
767 | 801 |
*/ |
768 | 802 |
public function save(); |
803 |
|
|
769 | 804 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules