/** * Returns true if the cache API can be initialised before Moodle has finished initialising itself. * * This check is essential when trying to cache the likes of configuration information. It checks to make sure that the cache * configuration file has been created which allows use to set up caching when ever is required. * * @return bool */ public static function ready_for_early_init() { return cache_config::config_file_exists(); }
/** * Creates a cache config instance with the ability to write if required. * * @param bool $writer Unused. * @return cache_config_disabled|cache_config_writer */ public function create_config_instance($writer = false) { // We are always going to use the cache_config_disabled class for all regular request. // However if the code has requested the writer then likely something is changing and // we're going to need to interact with the config.php file. // In this case we will still use the cache_config_writer. $class = 'cache_config_disabled'; if ($writer) { // If the writer was requested then something is changing. $class = 'cache_config_writer'; } if (!array_key_exists($class, $this->configs)) { self::set_state(self::STATE_INITIALISING); if ($class === 'cache_config_disabled') { $configuration = $class::create_default_configuration(); } else { $configuration = false; if (!cache_config::config_file_exists()) { cache_config_writer::create_default_configuration(true); } } $this->configs[$class] = new $class(); $this->configs[$class]->load($configuration); } self::set_state(self::STATE_READY); // Return the instance. return $this->configs[$class]; }
/** * Creates a cache config instance with the ability to write if required. * * @param bool $writer If set to true an instance that can update the configuration will be returned. * @return cache_config|cache_config_writer */ public function create_config_instance($writer = false) { global $CFG; // Check if we need to create a config file with defaults. $needtocreate = !cache_config::config_file_exists(); // The class to use. $class = 'cache_config'; if ($writer || $needtocreate) { require_once $CFG->dirroot . '/cache/locallib.php'; $class .= '_writer'; } // Check if this is a PHPUnit test and redirect to the phpunit config classes if it is. if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) { require_once $CFG->dirroot . '/cache/locallib.php'; require_once $CFG->dirroot . '/cache/tests/fixtures/lib.php'; // We have just a single class for PHP unit tests. We don't care enough about its // performance to do otherwise and having a single method allows us to inject things into it // while testing. $class = 'cache_config_phpunittest'; } $error = false; if ($needtocreate) { // Create the default configuration. // Update the state, we are now initialising the cache. self::set_state(self::STATE_INITIALISING); $configuration = $class::create_default_configuration(); if ($configuration !== true) { // Failed to create the default configuration. Disable the cache stores and update the state. self::set_state(self::STATE_ERROR_INITIALISING); $this->configs[$class] = new $class(); $this->configs[$class]->load($configuration); $error = true; } } if (!array_key_exists($class, $this->configs)) { // Create a new instance and call it to load it. $this->configs[$class] = new $class(); $this->configs[$class]->load(); } if (!$error) { // The cache is now ready to use. Update the state. self::set_state(self::STATE_READY); } // Return the instance. return $this->configs[$class]; }
/** * Creates a cache config instance with the ability to write if required. * * @param bool $writer If set to true an instance that can update the configuration will be returned. * @return cache_config|cache_config_writer */ public function create_config_instance($writer = false) { global $CFG; // Check if we need to create a config file with defaults. $needtocreate = !cache_config::config_file_exists(); // The class to use. $class = 'cache_config'; if ($writer || $needtocreate) { require_once $CFG->dirroot . '/cache/locallib.php'; $class .= '_writer'; } // Check if this is a PHPUnit test and redirect to the phpunit config classes if it is. if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) { require_once $CFG->dirroot . '/cache/locallib.php'; require_once $CFG->dirroot . '/cache/tests/fixtures/lib.php'; // We have just a single class for PHP unit tests. We don't care enough about its // performance to do otherwise and having a single method allows us to inject things into it // while testing. $class = 'cache_config_phpunittest'; } if ($needtocreate) { // Create the default configuration. $class::create_default_configuration(); } if (!array_key_exists($class, $this->configs)) { // Create a new instance and call it to load it. $this->configs[$class] = new $class(); $this->configs[$class]->load(); } // Return the instance. return $this->configs[$class]; }