3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
26 * This class implements one @xml_output able to send contents to one OS file
28 * Buffering enabled by default (can be disabled)
30 * TODO: Finish phpdocs
32 class file_xml_output extends xml_output {
34 protected $fullpath; // Full path to OS file where contents will be stored
35 protected $fhandle; // File handle where all write operations happen
37 public function __construct($fullpath, $usebuffer = true) {
38 $this->fullpath = $fullpath;
39 parent::__construct($usebuffer);
42 // Private API starts here
44 protected function init() {
45 if (!file_exists(dirname($this->fullpath))) {
46 throw new xml_output_exception('directory_not_exists', dirname($this->fullpath));
48 if (file_exists($this->fullpath)) {
49 throw new xml_output_exception('file_already_exists', $this->fullpath);
51 if (!is_writable(dirname($this->fullpath))) {
52 throw new xml_output_exception('directory_not_writable', dirname($this->fullpath));
54 // Open the OS file for writing
55 if (! $this->fhandle = fopen($this->fullpath, 'w')) {
56 throw new xml_output_exception('error_opening_file');
60 protected function finish() {
61 if (false === fclose($this->fhandle)) {
62 throw new xml_output_exception('error_closing_file');
66 protected function send($content) {
67 if (false === fwrite($this->fhandle, $content)) {
68 throw new xml_output_exception('error_writing_file');