Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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   * Read slave helper that exposes selected moodle_read_slave_trait metods
  19   *
  20   * @package    core
  21   * @category   dml
  22   * @copyright  2018 Srdjan Janković, Catalyst IT
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core;
  27  
  28  use ReflectionProperty;
  29  
  30  /**
  31   * Read slave helper that exposes selected moodle_read_slave_trait metods
  32   *
  33   * @package    core
  34   * @category   dml
  35   * @copyright  2018 Catalyst IT
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  trait test_moodle_read_slave_trait {
  39      /**
  40       * Constructs a mock db driver
  41       *
  42       * @param bool $external
  43       */
  44      public function __construct($external = false) {
  45          parent::__construct($external);
  46  
  47          $rw = fopen("php://memory", 'r+');
  48          fputs($rw, 'rw');
  49  
  50          $ro = fopen("php://memory", 'r+');
  51          fputs($ro, 'ro');
  52  
  53          $this->prefix = 'test_'; // Default, not to leave empty.
  54  
  55          $rcp = new ReflectionProperty(parent::class, 'wantreadslave');
  56          $rcp->setAccessible(true);
  57          $rcp->setValue($this, true);
  58  
  59          $this->dbhwrite = $rw;
  60          $this->dbhreadonly = $ro;
  61          $this->set_db_handle($this->dbhwrite);
  62  
  63          $this->temptables = new \moodle_temptables($this);
  64      }
  65  
  66      /**
  67       * Check db handle
  68       * @param string $id
  69       * @return bool
  70       */
  71      public function db_handle_is($id) {
  72          $dbh = $this->get_db_handle();
  73          rewind($dbh);
  74          return stream_get_contents($dbh) == $id;
  75      }
  76  
  77      /**
  78       * Check db handle is rw
  79       * @return bool
  80       */
  81      public function db_handle_is_rw() {
  82          return $this->db_handle_is('rw');
  83      }
  84  
  85      /**
  86       * Check db handle is ro
  87       * @return bool
  88       */
  89      public function db_handle_is_ro() {
  90          return $this->db_handle_is('ro');
  91      }
  92  
  93      /**
  94       * Upgrade to public
  95       * @return resource
  96       */
  97      public function get_db_handle() {
  98          return parent::get_db_handle();
  99      }
 100  
 101      /**
 102       * Upgrade to public
 103       * @param string $sql
 104       * @param array|null $params
 105       * @param int $type
 106       * @param array $extrainfo
 107       */
 108      public function query_start($sql, ?array $params, $type, $extrainfo = null) {
 109          return parent::query_start($sql, $params, $type);
 110      }
 111  
 112      /**
 113       * Upgrade to public
 114       * @param mixed $result
 115       */
 116      public function query_end($result) {
 117          parent::query_end($result);
 118          $this->set_db_handle($this->dbhwrite);
 119      }
 120  
 121      /**
 122       * Upgrade to public
 123       */
 124      public function dispose() {
 125      }
 126  }