/src/mako/file/FileSystem.php

https://github.com/mako-framework/framework · PHP · 377 lines · 154 code · 34 blank · 189 comment · 2 complexity · 5a58fb42e25a221468a4a10a9f1a57d6 MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Frederic G. Østby
  4. * @license http://www.makoframework.com/license
  5. */
  6. namespace mako\file;
  7. use FilesystemIterator;
  8. use SplFileObject;
  9. use function copy;
  10. use function dirname;
  11. use function disk_free_space;
  12. use function disk_total_space;
  13. use function file_exists;
  14. use function file_get_contents;
  15. use function file_put_contents;
  16. use function filemtime;
  17. use function filesize;
  18. use function getcwd;
  19. use function glob;
  20. use function is_dir;
  21. use function is_file;
  22. use function is_readable;
  23. use function is_writable;
  24. use function mkdir;
  25. use function pathinfo;
  26. use function rename;
  27. use function rmdir;
  28. use function unlink;
  29. /**
  30. * File system.
  31. */
  32. class FileSystem
  33. {
  34. /**
  35. * Returns TRUE if a file exists and FALSE if not.
  36. *
  37. * @param string $file Path to file
  38. * @return bool
  39. */
  40. public function has(string $file): bool
  41. {
  42. return file_exists($file);
  43. }
  44. /**
  45. * Returns TRUE if the provided path is a file and FALSE if not.
  46. *
  47. * @param string $file Path to file
  48. * @return bool
  49. */
  50. public function isFile(string $file): bool
  51. {
  52. return is_file($file);
  53. }
  54. /**
  55. * Returns TRUE if the provided path is a directory and FALSE if not.
  56. *
  57. * @param string $directory Path to directory
  58. * @return bool
  59. */
  60. public function isDirectory(string $directory): bool
  61. {
  62. return is_dir($directory);
  63. }
  64. /**
  65. * Returns TRUE if a file or directory is empty and FALSE if not.
  66. *
  67. * @param string $path Path to directory
  68. * @return bool
  69. */
  70. public function isEmpty(string $path): bool
  71. {
  72. if(is_dir($path))
  73. {
  74. return (new FilesystemIterator($path))->valid() === false;
  75. }
  76. return filesize($path) === 0;
  77. }
  78. /**
  79. * Returns TRUE if the file is readable and FALSE if not.
  80. *
  81. * @param string $file Path to file
  82. * @return bool
  83. */
  84. public function isReadable(string $file): bool
  85. {
  86. return is_readable($file);
  87. }
  88. /**
  89. * Returns TRUE if the file or directory is writable and FALSE if not.
  90. *
  91. * @param string $file Path to file
  92. * @return bool
  93. */
  94. public function isWritable(string $file): bool
  95. {
  96. return is_writable($file);
  97. }
  98. /**
  99. * Returns the time (unix timestamp) the file was last modified.
  100. *
  101. * @param string $file Path to file
  102. * @return int
  103. */
  104. public function lastModified(string $file): int
  105. {
  106. return filemtime($file);
  107. }
  108. /**
  109. * Returns the fize of the file in bytes.
  110. *
  111. * @param string $file Path to file
  112. * @return int
  113. */
  114. public function size(string $file): int
  115. {
  116. return filesize($file);
  117. }
  118. /**
  119. * Returns the extension of the file.
  120. *
  121. * @param string $file Path to file
  122. * @return string
  123. */
  124. public function extension(string $file): string
  125. {
  126. return pathinfo($file, PATHINFO_EXTENSION);
  127. }
  128. /**
  129. * Copies a file.
  130. *
  131. * @param string $source Path to source file
  132. * @param string $destination Path to the destination file
  133. * @return bool
  134. */
  135. public function copy(string $source, string $destination): bool
  136. {
  137. return copy($source, $destination);
  138. }
  139. /**
  140. * Renames a file or directory.
  141. *
  142. * @param string $oldName Old name
  143. * @param string $newName New name
  144. * @return bool
  145. */
  146. public function rename(string $oldName, string $newName): bool
  147. {
  148. return rename($oldName, $newName);
  149. }
  150. /**
  151. * Deletes the file from disk.
  152. *
  153. * @param string $file Path to file
  154. * @return bool
  155. */
  156. public function remove(string $file): bool
  157. {
  158. return unlink($file);
  159. }
  160. /**
  161. * Deletes a directory and its contents from disk.
  162. *
  163. * @param string $path Path to directory
  164. * @return bool
  165. */
  166. public function removeDirectory(string $path): bool
  167. {
  168. foreach(new FilesystemIterator($path) as $item)
  169. {
  170. if($item->isDir())
  171. {
  172. $this->removeDirectory($item->getPathname());
  173. }
  174. else
  175. {
  176. unlink($item->getPathname());
  177. }
  178. }
  179. return rmdir($path);
  180. }
  181. /**
  182. * Returns an array of pathnames matching the provided pattern.
  183. *
  184. * @param string $pattern Patern
  185. * @param int $flags Flags
  186. * @return array|false
  187. */
  188. public function glob(string $pattern, int $flags = 0)
  189. {
  190. return glob($pattern, $flags);
  191. }
  192. /**
  193. * Returns the contents of the file.
  194. *
  195. * @param string $file File path
  196. * @return false|string
  197. */
  198. public function get(string $file)
  199. {
  200. return file_get_contents($file);
  201. }
  202. /**
  203. * Writes the supplied data to a file.
  204. *
  205. * @param string $file File path
  206. * @param mixed $data File data
  207. * @param bool $lock Acquire an exclusive write lock?
  208. * @return false|int
  209. */
  210. public static function put(string $file, mixed $data, bool $lock = false)
  211. {
  212. return file_put_contents($file, $data, $lock ? LOCK_EX : 0);
  213. }
  214. /**
  215. * Prepends the supplied data to a file.
  216. *
  217. * @param string $file File path
  218. * @param mixed $data File data
  219. * @param bool $lock Acquire an exclusive write lock?
  220. * @return false|int
  221. */
  222. public static function prepend(string $file, mixed $data, bool $lock = false)
  223. {
  224. return file_put_contents($file, $data . file_get_contents($file), $lock ? LOCK_EX : 0);
  225. }
  226. /**
  227. * Appends the supplied data to a file.
  228. *
  229. * @param string $file File path
  230. * @param mixed $data File data
  231. * @param bool $lock Acquire an exclusive write lock?
  232. * @return false|int
  233. */
  234. public static function append(string $file, mixed $data, bool $lock = false)
  235. {
  236. return file_put_contents($file, $data, $lock ? FILE_APPEND | LOCK_EX : FILE_APPEND);
  237. }
  238. /**
  239. * Truncates a file.
  240. *
  241. * @param string $file File path
  242. * @param bool $lock Acquire an exclusive write lock?
  243. * @return bool
  244. */
  245. public static function truncate(string $file, bool $lock = false): bool
  246. {
  247. return (0 === file_put_contents($file, null, $lock ? LOCK_EX : 0));
  248. }
  249. /**
  250. * Creates a directory.
  251. *
  252. * @param string $path Path to directory
  253. * @param int $mode Mode
  254. * @param bool $recursive Recursive
  255. * @return bool
  256. */
  257. public function createDirectory(string $path, int $mode = 0777, bool $recursive = false): bool
  258. {
  259. return mkdir($path, $mode, $recursive);
  260. }
  261. /**
  262. * Returns the total size of a filesystem or disk partition in bytes.
  263. *
  264. * @param string|null $directory A directory of the filesystem or disk partition
  265. * @return float
  266. */
  267. public function getDiskSize(?string $directory = null): float
  268. {
  269. return disk_total_space($directory ?? (getcwd() ?: dirname(__FILE__)));
  270. }
  271. /**
  272. * Returns the total number of available bytes on the filesystem or disk partition.
  273. *
  274. * @param string|null $directory A directory of the filesystem or disk partition
  275. * @return float
  276. */
  277. public function getFreeSpaceOnDisk(?string $directory = null): float
  278. {
  279. return disk_free_space($directory ?? (getcwd() ?: dirname(__FILE__)));
  280. }
  281. /**
  282. * Includes a file.
  283. *
  284. * @param string $file Path to file
  285. * @return mixed
  286. */
  287. public function include(string $file): mixed
  288. {
  289. return include $file;
  290. }
  291. /**
  292. * Includes a file it hasn't already been included.
  293. *
  294. * @param string $file Path to file
  295. * @return mixed
  296. */
  297. public function includeOnce(string $file): mixed
  298. {
  299. return include_once $file;
  300. }
  301. /**
  302. * Requires a file.
  303. *
  304. * @param string $file Path to file
  305. * @return mixed
  306. */
  307. public function require(string $file): mixed
  308. {
  309. return require $file;
  310. }
  311. /**
  312. * Requires a file if it hasn't already been required.
  313. *
  314. * @param string $file Path to file
  315. * @return mixed
  316. */
  317. public function requireOnce(string $file): mixed
  318. {
  319. return require_once $file;
  320. }
  321. /**
  322. * Returns a FileInfo object.
  323. *
  324. * @param string $file Path to file
  325. * @return \mako\file\FileInfo
  326. */
  327. public function info(string $file): FileInfo
  328. {
  329. return new FileInfo($file);
  330. }
  331. /**
  332. * Returns a SplFileObject.
  333. *
  334. * @param string $file Path to file
  335. * @param string $openMode Open mode
  336. * @param bool $useIncludePath Use include path?
  337. * @return \SplFileObject
  338. */
  339. public function file(string $file, string $openMode = 'r', bool $useIncludePath = false)
  340. {
  341. return new SplFileObject($file, $openMode, $useIncludePath);
  342. }
  343. }