<?php
* Represents a file in the filesystem
*
* @warning Be sure to distinguish between get() and write() versus
* read() and put(), the former operates on the entire file, while
* the latter operates on a handle.
*/
class FSTools_File
{
protected $name;
protected $handle = false;
protected $fs;
* Filename of file you wish to instantiate.
* @note This file need not exist
*/
public function __construct($name, $fs = false)
{
$this->name = $name;
$this->fs = $fs ? $fs : FSTools::singleton();
}
public function getName() {return $this->name;}
public function getDirectory() {return $this->fs->dirname($this->name);}
* Retrieves the contents of a file
* @todo Throw an exception if file doesn't exist
*/
public function get()
{
return $this->fs->file_get_contents($this->name);
}
public function write($contents)
{
return $this->fs->file_put_contents($this->name, $contents);
}
public function delete()
{
return $this->fs->unlink($this->name);
}
public function exists()
{
return $this->fs->is_file($this->name);
}
public function getMTime()
{
return $this->fs->filemtime($this->name);
}
* Chmod a file
* @note We ignore errors because of some weird owner trickery due
* to SVN duality
*/
public function chmod($octal_code)
{
return @$this->fs->chmod($this->name, $octal_code);
}
public function open($mode)
{
if ($this->handle) $this->close();
$this->handle = $this->fs->fopen($this->name, $mode);
return true;
}
public function close()
{
if (!$this->handle) return false;
$status = $this->fs->fclose($this->handle);
$this->handle = false;
return $status;
}
public function getLine($length = null)
{
if (!$this->handle) $this->open('r');
if ($length === null) return $this->fs->fgets($this->handle);
else return $this->fs->fgets($this->handle, $length);
}
public function getChar()
{
if (!$this->handle) $this->open('r');
return $this->fs->fgetc($this->handle);
}
public function read($length)
{
if (!$this->handle) $this->open('r');
return $this->fs->fread($this->handle, $length);
}
public function put($string)
{
if (!$this->handle) $this->open('a');
return $this->fs->fwrite($this->handle, $string);
}
public function eof()
{
if (!$this->handle) return true;
return $this->fs->feof($this->handle);
}
public function __destruct()
{
if ($this->handle) $this->close();
}
}