Example #1
0
 /**
  * Loads the configuration file and parses its contents into the expected structure.
  *
  * @param array|false $configuration Can be used to force a configuration. Should only be used when truly required.
  * @return boolean
  */
 public function load($configuration = false)
 {
     global $CFG;
     if ($configuration === false) {
         $configuration = $this->include_configuration();
     }
     $this->configstores = array();
     $this->configdefinitions = array();
     $this->configlocks = array();
     $this->configmodemappings = array();
     $this->configdefinitionmappings = array();
     $this->configlockmappings = array();
     $siteidentifier = 'unknown';
     if (array_key_exists('siteidentifier', $configuration)) {
         $siteidentifier = $configuration['siteidentifier'];
     }
     $this->siteidentifier = $siteidentifier;
     // Filter the lock instances.
     $defaultlock = null;
     foreach ($configuration['locks'] as $conf) {
         if (!is_array($conf)) {
             // Something is very wrong here.
             continue;
         }
         if (!array_key_exists('name', $conf)) {
             // Not a valid definition configuration.
             continue;
         }
         $name = $conf['name'];
         if (array_key_exists($name, $this->configlocks)) {
             debugging('Duplicate cache lock detected. This should never happen.', DEBUG_DEVELOPER);
             continue;
         }
         $conf['default'] = !empty($conf['default']);
         if ($defaultlock === null || $conf['default']) {
             $defaultlock = $name;
         }
         $this->configlocks[$name] = $conf;
     }
     // Filter the stores.
     $availableplugins = cache_helper::early_get_cache_plugins();
     foreach ($configuration['stores'] as $store) {
         if (!is_array($store) || !array_key_exists('name', $store) || !array_key_exists('plugin', $store)) {
             // Not a valid instance configuration.
             debugging('Invalid cache store in config. Missing name or plugin.', DEBUG_DEVELOPER);
             continue;
         }
         $plugin = $store['plugin'];
         $class = 'cachestore_' . $plugin;
         $exists = array_key_exists($plugin, $availableplugins);
         if (!$exists) {
             // Not a valid plugin, or has been uninstalled, just skip it an carry on.
             debugging('Invalid cache store in config. Not an available plugin.', DEBUG_DEVELOPER);
             continue;
         }
         $file = $CFG->dirroot . '/cache/stores/' . $plugin . '/lib.php';
         if (!class_exists($class) && file_exists($file)) {
             require_once $file;
         }
         if (!class_exists($class)) {
             continue;
         }
         if (!array_key_exists('cache_store', class_parents($class))) {
             continue;
         }
         if (!array_key_exists('configuration', $store) || !is_array($store['configuration'])) {
             $store['configuration'] = array();
         }
         $store['class'] = $class;
         $store['default'] = !empty($store['default']);
         if (!array_key_exists('lock', $store) || !array_key_exists($store['lock'], $this->configlocks)) {
             $store['lock'] = $defaultlock;
         }
         $this->configstores[$store['name']] = $store;
     }
     // Filter the definitions.
     foreach ($configuration['definitions'] as $id => $conf) {
         if (!is_array($conf)) {
             // Something is very wrong here.
             continue;
         }
         if (!array_key_exists('mode', $conf) || !array_key_exists('component', $conf) || !array_key_exists('area', $conf)) {
             // Not a valid definition configuration.
             continue;
         }
         if (array_key_exists($id, $this->configdefinitions)) {
             debugging('Duplicate cache definition detected. This should never happen.', DEBUG_DEVELOPER);
             continue;
         }
         $conf['mode'] = (int) $conf['mode'];
         if ($conf['mode'] < cache_store::MODE_APPLICATION || $conf['mode'] > cache_store::MODE_REQUEST) {
             // Invalid cache mode used for the definition.
             continue;
         }
         $this->configdefinitions[$id] = $conf;
     }
     // Filter the mode mappings.
     foreach ($configuration['modemappings'] as $mapping) {
         if (!is_array($mapping) || !array_key_exists('mode', $mapping) || !array_key_exists('store', $mapping)) {
             // Not a valid mapping configuration.
             debugging('A cache mode mapping entry is invalid.', DEBUG_DEVELOPER);
             continue;
         }
         if (!array_key_exists($mapping['store'], $this->configstores)) {
             // Mapped array instance doesn't exist.
             debugging('A cache mode mapping exists for a mode or store that does not exist.', DEBUG_DEVELOPER);
             continue;
         }
         $mapping['mode'] = (int) $mapping['mode'];
         if ($mapping['mode'] < 0 || $mapping['mode'] > 4) {
             // Invalid cache type used for the mapping.
             continue;
         }
         if (!array_key_exists('sort', $mapping)) {
             $mapping['sort'] = 0;
         }
         $this->configmodemappings[] = $mapping;
     }
     // Filter the definition mappings.
     foreach ($configuration['definitionmappings'] as $mapping) {
         if (!is_array($mapping) || !array_key_exists('definition', $mapping) || !array_key_exists('store', $mapping)) {
             // Not a valid mapping configuration.
             continue;
         }
         if (!array_key_exists($mapping['store'], $this->configstores)) {
             // Mapped array instance doesn't exist.
             continue;
         }
         if (!array_key_exists($mapping['definition'], $this->configdefinitions)) {
             // Mapped array instance doesn't exist.
             continue;
         }
         if (!array_key_exists('sort', $mapping)) {
             $mapping['sort'] = 0;
         }
         $this->configdefinitionmappings[] = $mapping;
     }
     usort($this->configmodemappings, array($this, 'sort_mappings'));
     usort($this->configdefinitionmappings, array($this, 'sort_mappings'));
     return true;
 }