File.php 3.5 KB

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