PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/kirby/toolkit/lib/dir.php

https://gitlab.com/gricelya/rental
PHP | 209 lines | 59 code | 24 blank | 126 comment | 8 complexity | ad388e4c28391aeba4454c60391fc85a MD5 | raw file
  1. <?php
  2. /**
  3. * Dir
  4. *
  5. * Low level directory handling utilities
  6. *
  7. * @package Kirby Toolkit
  8. * @author Bastian Allgeier <bastian@getkirby.com>
  9. * @link http://getkirby.com
  10. * @copyright Bastian Allgeier
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  12. */
  13. class Dir {
  14. public static $defaults = array(
  15. 'permissions' => 0755,
  16. 'ignore' => array('.', '..', '.DS_Store', '.gitignore', '.git', '.svn', '.htaccess', 'Thumb.db', '@eaDir')
  17. );
  18. /**
  19. * Creates a new directory
  20. *
  21. * <code>
  22. *
  23. * $create = dir::make('/app/test/new-directory');
  24. *
  25. * if($create) echo 'the directory has been created';
  26. *
  27. * </code>
  28. *
  29. * @param string $dir The path for the new directory
  30. * @return boolean True: the dir has been created, false: creating failed
  31. */
  32. public static function make($dir, $recursive = true) {
  33. return is_dir($dir) ? true : @mkdir($dir, static::$defaults['permissions'], $recursive);
  34. }
  35. /**
  36. * Reads all files from a directory and returns them as an array.
  37. * It skips unwanted invisible stuff.
  38. *
  39. * <code>
  40. *
  41. * $files = dir::read('mydirectory');
  42. * // returns array('file-1.txt', 'file-2.txt', 'file-3.txt', etc...);
  43. *
  44. * </code>
  45. *
  46. * @param string $dir The path of directory
  47. * @param array $ignore Optional array with filenames, which should be ignored
  48. * @return mixed An array of filenames or false
  49. */
  50. public static function read($dir, $ignore = array()) {
  51. if(!is_dir($dir)) return array();
  52. $skip = array_merge(static::$defaults['ignore'], $ignore);
  53. return (array)array_diff(scandir($dir),$skip);
  54. }
  55. /**
  56. * Moves a directory to a new location
  57. *
  58. * <code>
  59. *
  60. * $move = dir::move('mydirectory', 'mynewdirectory');
  61. *
  62. * if($move) echo 'the directory has been moved to mynewdirectory';
  63. *
  64. * </code>
  65. *
  66. * @param string $old The current path of the directory
  67. * @param string $new The desired path where the dir should be moved to
  68. * @return boolean True: the directory has been moved, false: moving failed
  69. */
  70. public static function move($old, $new) {
  71. if(!is_dir($old)) return false;
  72. return @rename($old, $new);
  73. }
  74. /**
  75. * Deletes a directory
  76. *
  77. * <code>
  78. *
  79. * $remove = dir::remove('mydirectory');
  80. *
  81. * if($remove) echo 'the directory has been removed';
  82. *
  83. * </code>
  84. *
  85. * @param string $dir The path of the directory
  86. * @param boolean $keep If set to true, the directory will flushed but not removed.
  87. * @return boolean True: the directory has been removed, false: removing failed
  88. */
  89. public static function remove($dir, $keep = false) {
  90. if(!is_dir($dir)) return false;
  91. // It's easier to handle this with the Folder class
  92. $object = new Folder($dir);
  93. return $object->remove($keep);
  94. }
  95. /**
  96. * Flushes a directory
  97. *
  98. * @param string $dir The path of the directory
  99. * @return boolean True: the directory has been flushed, false: flushing failed
  100. */
  101. public static function clean($dir) {
  102. return static::remove($dir, true);
  103. }
  104. /**
  105. * Gets the size of the directory and all subfolders and files
  106. *
  107. * @param string $dir The path of the directory
  108. * @return mixed
  109. */
  110. public static function size($dir) {
  111. if(!file_exists($dir)) return false;
  112. // It's easier to handle this with the Folder class
  113. $object = new Folder($dir);
  114. return $object->size();
  115. }
  116. /**
  117. * Returns a nicely formatted size of all the contents of the folder
  118. *
  119. * @param string $dir The path of the directory
  120. * @return mixed
  121. */
  122. public static function niceSize($dir) {
  123. return f::niceSize(static::size($dir));
  124. }
  125. /**
  126. * Recursively check when the dir and all
  127. * subfolders have been modified for the last time.
  128. *
  129. * @param string $dir The path of the directory
  130. * @param string $format
  131. * @return int
  132. */
  133. public static function modified($dir, $format = null, $handler = 'date') {
  134. // It's easier to handle this with the Folder class
  135. $object = new Folder($dir);
  136. return $object->modified($format, $handler);
  137. }
  138. /**
  139. * Checks if the directory or any subdirectory has been
  140. * modified after the given timestamp
  141. *
  142. * @param string $dir
  143. * @param int $time
  144. * @return boolean
  145. */
  146. public static function wasModifiedAfter($dir, $time) {
  147. if(filemtime($dir) > $time) return true;
  148. $content = dir::read($dir);
  149. foreach($content as $item) {
  150. $subdir = $dir . DS . $item;
  151. if(filemtime($subdir) > $time) return true;
  152. if(is_dir($subdir) && dir::wasModifiedAfter($subdir, $time)) return true;
  153. }
  154. return false;
  155. }
  156. /**
  157. * Checks if the dir is writable
  158. *
  159. * @param string $dir
  160. * @return boolean
  161. */
  162. public static function writable($dir) {
  163. return is_writable($dir);
  164. }
  165. /**
  166. * Checks if the dir is readable
  167. *
  168. * @param string $dir
  169. * @return boolean
  170. */
  171. public static function readable($dir) {
  172. return is_readable($dir);
  173. }
  174. /**
  175. * Copy a file, or recursively copy a folder and its contents
  176. *
  177. * @param string $dir Source path
  178. * @param string $to Destination path
  179. */
  180. public static function copy($dir, $to) {
  181. // It's easier to handle this with the Folder class
  182. $object = new Folder($dir);
  183. return $object->copy($to);
  184. }
  185. }