variable_realm_union.class.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @file
  4. * Classes for Realm Union.
  5. */
  6. /**
  7. * Default Realm Union class
  8. */
  9. class VariableRealmUnionController extends VariableRealmDefaultController implements VariableRealmHooks {
  10. protected $union_realms;
  11. /**
  12. * Implementation of VariableRealmControllerInterface::__construct().
  13. */
  14. public function __construct($realm_name) {
  15. parent::__construct($realm_name);
  16. // Get / create realm controllers for each of the union realms.
  17. foreach ($this->getParentRealms() as $realm_name) {
  18. $this->union_realms[$realm_name] = variable_realm_controller($realm_name);
  19. }
  20. }
  21. /**
  22. * Implementation of VariableRealmControllerInterface::start().
  23. */
  24. public function start($realm_key = NULL) {
  25. if (!isset($this->current_key)) {
  26. if (isset($realm_key)) {
  27. return $this->current_key = $realm_key;
  28. }
  29. elseif ($request_key = $this->getRequestKey()) {
  30. return $this->current_key = $request_key;
  31. }
  32. }
  33. }
  34. /**
  35. * Implementation of VariableRealmControllerInterface::getAllKeys().
  36. */
  37. public function getAllKeys() {
  38. $all_realm_keys = $this->invokeUnionRealms('getAllKeys');
  39. // First build all combinations of realm keys.
  40. $combine = array(array('keys' => array(), 'names' => array()));
  41. foreach ($all_realm_keys as $realm => $realm_keys) {
  42. $new_combine = array();
  43. foreach ($combine as $index => $data) {
  44. foreach ($realm_keys as $new_key => $new_name) {
  45. $keys = $data['keys'];
  46. $names = $data['names'];
  47. $keys[$realm] = $new_key;
  48. $names[$realm] = $new_name;
  49. $new_combine[] = array('keys' => $keys, 'names' => $names);
  50. }
  51. }
  52. $combine = $new_combine;
  53. }
  54. // Now build all realm keys for the combinations.
  55. $keys = array();
  56. foreach ($combine as $data) {
  57. $key = $this->buildUnionKey($data['keys']);
  58. $name = $this->buildUnionName($data['names']);
  59. $keys[$key] = $name;
  60. }
  61. return $keys;
  62. }
  63. /**
  64. * Implementation of VariableRealmControllerInterface::getDefaultKey().
  65. */
  66. public function getDefaultKey() {
  67. $keys = $this->invokeUnionRealms('getDefaultKey');
  68. return $this->buildUnionKey($keys);
  69. }
  70. /**
  71. * Implementation of VariableRealmControllerInterface::getRequestKey().
  72. */
  73. public function getRequestKey() {
  74. // We'll have a request key if union realms have a current key.
  75. $keys = $this->invokeUnionRealms('getKey');
  76. return $this->buildUnionKey($keys);
  77. }
  78. /**
  79. * Implementation of VariableRealmControllerInterface::getAvailableVariables().
  80. */
  81. public function getAvailableVariables() {
  82. $variables = $this->invokeUnionRealms('getAvailableVariables');
  83. return call_user_func_array('array_intersect', $variables);
  84. }
  85. /**
  86. * Implementation of VariableRealmControllerInterface::getEnabledVariables().
  87. */
  88. public function getEnabledVariables() {
  89. $variables = $this->invokeUnionRealms('getEnabledVariables');
  90. return call_user_func_array('array_intersect', $variables);
  91. }
  92. /**
  93. * Implementation of VariableRealmControllerInterface::getParentRealms().
  94. */
  95. public function getParentRealms() {
  96. return $this->getInfo('union', array());
  97. }
  98. /**
  99. * Get union realms controllers.
  100. */
  101. protected function getUnionRealms() {
  102. return $this->union_realms;
  103. }
  104. /**
  105. * Implementation of VariableRealmHooks::variableRealmEnable()
  106. */
  107. public function variableRealmEnable($realm_name, $realm_key) {
  108. // If this realm is enabled but not active, try to find a key.
  109. if ($this->isEnabled() && !$this->isActive() && $this->isUnionRealm($realm_name) && $union_key = $this->getRequestKey()) {
  110. $this->setKey($union_key);
  111. }
  112. }
  113. /**
  114. * Implementation of VariableRealmHooks::variableRealmSwitch()
  115. */
  116. public function variableRealmSwitch($realm_name, $realm_key) {
  117. // If the this realm is active, try to find new key.
  118. if ($this->isActive() && $this->isUnionRealm($realm_name) && ($union_key = $this->getRequestKey())) {
  119. $this->setKey($union_key);
  120. }
  121. }
  122. /**
  123. * Check whether a realm belongs to the union realms.
  124. */
  125. protected function isUnionRealm($realm_name) {
  126. return isset($this->union_realms[$realm_name]);
  127. }
  128. /**
  129. * Invoke function on all realms.
  130. */
  131. protected function invokeUnionRealms($method) {
  132. return _variable_realm_invoke($this->getUnionRealms(), $method);
  133. }
  134. /**
  135. * Build key from union realm keys.
  136. */
  137. protected static function buildUnionKey($keys) {
  138. if (in_array(FALSE, $keys, TRUE)) {
  139. return FALSE;
  140. }
  141. else {
  142. // Make sure values are in correct order
  143. ksort($keys);
  144. // implode values
  145. return implode(':', $keys);
  146. }
  147. }
  148. /**
  149. * Build key name from union realm key names.
  150. */
  151. protected static function buildUnionName($names) {
  152. return implode(', ', $names);
  153. }
  154. }