PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/kirby/toolkit/lib/dir.php

https://github.com/aurer/aurer-kirby
PHP | 210 lines | 59 code | 24 blank | 127 comment | 7 complexity | 5c9a155ad1af1cc63736e9031c41037d 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. static public $defaults = array(
  15. 'permissions' => 0755,
  16. 'ignore' => array('.', '..', '.DS_Store', '.gitignore', '.git', '.svn', '.htaccess')
  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. static public 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. static public 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. static public 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. static public 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. static public 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. static public 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. * @param boolean $recursive
  121. * @return mixed
  122. */
  123. static public function niceSize($dir) {
  124. return f::niceSize(static::size($dir));
  125. }
  126. /**
  127. * Recursively check when the dir and all
  128. * subfolders have been modified for the last time.
  129. *
  130. * @param string $dir The path of the directory
  131. * @param string $format
  132. * @return int
  133. */
  134. static public function modified($dir, $format = null) {
  135. // It's easier to handle this with the Folder class
  136. $object = new Folder($dir);
  137. return $object->modified($format);
  138. }
  139. /**
  140. * Checks if the directory or any subdirectory has been
  141. * modified after the given timestamp
  142. *
  143. * @param string $dir
  144. * @param int $time
  145. * @return boolean
  146. */
  147. static public function wasModifiedAfter($dir, $time) {
  148. if(filemtime($dir) > $time) return true;
  149. $content = dir::read($dir);
  150. foreach($content as $item) {
  151. $subdir = $dir . DS . $item;
  152. if(filemtime($subdir) > $time) return true;
  153. if(is_dir($subdir) and dir::wasModifiedAfter($subdir, $time)) return true;
  154. }
  155. return false;
  156. }
  157. /**
  158. * Checks if the dir is writable
  159. *
  160. * @param string $dir
  161. * @return boolean
  162. */
  163. static public function writable($dir) {
  164. return is_writable($dir);
  165. }
  166. /**
  167. * Checks if the dir is readable
  168. *
  169. * @param string $dir
  170. * @return boolean
  171. */
  172. static public function readable($dir) {
  173. return is_readable($dir);
  174. }
  175. /**
  176. * Copy a file, or recursively copy a folder and its contents
  177. *
  178. * @param string $dir Source path
  179. * @param string $to Destination path
  180. */
  181. static function copy($dir, $to) {
  182. // It's easier to handle this with the Folder class
  183. $object = new Folder($dir);
  184. return $object->copy($to);
  185. }
  186. }