FSTools.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Filesystem tools not provided by default; can recursively create, copy
  4. * and delete folders. Some template methods are provided for extensibility.
  5. *
  6. * @note This class must be instantiated to be used, although it does
  7. * not maintain state.
  8. */
  9. class FSTools
  10. {
  11. private static $singleton;
  12. /**
  13. * Returns a global instance of FSTools
  14. */
  15. static public function singleton() {
  16. if (empty(FSTools::$singleton)) FSTools::$singleton = new FSTools();
  17. return FSTools::$singleton;
  18. }
  19. /**
  20. * Sets our global singleton to something else; useful for overloading
  21. * functions.
  22. */
  23. static public function setSingleton($singleton) {
  24. FSTools::$singleton = $singleton;
  25. }
  26. /**
  27. * Recursively creates a directory
  28. * @param string $folder Name of folder to create
  29. * @note Adapted from the PHP manual comment 76612
  30. */
  31. public function mkdirr($folder) {
  32. $folders = preg_split("#[\\\\/]#", $folder);
  33. $base = '';
  34. for($i = 0, $c = count($folders); $i < $c; $i++) {
  35. if(empty($folders[$i])) {
  36. if (!$i) {
  37. // special case for root level
  38. $base .= DIRECTORY_SEPARATOR;
  39. }
  40. continue;
  41. }
  42. $base .= $folders[$i];
  43. if(!is_dir($base)){
  44. $this->mkdir($base);
  45. }
  46. $base .= DIRECTORY_SEPARATOR;
  47. }
  48. }
  49. /**
  50. * Copy a file, or recursively copy a folder and its contents; modified
  51. * so that copied files, if PHP, have includes removed
  52. * @note Adapted from http://aidanlister.com/repos/v/function.copyr.php
  53. */
  54. public function copyr($source, $dest) {
  55. // Simple copy for a file
  56. if (is_file($source)) {
  57. return $this->copy($source, $dest);
  58. }
  59. // Make destination directory
  60. if (!is_dir($dest)) {
  61. $this->mkdir($dest);
  62. }
  63. // Loop through the folder
  64. $dir = $this->dir($source);
  65. while ( false !== ($entry = $dir->read()) ) {
  66. // Skip pointers
  67. if ($entry == '.' || $entry == '..') {
  68. continue;
  69. }
  70. if (!$this->copyable($entry)) {
  71. continue;
  72. }
  73. // Deep copy directories
  74. if ($dest !== "$source/$entry") {
  75. $this->copyr("$source/$entry", "$dest/$entry");
  76. }
  77. }
  78. // Clean up
  79. $dir->close();
  80. return true;
  81. }
  82. /**
  83. * Overloadable function that tests a filename for copyability. By
  84. * default, everything should be copied; you can restrict things to
  85. * ignore hidden files, unreadable files, etc. This function
  86. * applies to copyr().
  87. */
  88. public function copyable($file) {
  89. return true;
  90. }
  91. /**
  92. * Delete a file, or a folder and its contents
  93. * @note Adapted from http://aidanlister.com/repos/v/function.rmdirr.php
  94. */
  95. public function rmdirr($dirname)
  96. {
  97. // Sanity check
  98. if (!$this->file_exists($dirname)) {
  99. return false;
  100. }
  101. // Simple delete for a file
  102. if ($this->is_file($dirname) || $this->is_link($dirname)) {
  103. return $this->unlink($dirname);
  104. }
  105. // Loop through the folder
  106. $dir = $this->dir($dirname);
  107. while (false !== $entry = $dir->read()) {
  108. // Skip pointers
  109. if ($entry == '.' || $entry == '..') {
  110. continue;
  111. }
  112. // Recurse
  113. $this->rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
  114. }
  115. // Clean up
  116. $dir->close();
  117. return $this->rmdir($dirname);
  118. }
  119. /**
  120. * Recursively globs a directory.
  121. */
  122. public function globr($dir, $pattern, $flags = NULL) {
  123. $files = $this->glob("$dir/$pattern", $flags);
  124. if ($files === false) $files = array();
  125. $sub_dirs = $this->glob("$dir/*", GLOB_ONLYDIR);
  126. if ($sub_dirs === false) $sub_dirs = array();
  127. foreach ($sub_dirs as $sub_dir) {
  128. $sub_files = $this->globr($sub_dir, $pattern, $flags);
  129. $files = array_merge($files, $sub_files);
  130. }
  131. return $files;
  132. }
  133. /**
  134. * Allows for PHP functions to be called and be stubbed.
  135. * @warning This function will not work for functions that need
  136. * to pass references; manually define a stub function for those.
  137. */
  138. public function __call($name, $args) {
  139. return call_user_func_array($name, $args);
  140. }
  141. }
  142. // vim: et sw=4 sts=4