Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package    moodlecore
  20   * @subpackage backup-xml
  21   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * This class implements one @xml_output able to send contents to one OS file
  27   *
  28   * Buffering enabled by default (can be disabled)
  29   *
  30   * TODO: Finish phpdocs
  31   */
  32  class file_xml_output extends xml_output {
  33  
  34      protected $fullpath; // Full path to OS file where contents will be stored
  35      protected $fhandle;  // File handle where all write operations happen
  36  
  37      public function __construct($fullpath, $usebuffer = true) {
  38          $this->fullpath = $fullpath;
  39          parent::__construct($usebuffer);
  40      }
  41  
  42  // Private API starts here
  43  
  44      protected function init() {
  45          if (!file_exists(dirname($this->fullpath))) {
  46              throw new xml_output_exception('directory_not_exists', dirname($this->fullpath));
  47          }
  48          if (file_exists($this->fullpath)) {
  49              throw new xml_output_exception('file_already_exists', $this->fullpath);
  50          }
  51          if (!is_writable(dirname($this->fullpath))) {
  52              throw new xml_output_exception('directory_not_writable', dirname($this->fullpath));
  53          }
  54          // Open the OS file for writing
  55          if (! $this->fhandle = fopen($this->fullpath, 'w')) {
  56              throw new xml_output_exception('error_opening_file');
  57          }
  58      }
  59  
  60      protected function finish() {
  61          if (false === fclose($this->fhandle)) {
  62              throw new xml_output_exception('error_closing_file');
  63          }
  64      }
  65  
  66      protected function send($content) {
  67          if (false === fwrite($this->fhandle, $content)) {
  68              throw new xml_output_exception('error_writing_file');
  69          }
  70      }
  71  }