File.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Elgg\Filesystem;
  3. /**
  4. * Represents a file that may or may not actually exist.
  5. *
  6. * @since 1.10.0
  7. *
  8. * @access private
  9. */
  10. class File {
  11. /** @var Directory */
  12. private $directory;
  13. /** @var string */
  14. private $path;
  15. /**
  16. * Constructor
  17. *
  18. * @param Directory $directory The directory where this file resides
  19. * @param string $path The path to this file relative to the directory
  20. */
  21. public function __construct(Directory $directory, $path) {
  22. $this->directory = $directory;
  23. $this->path = $path;
  24. }
  25. /**
  26. * @return boolean Whether this file exists.
  27. */
  28. public function exists() {
  29. return $this->directory->isFile($this->path);
  30. }
  31. /**
  32. * @return string The file's basename.
  33. */
  34. public function getBasename() {
  35. return pathinfo($this->path, PATHINFO_BASENAME);
  36. }
  37. /**
  38. * Get the text content of this file. Empty string if it doesn't exist.
  39. *
  40. * @return string
  41. */
  42. public function getContents() {
  43. return $this->directory->getContents($this->path);
  44. }
  45. /**
  46. * @return string The file's extension.
  47. */
  48. public function getExtension() {
  49. return pathinfo($this->path, PATHINFO_EXTENSION);
  50. }
  51. /**
  52. * Do a PHP include of the file and return the result.
  53. *
  54. * TODO(ewinslow): This may only work for local filesystems?
  55. *
  56. * @return mixed
  57. */
  58. public function includeFile() {
  59. return $this->directory->includeFile($this->path);
  60. }
  61. /** @inheritDoc */
  62. public function __toString() {
  63. return $this->path;
  64. }
  65. }