1.20.x bootstrap.inc state_initialize()

Loads the persistent state table.

The state table is composed of values that have been saved in the table with state_set().

File

includes/bootstrap.inc, line 1272
Functions that need to be loaded on every Backdrop request.

Code

function state_initialize() {
  if ($cached = cache('cache')->get('states')) {
    $states = $cached->data;
  }
  else {
    // Cache miss. Avoid a stampede.
    $name = 'state_init';
    if (!lock_acquire($name, 1)) {
      // Another request is building the variable cache.
      // Wait, then re-run this function.
      lock_wait($name);
      return state_initialize();
    }
    else {
      // Proceed with variable rebuild.
      $states = array_map('unserialize', db_query('SELECT name, value FROM {state}')->fetchAllKeyed());
      cache('cache')->set('states', $states);
      lock_release($name);
    }
  }

  return $states;
}