Explicitly initialize anonModule to null.
[htmlpurifier.git] / extras / FSTools / File.php
blob1c76705d65a31fd1e86dfea49414dd843a808c8d
1 <?php
3 /**
4 * Represents a file in the filesystem
6 * @warning Be sure to distinguish between get() and write() versus
7 * read() and put(), the former operates on the entire file, while
8 * the latter operates on a handle.
9 */
10 class FSTools_File
13 /** Filename of file this object represents */
14 protected $name;
16 /** Handle for the file */
17 protected $handle = false;
19 /** Instance of FSTools for interfacing with filesystem */
20 protected $fs;
22 /**
23 * Filename of file you wish to instantiate.
24 * @note This file need not exist
26 public function __construct($name, $fs = false) {
27 $this->name = $name;
28 $this->fs = $fs ? $fs : FSTools::singleton();
31 /** Returns the filename of the file. */
32 public function getName() {return $this->name;}
34 /** Returns directory of the file without trailing slash */
35 public function getDirectory() {return $this->fs->dirname($this->name);}
37 /**
38 * Retrieves the contents of a file
39 * @todo Throw an exception if file doesn't exist
41 public function get() {
42 return $this->fs->file_get_contents($this->name);
45 /** Writes contents to a file, creates new file if necessary */
46 public function write($contents) {
47 return $this->fs->file_put_contents($this->name, $contents);
50 /** Deletes the file */
51 public function delete() {
52 return $this->fs->unlink($this->name);
55 /** Returns true if file exists and is a file. */
56 public function exists() {
57 return $this->fs->is_file($this->name);
60 /** Returns last file modification time */
61 public function getMTime() {
62 return $this->fs->filemtime($this->name);
65 /**
66 * Chmod a file
67 * @note We ignore errors because of some weird owner trickery due
68 * to SVN duality
70 public function chmod($octal_code) {
71 return @$this->fs->chmod($this->name, $octal_code);
74 /** Opens file's handle */
75 public function open($mode) {
76 if ($this->handle) $this->close();
77 $this->handle = $this->fs->fopen($this->name, $mode);
78 return true;
81 /** Closes file's handle */
82 public function close() {
83 if (!$this->handle) return false;
84 $status = $this->fs->fclose($this->handle);
85 $this->handle = false;
86 return $status;
89 /** Retrieves a line from an open file, with optional max length $length */
90 public function getLine($length = null) {
91 if (!$this->handle) $this->open('r');
92 if ($length === null) return $this->fs->fgets($this->handle);
93 else return $this->fs->fgets($this->handle, $length);
96 /** Retrieves a character from an open file */
97 public function getChar() {
98 if (!$this->handle) $this->open('r');
99 return $this->fs->fgetc($this->handle);
102 /** Retrieves an $length bytes of data from an open data */
103 public function read($length) {
104 if (!$this->handle) $this->open('r');
105 return $this->fs->fread($this->handle, $length);
108 /** Writes to an open file */
109 public function put($string) {
110 if (!$this->handle) $this->open('a');
111 return $this->fs->fwrite($this->handle, $string);
114 /** Returns TRUE if the end of the file has been reached */
115 public function eof() {
116 if (!$this->handle) return true;
117 return $this->fs->feof($this->handle);
120 public function __destruct() {
121 if ($this->handle) $this->close();
126 // vim: et sw=4 sts=4