[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/log/classes/helper/ -> buffered_writer.php (source)

   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   * Helper trait buffered_writer
  19   *
  20   * @package    tool_log
  21   * @copyright  2014 onwards Ankit Agarwal
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_log\helper;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Helper trait buffered_writer. Adds buffer support for the store.
  30   *
  31   * @package    tool_log
  32   * @copyright  2014 onwards Ankit Agarwal
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  trait buffered_writer {
  36  
  37      /** @var array $buffer buffer of events. */
  38      protected $buffer = array();
  39  
  40      /** @var array $buffer buffer size of events. */
  41      protected $buffersize;
  42  
  43      /** @var int $count Counter. */
  44      protected $count = 0;
  45  
  46      /**
  47       * Should the event be ignored (== not logged)?
  48       * @param \core\event\base $event
  49       * @return bool
  50       */
  51      abstract protected function is_event_ignored(\core\event\base $event);
  52  
  53      /**
  54       * Write event in the store with buffering. Method insert_event_entries() must be
  55       * defined.
  56       *
  57       * @param \core\event\base $event
  58       *
  59       * @return void
  60       */
  61      public function write(\core\event\base $event) {
  62          global $PAGE;
  63  
  64          if ($this->is_event_ignored($event)) {
  65              return;
  66          }
  67  
  68          // We need to capture current info at this moment,
  69          // at the same time this lowers memory use because
  70          // snapshots and custom objects may be garbage collected.
  71          $entry = $event->get_data();
  72          $entry['other'] = serialize($entry['other']);
  73          $entry['origin'] = $PAGE->requestorigin;
  74          $entry['ip'] = $PAGE->requestip;
  75          $entry['realuserid'] = \core\session\manager::is_loggedinas() ? $GLOBALS['USER']->realuser : null;
  76  
  77          $this->buffer[] = $entry;
  78          $this->count++;
  79  
  80          if (!isset($this->buffersize)) {
  81              $this->buffersize = $this->get_config('buffersize', 50);
  82          }
  83  
  84          if ($this->count >= $this->buffersize) {
  85              $this->flush();
  86          }
  87      }
  88  
  89      /**
  90       * Flush event buffer.
  91       */
  92      public function flush() {
  93          if ($this->count == 0) {
  94              return;
  95          }
  96          $events = $this->buffer;
  97          $this->count = 0;
  98          $this->buffer = array();
  99          $this->insert_event_entries($events);
 100      }
 101  
 102      /**
 103       * Bulk write a given array of events to the backend. Stores must implement this.
 104       *
 105       * @param array $evententries raw event data
 106       */
 107      abstract protected function insert_event_entries($evententries);
 108  
 109      /**
 110       * Get a config value for the store.
 111       *
 112       * @param string $name Config name
 113       * @param mixed $default default value
 114       *
 115       * @return mixed config value if set, else the default value.
 116       */
 117      abstract protected function get_config($name, $default = null);
 118  
 119      /**
 120       * Push any remaining events to the database. Insert_events() must be
 121       * defined. override in stores if the store doesn't support buffering.
 122       *
 123       */
 124      public function dispose() {
 125          $this->flush();
 126      }
 127  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1