|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00032 class file_xml_output extends xml_output { 00033 00034 protected $fullpath; // Full path to OS file where contents will be stored 00035 protected $fhandle; // File handle where all write operations happen 00036 00037 public function __construct($fullpath, $usebuffer = true) { 00038 $this->fullpath = $fullpath; 00039 parent::__construct($usebuffer); 00040 } 00041 00042 // Private API starts here 00043 00044 protected function init() { 00045 if (!file_exists(dirname($this->fullpath))) { 00046 throw new xml_output_exception('directory_not_exists', dirname($this->fullpath)); 00047 } 00048 if (file_exists($this->fullpath)) { 00049 throw new xml_output_exception('file_already_exists', $this->fullpath); 00050 } 00051 if (!is_writable(dirname($this->fullpath))) { 00052 throw new xml_output_exception('directory_not_writable', dirname($this->fullpath)); 00053 } 00054 // Open the OS file for writing 00055 if (! $this->fhandle = fopen($this->fullpath, 'w')) { 00056 throw new xml_output_exception('error_opening_file'); 00057 } 00058 } 00059 00060 protected function finish() { 00061 if (false === fclose($this->fhandle)) { 00062 throw new xml_output_exception('error_closing_file'); 00063 } 00064 } 00065 00066 protected function send($content) { 00067 if (false === fwrite($this->fhandle, $content)) { 00068 throw new xml_output_exception('error_writing_file'); 00069 } 00070 } 00071 }