variable_realm.class.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. /**
  3. * @file
  4. * Variable realm controller
  5. */
  6. /**
  7. * Realm Controller Interface.
  8. */
  9. interface VariableRealmControllerInterface {
  10. /**
  11. * Class constructor.
  12. *
  13. * @param $realm_name
  14. * Realm name.
  15. * @param $store_class
  16. * Realm key.
  17. */
  18. public function __construct($realm_name);
  19. /**
  20. * Check whether the realm is enabled (has a key set).
  21. */
  22. public function isEnabled();
  23. /**
  24. * Check whether the realm is active (has a valid key set).
  25. */
  26. public function isActive();
  27. /**
  28. * Start this realm with request key.
  29. *
  30. * This will only change the realm key if the realm is not initialized yet.
  31. */
  32. public function enable($realm_key = NULL);
  33. /**
  34. * Get title for this realm.
  35. */
  36. public function getTitle();
  37. /**
  38. * Get name for variables in this realm.
  39. */
  40. public function getVariableName();
  41. /**
  42. * Add store for realm key.
  43. *
  44. * @param $variables
  45. * Optional array with variable values to initialize the realm key store.
  46. */
  47. public function addStore($realm_key, $variables = NULL);
  48. /**
  49. * Get store for realm key.
  50. */
  51. public function getStore($realm_key);
  52. /**
  53. * Set store for realm key.
  54. */
  55. public function setStore($realm_key, $realm_store);
  56. /**
  57. * Get current realm key.
  58. */
  59. public function getKey();
  60. /**
  61. * Get default key for this realm.
  62. */
  63. public function getDefaultKey();
  64. /**
  65. * Get key for this page request.
  66. */
  67. public function getRequestKey();
  68. /**
  69. * Set current realm key.
  70. */
  71. public function setKey($realm_key);
  72. /**
  73. * Get all keys for this realm.
  74. */
  75. public function getAllKeys();
  76. /**
  77. * Get other realms this one may depend upon.
  78. */
  79. public function getParentRealms();
  80. /**
  81. * Get current realm weight.
  82. */
  83. public function getWeight();
  84. /**
  85. * Set current realm weight.
  86. */
  87. public function setWeight($weight);
  88. /**
  89. * Implementation of VariableRealmControllerInterface::getWeight().
  90. */
  91. public function getDefaultWeight();
  92. /**
  93. * Get current variable store.
  94. */
  95. public function getCurrentStore();
  96. /**
  97. * List all current variable values.
  98. */
  99. public function getCurrentVariables();
  100. /**
  101. * Get information for this realm.
  102. *
  103. * Return information provided by hook_variable_realm_info()
  104. */
  105. public function getInfo($property = NULL, $default = NULL);
  106. /**
  107. * Get list of variables available for this realm.
  108. */
  109. public function getAvailableVariables();
  110. /**
  111. * Get list of variables enabled for this realm.
  112. */
  113. public function getEnabledVariables();
  114. /**
  115. * Get system variable for this realm.
  116. */
  117. public function getRealmVariable($name, $default = NULL);
  118. /**
  119. * Set system variable for this realm.
  120. */
  121. public function setRealmVariable($name, $value);
  122. /**
  123. * Delete variable for all keys this realm.
  124. *
  125. * @param $variable_name
  126. * Variable name to delete.
  127. */
  128. public function deleteVariable($variable_name);
  129. }
  130. /**
  131. * Variable Realm Hooks.
  132. */
  133. interface VariableRealmHooks {
  134. /**
  135. * Callback function that will be invoked when a realm is enabled.
  136. */
  137. public function variableRealmEnable($realm_name, $realm_key);
  138. /**
  139. * Callback method that will be invoked when a realm is switched.
  140. */
  141. public function variableRealmSwitch($realm_name, $realm_key);
  142. }
  143. /**
  144. * Realm Store Interface.
  145. */
  146. interface VariableRealmStoreInterface {
  147. /**
  148. * Class constructor.
  149. *
  150. * @param $realm
  151. * Realm name.
  152. * @param $key
  153. * Realm key.
  154. * @param $variables
  155. * Optional array of variables to initialize the realm with.
  156. */
  157. public function __construct($realm, $key, $variables = NULL);
  158. /**
  159. * Initialize variables.
  160. */
  161. public function variable_init();
  162. /**
  163. * Get single variable.
  164. *
  165. * @param $name
  166. * Variable name
  167. * @param $default
  168. * Default value
  169. */
  170. public function variable_get($name, $default = NULL);
  171. /**
  172. * Set single variable.
  173. *
  174. * @param $name
  175. * Variable name
  176. * @param $value
  177. * Variable value
  178. */
  179. public function variable_set($name, $value);
  180. /**
  181. * Delete single variable.
  182. *
  183. * @param $name
  184. * Variable name
  185. */
  186. public function variable_del($name);
  187. /**
  188. * Add single variable to the realm for current request.
  189. *
  190. * While variable set actually sets the variable on whatever realm storage
  191. * we are using, this function just sets a runtime value.
  192. *
  193. * @param $name
  194. * Variable name
  195. * @param $value
  196. * Variable value
  197. */
  198. public function variable_add($name, $value);
  199. /**
  200. * Check whether a variable exists in the realm.
  201. */
  202. public function variable_exists($name);
  203. /**
  204. * List all current variable values.
  205. */
  206. public function variable_list();
  207. }
  208. /**
  209. * Base class, keeps static list of variables.
  210. */
  211. class VariableRealmDefaultController implements VariableRealmControllerInterface {
  212. // Unique realm name (language, domain..)
  213. public $realm_name;
  214. // Current realm key.
  215. public $current_key;
  216. // Current realm weight.
  217. public $current_weight;
  218. // Array of variable stores indexed by realm key.
  219. protected $store;
  220. /**
  221. * Implementation of VariableRealmControllerInterface::__construct().
  222. */
  223. public function __construct($realm_name) {
  224. $this->realm_name = $realm_name;
  225. $this->current_weight = $this->getDefaultWeight();
  226. }
  227. /**
  228. * Implementation of VariableRealmControllerInterface::isEnabled()
  229. */
  230. public function isEnabled() {
  231. return isset($this->current_key);
  232. }
  233. /**
  234. * Implementation of VariableRealmControllerInterface::isActive()
  235. */
  236. public function isActive() {
  237. return $this->isEnabled() && $this->current_key !== FALSE;
  238. }
  239. /**
  240. * Implementation of VariableRealmControllerInterface::enable().
  241. */
  242. public function enable($realm_key = NULL) {
  243. if (!isset($this->current_key)) {
  244. return $this->current_key = isset($realm_key) ? $realm_key : $this->getRequestKey();
  245. }
  246. }
  247. /**
  248. * Implementation of VariableRealmControllerInterface::getTitle().
  249. */
  250. public function getTitle() {
  251. return $this->getInfo('title');
  252. }
  253. /**
  254. * Implementation of VariableRealmControllerInterface::getVariableName().
  255. */
  256. public function getVariableName() {
  257. return $this->getInfo('variable name');
  258. }
  259. /**
  260. * Implementation of VariableRealmControllerInterface::getStore().
  261. */
  262. public function getStore($realm_key) {
  263. if (isset($this->store[$realm_key])) {
  264. return $this->store[$realm_key];
  265. }
  266. else {
  267. return $this->addStore($realm_key);
  268. }
  269. }
  270. /**
  271. * Implementation of VariableRealmControllerInterface::addStore().
  272. */
  273. public function addStore($realm_key, $variables = NULL) {
  274. $store = $this->createStore($realm_key, $variables);
  275. $this->setStore($realm_key, $store);
  276. return $store;
  277. }
  278. /**
  279. * Create Store for key.
  280. */
  281. protected function createStore($realm_key, $variables = NULL) {
  282. $class = $this->getInfo('store class');
  283. $class = $class && class_exists($class) ? $class : 'VariableRealmDefaultStore';
  284. return new $class($this->realm_name, $realm_key, $variables);
  285. }
  286. /**
  287. * Set store for realm key.
  288. */
  289. public function setStore($realm_key, $realm_store) {
  290. $this->store[$realm_key] = $realm_store;
  291. }
  292. /**
  293. * Implementation of VariableRealmControllerInterface::setKey().
  294. */
  295. public function setKey($realm_key) {
  296. $this->current_key = $realm_key;
  297. }
  298. /**
  299. * Implementation of VariableRealmControllerInterface::getKey().
  300. */
  301. public function getKey() {
  302. return isset($this->current_key) ? $this->current_key : FALSE;
  303. }
  304. /**
  305. * Implementation of VariableRealmControllerInterface::getAllKeys().
  306. */
  307. public function getAllKeys() {
  308. return $this->getInfo('keys', array());
  309. }
  310. /**
  311. * Implementation of VariableRealmControllerInterface::getDefaultKey().
  312. */
  313. public function getDefaultKey() {
  314. return $this->getInfo('default key', FALSE);
  315. }
  316. /**
  317. * Implementation of VariableRealmControllerInterface::getRequestKey().
  318. */
  319. public function getRequestKey() {
  320. return FALSE;
  321. }
  322. /**
  323. * Implementation of VariableRealmControllerInterface::getWeight().
  324. */
  325. public function getWeight() {
  326. return isset($this->current_weight) ? $this->current_weight : $this->controller_data['weight'];
  327. }
  328. /**
  329. * Implementation of VariableRealmControllerInterface::setWeight().
  330. */
  331. public function setWeight($weight) {
  332. $this->current_weight = $weight;
  333. }
  334. /**
  335. * Implementation of VariableRealmControllerInterface::getWeight().
  336. */
  337. public function getDefaultWeight() {
  338. return $this->getRealmVariable('weight', $this->getInfo('weight', 0));
  339. }
  340. /**
  341. * Implementation of VariableRealmControllerInterface::getParentRealms().
  342. */
  343. public function getParentRealms() {
  344. return array();
  345. }
  346. /**
  347. * Implementation of VariableRealmControllerInterface::getCurrentVariables().
  348. */
  349. public function getCurrentVariables() {
  350. if ($store = $this->getCurrentStore()) {
  351. return $store->variable_list();
  352. }
  353. else {
  354. return array();
  355. }
  356. }
  357. /**
  358. * Implementation of VariableRealmControllerInterface::getCurrentStore().
  359. */
  360. public function getCurrentStore() {
  361. if ($this->isActive()) {
  362. return $this->getStore($this->getKey());
  363. }
  364. else {
  365. return NULL;
  366. }
  367. }
  368. /**
  369. * Implementation of VariableRealmControllerInterface::getAvailableVariables().
  370. */
  371. public function getAvailableVariables() {
  372. if ($options = $this->getInfo('options')) {
  373. return $options;
  374. }
  375. else {
  376. // Defaults to all available variables.
  377. return array_keys(variable_get_info());
  378. }
  379. }
  380. /**
  381. * Implementation of VariableRealmControllerInterface::getEnabledVariables().
  382. */
  383. public function getEnabledVariables() {
  384. if ($this->getInfo('select')) {
  385. return $this->getRealmVariable('list', array());
  386. }
  387. else {
  388. // If the variable is not set it will default to all variables
  389. return $this->getAvailableVariables();
  390. }
  391. }
  392. /**
  393. * Implementation of VariableRealmControllerInterface::getInfo().
  394. */
  395. public function getInfo($property = NULL, $default = NULL) {
  396. $info = variable_realm_info($this->realm_name);
  397. if ($property) {
  398. return isset($info[$property]) ? $info[$property] : $default;
  399. }
  400. else {
  401. return $info;
  402. }
  403. }
  404. /**
  405. * Implementation of VariableRealmControllerInterface::getRealmVariable().
  406. */
  407. public function getRealmVariable($name, $default = NULL) {
  408. return variable_get('variable_realm_' . $name . '_' . $this->realm_name, $default);
  409. }
  410. /**
  411. * Implementation of VariableRealmControllerInterface::setRealmVariable().
  412. */
  413. public function setRealmVariable($name, $value) {
  414. variable_realm_global_set('variable_realm_' . $name . '_' . $this->realm_name, $value);
  415. }
  416. /**
  417. * Implementation of VariableRealmControllerInterface::deleteVariable().
  418. */
  419. public function deleteVariable($variable_name) {
  420. foreach ($this->getAllKeys() as $key => $name) {
  421. variable_realm_del($this->realm_name, $key, $variable_name, FALSE);
  422. }
  423. }
  424. }
  425. /**
  426. * Base class, keeps static list of variables.
  427. */
  428. class VariableRealmDefaultStore implements VariableRealmStoreInterface {
  429. public $realm;
  430. public $key;
  431. protected $variables;
  432. /**
  433. * Class constructor.
  434. */
  435. public function __construct($realm, $key, $variables = NULL) {
  436. $this->realm = $realm;
  437. $this->key = $key;
  438. $this->variables = $variables;
  439. }
  440. /**
  441. * Initialize variables.
  442. */
  443. public function variable_init() {
  444. if (!isset($this->variables)) {
  445. $this->variables = array();
  446. }
  447. }
  448. /**
  449. * Get single variable.
  450. *
  451. * @param $name
  452. * Variable name
  453. * @param $default
  454. * Default value
  455. */
  456. public function variable_get($name, $default = NULL) {
  457. $this->variable_init();
  458. return isset($this->variables[$name]) ? $this->variables[$name] : $default;
  459. }
  460. /**
  461. * Set single variable.
  462. *
  463. * @param $name
  464. * Variable name
  465. * @param $value
  466. * Variable value
  467. */
  468. public function variable_set($name, $value) {
  469. $this->variable_init();
  470. $this->variables[$name] = $value;
  471. }
  472. /**
  473. * Delete single variable.
  474. *
  475. * @param $name
  476. * Variable name
  477. */
  478. public function variable_del($name) {
  479. $this->variable_init();
  480. unset($this->variables[$name]);
  481. }
  482. /**
  483. * Implementation of VariableRealmStoreInterface::variable_add().
  484. */
  485. public function variable_add($name, $value) {
  486. $this->variable_init();
  487. $this->variables[$name] = $value;
  488. }
  489. /**
  490. * Implementation of VariableRealmStoreInterface::variable_exists().
  491. */
  492. public function variable_exists($name) {
  493. $this->variable_init();
  494. return isset($this->variables[$name]);
  495. }
  496. /**
  497. * List all current variable values.
  498. */
  499. public function variable_list() {
  500. $this->variable_init();
  501. return $this->variables;
  502. }
  503. }
  504. /**
  505. * Controller for default system variables.
  506. */
  507. class VariableRealmGlobalStore extends VariableRealmDefaultStore {
  508. /**
  509. * Initialize variables.
  510. */
  511. public function variable_init() {
  512. if (!isset($this->variables)) {
  513. $this->variables = $GLOBALS['conf'];
  514. }
  515. }
  516. /**
  517. * Set single variable.
  518. *
  519. * @param $name
  520. * Variable name
  521. * @param $value
  522. * Variable value
  523. */
  524. public function variable_set($name, $value) {
  525. parent::variable_set($name, $value);
  526. variable_set($name, $value);
  527. }
  528. /**
  529. * Delete single variable.
  530. *
  531. * @param $name
  532. * Variable name
  533. */
  534. public function variable_del($name) {
  535. parent::variable_del($name);
  536. variable_del($name);
  537. }
  538. }