Path.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace Path\Path;
  3. class Path
  4. {
  5. protected string $path;
  6. public function __construct(string $path)
  7. {
  8. $this->path = $path;
  9. }
  10. /**
  11. * Resolve the path.
  12. *
  13. * @return bool|string Returns the resolved path as a string if successful, otherwise returns false.
  14. */
  15. public function resolve(): bool|string
  16. {
  17. return realpath($this->path);
  18. }
  19. /**
  20. * Check if the path refers to a regular file.
  21. *
  22. * @return bool Returns true if the path refers to a regular file, otherwise returns false.
  23. */
  24. public function isFile(): bool
  25. {
  26. return is_file($this->path);
  27. }
  28. /**
  29. * Check if the given path is a directory.
  30. *
  31. * @return bool Returns true if the path is a directory, false otherwise.
  32. */
  33. public function isDir()
  34. {
  35. return is_dir($this->path);
  36. }
  37. /**
  38. * Get the extension of the given path.
  39. *
  40. * @return array|string Returns the extension of the path as a string if it exists, or an empty string otherwise.
  41. */
  42. public function extension(): array|string
  43. {
  44. return pathinfo($this->path, PATHINFO_EXTENSION);
  45. }
  46. /**
  47. * Get the name of the file or path.
  48. *
  49. * @return array|string Returns the name of the file or path.
  50. * If the path has an extension, it returns the name without the extension as a string.
  51. * If the path doesn't have an extension, it returns the name as an array containing the directory name and the file name.
  52. */
  53. public function name(): array|string
  54. {
  55. return pathinfo($this->path, PATHINFO_FILENAME);
  56. }
  57. /**
  58. * Get the base name of the path.
  59. *
  60. * @return string The base name of the path.
  61. */
  62. public function basename(): string
  63. {
  64. return pathinfo($this->path, PATHINFO_BASENAME);
  65. }
  66. /**
  67. * Creates a new directory.
  68. *
  69. * @param int $mode (optional) The permissions for the new directory. Default is 0777.
  70. * @param bool $recursive (optional) Indicates whether to create parent directories if they do not exist. Default is false.
  71. *
  72. * @return void
  73. */
  74. public function mkdir($mode = 0777, $recursive = false): void
  75. {
  76. if (!file_exists($this->path)) {
  77. mkdir($this->path, $mode, $recursive);
  78. }
  79. }
  80. /**
  81. * Deletes a file or a directory.
  82. *
  83. * @return void
  84. */
  85. public function delete(): void
  86. {
  87. if (is_file($this->path)) {
  88. unlink($this->path); //for file
  89. }
  90. else if (is_dir($this->path)) {
  91. rmdir($this->path); //for directory
  92. }
  93. }
  94. /**
  95. * Copies a file to a specified destination.
  96. *
  97. * @param string $destination The path to the destination file or directory to copy to.
  98. *
  99. * @return void
  100. */
  101. public function copy($destination): void
  102. {
  103. if (is_file($this->path)) {
  104. copy($this->path, $destination);
  105. }
  106. else if (is_dir($this->path)) {
  107. // Copy dir needs special handling, not covered in this example.
  108. }
  109. }
  110. /**
  111. * Moves a file or directory to a new location.
  112. *
  113. * @param string $destination The new location where the file or directory should be moved to.
  114. *
  115. * @return void
  116. */
  117. public function move($destination): void
  118. {
  119. rename($this->path, $destination);
  120. }
  121. /**
  122. * Updates the access and modification time of a file or creates a new empty file if it doesn't exist.
  123. *
  124. * @param int|null $time (optional) The access and modification time to set. Default is the current time.
  125. * @param int|null $atime (optional) The access time to set. Default is the value of $time.
  126. *
  127. * @return void
  128. */
  129. public function touch($time = null, $atime = null): void
  130. {
  131. if (!file_exists($this->path)) {
  132. touch($this->path, $time, $atime);
  133. }
  134. }
  135. /**
  136. * Returns the last modified timestamp of a file or directory.
  137. *
  138. * @return int|bool The last modified timestamp, or false if an error occurred.
  139. */
  140. public function lastModified(): bool|int
  141. {
  142. return filemtime($this->path);
  143. }
  144. /**
  145. * Calculates the size of a file.
  146. *
  147. * @return bool|int The size of the file in bytes. Returns false if the file does not exist or on failure.
  148. */
  149. public function size(): bool|int
  150. {
  151. return filesize($this->path);
  152. }
  153. /**
  154. * Retrieves the parent directory of a file or directory path.
  155. *
  156. * @return string The parent directory of the specified path.
  157. */
  158. public function parent(): string
  159. {
  160. return dirname($this->path);
  161. }
  162. /**
  163. * Retrieves the contents of a file.
  164. *
  165. * @return bool|string The contents of the file as a string. Returns false if the file does not exist or on failure.
  166. */
  167. public function getContents(): bool|string
  168. {
  169. return file_get_contents($this->path);
  170. }
  171. /**
  172. * Writes contents to a file.
  173. *
  174. * @param mixed $contents The contents to be written to the file.
  175. * @return void
  176. */
  177. public function putContents($contents): void
  178. {
  179. file_put_contents($this->path, $contents);
  180. }
  181. /**
  182. * Appends contents to a file.
  183. *
  184. * @param string $contents The contents to append to the file.
  185. *
  186. * @return void
  187. */
  188. public function appendContents($contents): void
  189. {
  190. file_put_contents($this->path, $contents, FILE_APPEND);
  191. }
  192. /**
  193. * Retrieves the permissions of a file or directory.
  194. *
  195. * @return string The permissions of the file or directory in octal notation. Returns an empty string if the file or directory does not exist.
  196. */
  197. public function getPermissions(): string
  198. {
  199. return substr(sprintf('%o', fileperms($this->path)), -4);
  200. }
  201. /**
  202. * Changes the permissions of a file or directory.
  203. *
  204. * @param int $permissions The new permissions to set. The value should be an octal number.
  205. * @return bool Returns true on success, false on failure.
  206. */
  207. public function changePermissions($permissions): bool
  208. {
  209. return chmod($this->path, $permissions);
  210. }
  211. /**
  212. * Checks if a file exists.
  213. *
  214. * @return bool Returns true if the file exists, false otherwise.
  215. */
  216. public function exists(): bool
  217. {
  218. return file_exists($this->path);
  219. }
  220. }