Projet

Général

Profil

Paste
Télécharger (3,86 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / variable / variable_store / variable_store.module @ 7942932f

1
<?php
2
/**
3
 * @file
4
 * Variable API module - Database storage
5
 *
6
 * This module provides database storage for variable realms
7
 */
8

    
9
/**
10
 * Get variable store
11
 */
12
function &variable_store($realm, $key) {
13
  $variable_store = &drupal_static('variable_store');
14

    
15
  if (!isset($variable_store[$realm][$key])) {
16
    $variable_store[$realm][$key] = _variable_store_load($realm, $key);
17
  }
18
  return $variable_store[$realm][$key];
19
}
20

    
21
/**
22
 * Implementation of hook_boot()
23
 */
24
function variable_store_boot() {
25
  // Do nothing, we just want this module to be available for boot.
26
}
27

    
28
/**
29
 * Delete variable from db
30
 */
31
function variable_store_del($realm, $key, $name) {
32
  $store = &variable_store($realm, $key);
33
  db_delete('variable_store')
34
    ->condition('realm', $realm)
35
    ->condition('realm_key', $key)
36
    ->condition('name', $name)
37
    ->execute();
38
  unset($store[$name]);
39
  cache_clear_all('variable:' . $realm . ':' . $key, 'cache_bootstrap');
40
}
41

    
42
/**
43
 * Get single variable from store
44
 */
45
function variable_store_get($realm, $key, $name, $default = NULL) {
46
  $variables = variable_store($realm, $key);
47
  return $variables && isset($variables[$name]) ? $variables[$name] : $default;
48
}
49

    
50
/**
51
 * Delete realm variable or full realm from store.
52
 *
53
 * @param $realm
54
 *   Realm name to delete. NULL to delete all realms.
55
 * @param $key
56
 *   Realm key to delete. NULL to delete all realm keys.
57
 * @param $name
58
 *   Variable name to delete. NULL to delete all variables for that realm, key
59
 */
60
function variable_store_delete_all($realm, $key, $name = NULL) {
61
  _variable_store_reset();
62
  $query = db_delete('variable_store');
63
  if (isset($realm)) {
64
    $query->condition('realm', $realm);
65
  }
66
  if (isset($key)) {
67
    $query->condition('realm_key', $key);
68
  }
69
  if (isset($name)) {
70
    $query->condition('name', $name);
71
  }
72
  return $query->execute();
73
}
74

    
75
/**
76
 * List all variable names from a realm.
77
 *
78
 * @param $realm
79
 *   Realm name to list. NULL to list all realms.
80
 * @param $key
81
 *   Realm key to list. NULL to list all realm keys.
82
 *
83
 * @return array
84
 *   List of variable names.
85
 */
86
function variable_store_list_all($realm, $key = NULL) {
87
  $query = db_select('variable_store', 'vs')
88
    ->fields('vs', array('name'))
89
    ->distinct();
90
  if ($realm) {
91
    $query->condition('realm', $realm);
92
  }
93
  if ($key) {
94
    $query->condition('realm_key', $key);
95
  }
96
  return $query->execute()->fetchCol();
97
}
98

    
99
/**
100
 * Load realm from db store
101
 */
102
function _variable_store_load($realm, $key) {
103
  $cacheid = 'variable:' . $realm . ':' . $key;
104
  if ($cached = cache_get($cacheid, 'cache_bootstrap')) {
105
    $variables = $cached->data;
106
  }
107
  else {
108
    $result = db_select('variable_store', 's')
109
      ->fields('s', array('name', 'value', 'serialized'))
110
      ->condition('realm', $realm)
111
      ->condition('realm_key', $key)
112
      ->execute();
113
    $variables = array();
114
    foreach ($result as $variable) {
115
      $variables[$variable->name] = $variable->serialized ? unserialize($variable->value) : $variable->value;
116
    }
117
    cache_set($cacheid, $variables, 'cache_bootstrap');
118
  }
119
  return $variables;
120
}
121

    
122
/**
123
 * Reset caches and static variables.
124
 */
125
function _variable_store_reset() {
126
  $store =  &drupal_static('variable_store', array());
127
  foreach ($store as $realm => &$realm_store) {
128
    foreach (array_keys($realm_store) as $key) {
129
      $realm_store[$key] = NULL;
130
    }
131
  }
132
  cache_clear_all('variable:', 'cache_bootstrap', TRUE);
133
}
134

    
135
/**
136
 * Set variable value
137
 */
138
function variable_store_set($realm, $key, $name, $value, $rebuild = TRUE) {
139
  $store = &variable_store($realm, $key);
140
  $serialize = !is_int($value) && !is_string($value);
141
  db_merge('variable_store')
142
    ->key(array('realm' => $realm, 'realm_key' => $key, 'name' => $name))
143
    ->fields(array('value' => $serialize ? serialize($value) : $value, 'serialized' => $serialize ? 1 : 0))
144
    ->execute();
145
  cache_clear_all('variable:' . $realm . ':' . $key, 'cache_bootstrap');
146
  $store[$name] = $value;
147
}
148