Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * std_proxy class.
  19   *
  20   * @package    core_calendar
  21   * @copyright  2017 Cameron Ball <cameron@cameron1729.xyz>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_calendar\local\event\proxies;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_calendar\local\event\exceptions\member_does_not_exist_exception;
  30  
  31  /**
  32   * stdClass proxy.
  33   *
  34   * This class is intended to proxy things like user, group, etc 'classes'
  35   * It will only run the callback to load the object from the DB when necessary.
  36   *
  37   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
  38   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class std_proxy implements proxy_interface {
  41      /**
  42       * @var int $id The ID of the database record.
  43       */
  44      protected $id;
  45  
  46      /**
  47       * @var \stdClass $class The class we are proxying.
  48       */
  49      protected $class;
  50  
  51      /**
  52       * @var callable $callback Callback to run which will load the class to proxy.
  53       */
  54      protected $callback;
  55  
  56      /**
  57       * @var array $callbackargs Array of arguments to pass to the callback.
  58       */
  59      protected $callbackargs;
  60  
  61      /**
  62       * @var \stdClass $base Base class to get members from.
  63       */
  64      protected $base;
  65  
  66  
  67      /**
  68       * Constructor.
  69       *
  70       * @param int       $id       The ID of the record in the database.
  71       * @param callable  $callback Callback to load the class.
  72       * @param \stdClass $base     Class containing base values.
  73       */
  74      public function __construct($id, callable $callback, \stdClass $base = null) {
  75          $this->id = $id;
  76          $this->callbackargs = [$id];
  77          $this->callback = $callback;
  78          $this->base = $base;
  79      }
  80  
  81      public function get($member) {
  82          if ($member === 'id') {
  83              return $this->id;
  84          }
  85  
  86          if ($this->base && property_exists($this->base, $member)) {
  87              return $this->base->{$member};
  88          }
  89  
  90          if (!property_exists($this->get_proxied_instance(), $member)) {
  91              throw new member_does_not_exist_exception(sprintf('Member %s does not exist', $member));
  92          }
  93  
  94          return $this->get_proxied_instance()->{$member};
  95      }
  96  
  97      public function get_proxied_instance() {
  98          $callback = $this->callback;
  99          return $this->class = $this->class ? $this->class : $callback(...$this->callbackargs);
 100      }
 101  }