Search moodle.org's
Developer Documentation


Long Term Support Release

  • Bug fixes for general core bugs in 3.1.x ended 8 May 2017 (12 months).
  • Bug fixes for security issues in 3.1.x ended 13 May 2019 (36 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x , 5.5.x or 5.6.x on Windows - http://windows.php.net/download/). PHP 7.0.x is supported but has some engine limitations. PHP 7.1.x is not supported.
  • Differences Between: [Versions 29 and 31] [Versions 31 and 33] [Versions 31 and 34] [Versions 31 and 35] [Versions 31 and 36] [Versions 31 and 37] [Versions 31 and 38]

       1  <?php
       2  // This file is part of Moodle - http://moodle.org/
       3  //
       4  // Moodle is free software: you can redistribute it and/or modify
       5  // it under the terms of the GNU General Public License as published by
       6  // the Free Software Foundation, either version 3 of the License, or
       7  // (at your option) any later version.
       8  //
       9  // Moodle is distributed in the hope that it will be useful,
      10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12  // GNU General Public License for more details.
      13  //
      14  // You should have received a copy of the GNU General Public License
      15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
      16  
      17  /**
      18   * This file contains classes that are used by the Cache API only when it is disabled.
      19   *
      20   * These classes are derivatives of other significant classes used by the Cache API customised specifically
      21   * to only do what is absolutely necessary when initialising and using the Cache API when its been disabled.
      22   *
      23   * @package    core
      24   * @category   cache
      25   * @copyright  2012 Sam Hemelryk
      26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      27   */
      28  
      29  defined('MOODLE_INTERNAL') || die();
      30  
      31  /**
      32   * Required as it is needed for cache_config_disabled which extends cache_config_writer.
      33   */
      34  require_once($CFG->dirroot.'/cache/locallib.php');
      35  
      36  /**
      37   * The cache loader class used when the Cache has been disabled.
      38   *
      39   * @copyright  2012 Sam Hemelryk
      40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      41   */
      42  class cache_disabled extends cache {
      43  
      44      /**
      45       * Constructs the cache.
      46       *
      47       * @param cache_definition $definition
      48       * @param cache_store $store
      49       * @param null $loader Unused.
      50       */
      51      public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
      52          // Nothing to do here.
      53      }
      54  
      55      /**
      56       * Gets a key from the cache.
      57       *
      58       * @param int|string $key
      59       * @param int $strictness Unused.
      60       * @return bool
      61       */
      62      public function get($key, $strictness = IGNORE_MISSING) {
      63          return false;
      64      }
      65  
      66      /**
      67       * Gets many keys at once from the cache.
      68       *
      69       * @param array $keys
      70       * @param int $strictness Unused.
      71       * @return array
      72       */
      73      public function get_many(array $keys, $strictness = IGNORE_MISSING) {
      74          $return = array();
      75          foreach ($keys as $key) {
      76              $return[$key] = false;
      77          }
      78          return $return;
      79      }
      80  
      81      /**
      82       * Sets a key value pair in the cache.
      83       *
      84       * @param int|string $key Unused.
      85       * @param mixed $data Unused.
      86       * @return bool
      87       */
      88      public function set($key, $data) {
      89          return false;
      90      }
      91  
      92      /**
      93       * Sets many key value pairs in the cache at once.
      94       *
      95       * @param array $keyvaluearray Unused.
      96       * @return int
      97       */
      98      public function set_many(array $keyvaluearray) {
      99          return 0;
     100      }
     101  
     102      /**
     103       * Deletes an item from the cache.
     104       *
     105       * @param int|string $key Unused.
     106       * @param bool $recurse Unused.
     107       * @return bool
     108       */
     109      public function delete($key, $recurse = true) {
     110          return false;
     111      }
     112  
     113      /**
     114       * Deletes many items at once from the cache.
     115       *
     116       * @param array $keys Unused.
     117       * @param bool $recurse Unused.
     118       * @return int
     119       */
     120      public function delete_many(array $keys, $recurse = true) {
     121          return 0;
     122      }
     123  
     124      /**
     125       * Checks if the cache has the requested key.
     126       *
     127       * @param int|string $key Unused.
     128       * @return bool
     129       */
     130      public function has($key) {
     131          return false;
     132      }
     133  
     134      /**
     135       * Checks if the cache has all of the requested keys.
     136       * @param array $keys Unused.
     137       * @return bool
     138       */
     139      public function has_all(array $keys) {
     140          return false;
     141      }
     142  
     143      /**
     144       * Checks if the cache has any of the requested keys.
     145       *
     146       * @param array $keys Unused.
     147       * @return bool
     148       */
     149      public function has_any(array $keys) {
     150          return false;
     151      }
     152  
     153      /**
     154       * Purges all items from the cache.
     155       *
     156       * @return bool
     157       */
     158      public function purge() {
     159          return true;
     160      }
     161  }
     162  
     163  /**
     164   * The cache factory class used when the Cache has been disabled.
     165   *
     166   * @copyright  2012 Sam Hemelryk
     167   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     168   */
     169  class cache_factory_disabled extends cache_factory {
     170  
     171      /**
     172       * Returns an instance of the cache_factor method.
     173       *
     174       * @param bool $forcereload Unused.
     175       * @return cache_factory
     176       * @throws coding_exception
     177       */
     178      public static function instance($forcereload = false) {
     179          throw new coding_exception('You must not call to this cache factory within your code.');
     180      }
     181  
     182      /**
     183       * Creates a definition instance or returns the existing one if it has already been created.
     184       *
     185       * @param string $component
     186       * @param string $area
     187       * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
     188       * @return cache_definition
     189       */
     190      public function create_definition($component, $area, $unused = null) {
     191          return cache_definition::load_adhoc(cache_store::MODE_REQUEST, $component, $area);
     192      }
     193  
     194      /**
     195       * Common public method to create a cache instance given a definition.
     196       *
     197       * @param cache_definition $definition
     198       * @return cache_application|cache_session|cache_store
     199       * @throws coding_exception
     200       */
     201      public function create_cache(cache_definition $definition) {
     202          return new cache_disabled($definition, $this->create_dummy_store($definition));
     203      }
     204  
     205      /**
     206       * Creates a cache object given the parameters for a definition.
     207       *
     208       * @param string $component
     209       * @param string $area
     210       * @param array $identifiers
     211       * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
     212       * @return cache_application|cache_session|cache_request
     213       */
     214      public function create_cache_from_definition($component, $area, array $identifiers = array(), $unused = null) {
     215          $definition = $this->create_definition($component, $area);
     216          $definition->set_identifiers($identifiers);
     217          $cache = $this->create_cache($definition);
     218          return $cache;
     219      }
     220  
     221      /**
     222       * Creates an ad-hoc cache from the given param.
     223       *
     224       * @param int $mode
     225       * @param string $component
     226       * @param string $area
     227       * @param array $identifiers
     228       * @param array $options An array of options, available options are:
     229       *   - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
     230       *   - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
     231       *   - staticacceleration : If set to true the cache will hold onto all data passing through it.
     232       *   - staticaccelerationsize : Sets the max size of the static acceleration array.
     233       * @return cache_application|cache_session|cache_request
     234       */
     235      public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
     236          $definition = cache_definition::load_adhoc($mode, $component, $area);
     237          $definition->set_identifiers($identifiers);
     238          $cache = $this->create_cache($definition);
     239          return $cache;
     240      }
     241  
     242      /**
     243       * Creates a store instance given its name and configuration.
     244       *
     245       * @param string $name Unused.
     246       * @param array $details Unused.
     247       * @param cache_definition $definition
     248       * @return boolean|cache_store
     249       */
     250      public function create_store_from_config($name, array $details, cache_definition $definition) {
     251          return $this->create_dummy_store($definition);
     252      }
     253  
     254      /**
     255       * Creates a cache config instance with the ability to write if required.
     256       *
     257       * @param bool $writer Unused.
     258       * @return cache_config_disabled|cache_config_writer
     259       */
     260      public function create_config_instance($writer = false) {
     261          // We are always going to use the cache_config_disabled class for all regular request.
     262          // However if the code has requested the writer then likely something is changing and
     263          // we're going to need to interact with the config.php file.
     264          // In this case we will still use the cache_config_writer.
     265          $class = 'cache_config_disabled';
     266          if ($writer) {
     267              // If the writer was requested then something is changing.
     268              $class = 'cache_config_writer';
     269          }
     270          if (!array_key_exists($class, $this->configs)) {
     271              self::set_state(self::STATE_INITIALISING);
     272              if ($class === 'cache_config_disabled') {
     273                  $configuration = $class::create_default_configuration();
     274              } else {
     275                  $configuration = false;
     276                  if (!cache_config::config_file_exists()) {
     277                      cache_config_writer::create_default_configuration(true);
     278                  }
     279              }
     280              $this->configs[$class] = new $class;
     281              $this->configs[$class]->load($configuration);
     282          }
     283          self::set_state(self::STATE_READY);
     284  
     285          // Return the instance.
     286          return $this->configs[$class];
     287      }
     288  }
     289  
     290  /**
     291   * The cache config class used when the Cache has been disabled.
     292   *
     293   * @copyright  2012 Sam Hemelryk
     294   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     295   */
     296  class cache_config_disabled extends cache_config_writer {
     297  
     298      /**
     299       * Returns an instance of the configuration writer.
     300       *
     301       * @return cache_config_disabled
     302       */
     303      public static function instance() {
     304          $factory = cache_factory::instance();
     305          return $factory->create_config_instance(true);
     306      }
     307  
     308      /**
     309       * Saves the current configuration.
     310       */
     311      protected function config_save() {
     312          // Nothing to do here.
     313      }
     314  
     315      /**
     316       * Generates a configuration array suitable to be written to the config file.
     317       *
     318       * @return array
     319       */
     320      protected function generate_configuration_array() {
     321          $configuration = array();
     322          $configuration['stores'] = $this->configstores;
     323          $configuration['modemappings'] = $this->configmodemappings;
     324          $configuration['definitions'] = $this->configdefinitions;
     325          $configuration['definitionmappings'] = $this->configdefinitionmappings;
     326          $configuration['locks'] = $this->configlocks;
     327          return $configuration;
     328      }
     329  
     330      /**
     331       * Adds a plugin instance.
     332       *
     333       * @param string $name Unused.
     334       * @param string $plugin Unused.
     335       * @param array $configuration Unused.
     336       * @return bool
     337       * @throws cache_exception
     338       */
     339      public function add_store_instance($name, $plugin, array $configuration = array()) {
     340          return false;
     341      }
     342  
     343      /**
     344       * Sets the mode mappings.
     345       *
     346       * @param array $modemappings Unused.
     347       * @return bool
     348       * @throws cache_exception
     349       */
     350      public function set_mode_mappings(array $modemappings) {
     351          return false;
     352      }
     353  
     354      /**
     355       * Edits a give plugin instance.
     356       *
     357       * @param string $name Unused.
     358       * @param string $plugin Unused.
     359       * @param array $configuration Unused.
     360       * @return bool
     361       * @throws cache_exception
     362       */
     363      public function edit_store_instance($name, $plugin, $configuration) {
     364          return false;
     365      }
     366  
     367      /**
     368       * Deletes a store instance.
     369       *
     370       * @param string $name Unused.
     371       * @return bool
     372       * @throws cache_exception
     373       */
     374      public function delete_store_instance($name) {
     375          return false;
     376      }
     377  
     378      /**
     379       * Creates the default configuration and saves it.
     380       *
     381       * @param bool $forcesave Ignored because we are disabled!
     382       * @return array
     383       */
     384      public static function create_default_configuration($forcesave = false) {
     385          global $CFG;
     386  
     387          // HACK ALERT.
     388          // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
     389          // default store plugins are protected from deletion.
     390          require_once($CFG->dirroot.'/cache/stores/file/lib.php');
     391          require_once($CFG->dirroot.'/cache/stores/session/lib.php');
     392          require_once($CFG->dirroot.'/cache/stores/static/lib.php');
     393  
     394          $writer = new self;
     395          $writer->configstores = array(
     396              'default_application' => array(
     397                  'name' => 'default_application',
     398                  'plugin' => 'file',
     399                  'configuration' => array(),
     400                  'features' => cachestore_file::get_supported_features(),
     401                  'modes' => cache_store::MODE_APPLICATION,
     402                  'default' => true,
     403              ),
     404              'default_session' => array(
     405                  'name' => 'default_session',
     406                  'plugin' => 'session',
     407                  'configuration' => array(),
     408                  'features' => cachestore_session::get_supported_features(),
     409                  'modes' => cache_store::MODE_SESSION,
     410                  'default' => true,
     411              ),
     412              'default_request' => array(
     413                  'name' => 'default_request',
     414                  'plugin' => 'static',
     415                  'configuration' => array(),
     416                  'features' => cachestore_static::get_supported_features(),
     417                  'modes' => cache_store::MODE_REQUEST,
     418                  'default' => true,
     419              )
     420          );
     421          $writer->configdefinitions = array();
     422          $writer->configmodemappings = array(
     423              array(
     424                  'mode' => cache_store::MODE_APPLICATION,
     425                  'store' => 'default_application',
     426                  'sort' => -1
     427              ),
     428              array(
     429                  'mode' => cache_store::MODE_SESSION,
     430                  'store' => 'default_session',
     431                  'sort' => -1
     432              ),
     433              array(
     434                  'mode' => cache_store::MODE_REQUEST,
     435                  'store' => 'default_request',
     436                  'sort' => -1
     437              )
     438          );
     439          $writer->configlocks = array(
     440              'default_file_lock' => array(
     441                  'name' => 'cachelock_file_default',
     442                  'type' => 'cachelock_file',
     443                  'dir' => 'filelocks',
     444                  'default' => true
     445              )
     446          );
     447  
     448          return $writer->generate_configuration_array();
     449      }
     450  
     451      /**
     452       * Updates the definition in the configuration from those found in the cache files.
     453       *
     454       * @param bool $coreonly Unused.
     455       */
     456      public static function update_definitions($coreonly = false) {
     457          // Nothing to do here.
     458      }
     459  
     460      /**
     461       * Locates all of the definition files.
     462       *
     463       * @param bool $coreonly Unused.
     464       * @return array
     465       */
     466      protected static function locate_definitions($coreonly = false) {
     467          return array();
     468      }
     469  
     470      /**
     471       * Sets the mappings for a given definition.
     472       *
     473       * @param string $definition Unused.
     474       * @param array $mappings Unused.
     475       * @throws coding_exception
     476       */
     477      public function set_definition_mappings($definition, $mappings) {
     478          // Nothing to do here.
     479      }
     480  }
    

    Search This Site: