mock-fs.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. class WP_Filesystem_MockFS extends WP_Filesystem_Base {
  3. private $cwd;
  4. // Holds a array of objects which contain an array of objects, etc.
  5. private $fs = null;
  6. // Holds a array of /path/to/file.php and /path/to/dir/ map to an object in $fs above.
  7. // A fast, more efficient way of determining if a path exists, and access to that node.
  8. private $fs_map = array();
  9. public $verbose = false; // Enable to debug WP_Filesystem_Base::find_folder() / etc.
  10. public $errors = array();
  11. public $method = 'MockFS';
  12. function __construct() {}
  13. function connect() {
  14. return true;
  15. }
  16. // Copy of core's function, but accepts a path.
  17. function abspath( $path = false ) {
  18. if ( ! $path ) {
  19. $path = ABSPATH;
  20. }
  21. $folder = $this->find_folder( $path );
  22. // Perhaps the FTP folder is rooted at the WordPress installation.
  23. // Check for wp-includes folder in root, could have some false positives, but rare.
  24. if ( ! $folder && $this->is_dir( '/wp-includes' ) ) {
  25. $folder = '/';
  26. }
  27. return $folder;
  28. }
  29. // Mock FS-specific functions:
  30. /**
  31. * Sets initial filesystem environment and/or clears the current environment.
  32. * Can also be passed the initial filesystem to be setup which is passed to self::setfs()
  33. */
  34. function init( $paths = '', $home_dir = '/' ) {
  35. $this->fs = new MockFS_Directory_Node( '/' );
  36. $this->fs_map = array(
  37. '/' => $this->fs,
  38. );
  39. $this->cache = array(); // Used by find_folder() and friends.
  40. $this->cwd = isset( $this->fs_map[ $home_dir ] ) ? $this->fs_map[ $home_dir ] : '/';
  41. $this->setfs( $paths );
  42. }
  43. /**
  44. * "Bulk Loads" a filesystem into the internal virtual filesystem
  45. */
  46. function setfs( $paths ) {
  47. if ( ! is_array( $paths ) ) {
  48. $paths = explode( "\n", $paths );
  49. }
  50. $paths = array_filter( array_map( 'trim', $paths ) );
  51. foreach ( $paths as $path ) {
  52. // Allow for comments.
  53. if ( '#' === $path[0] ) {
  54. continue;
  55. }
  56. // Directories.
  57. if ( '/' === $path[ strlen( $path ) - 1 ] ) {
  58. $this->mkdir( $path );
  59. } else { // Files (with dummy content for now).
  60. $this->put_contents( $path, 'This is a test file' );
  61. }
  62. }
  63. }
  64. /**
  65. * Locates a filesystem "node"
  66. */
  67. private function locate_node( $path ) {
  68. return isset( $this->fs_map[ $path ] ) ? $this->fs_map[ $path ] : false;
  69. }
  70. /**
  71. * Locates a filesystem node for the parent of the given item
  72. */
  73. private function locate_parent_node( $path ) {
  74. $dirname = str_replace( '\\', '/', dirname( $path ) );
  75. return $this->locate_node( trailingslashit( $dirname ) );
  76. }
  77. // Here starteth the WP_Filesystem functions.
  78. function mkdir( $path, /* Optional args are ignored */ $chmod = false, $chown = false, $chgrp = false ) {
  79. $path = trailingslashit( $path );
  80. $parent_node = $this->locate_parent_node( $path );
  81. if ( ! $parent_node ) {
  82. $dirname = str_replace( '\\', '/', dirname( $path ) );
  83. $this->mkdir( $dirname );
  84. $parent_node = $this->locate_parent_node( $path );
  85. if ( ! $parent_node ) {
  86. return false;
  87. }
  88. }
  89. $node = new MockFS_Directory_Node( $path );
  90. $parent_node->children[ $node->name ] = $node;
  91. $this->fs_map[ $path ] = $node;
  92. return true;
  93. }
  94. function put_contents( $path, $contents = '', $mode = null ) {
  95. if ( ! $this->is_dir( dirname( $path ) ) ) {
  96. $this->mkdir( dirname( $path ) );
  97. }
  98. $parent = $this->locate_parent_node( $path );
  99. $new_file = new MockFS_File_Node( $path, $contents );
  100. $parent->children[ $new_file->name ] = $new_file;
  101. $this->fs_map[ $path ] = $new_file;
  102. }
  103. function get_contents( $file ) {
  104. if ( ! $this->is_file( $file ) ) {
  105. return false;
  106. }
  107. return $this->fs_map[ $file ]->contents;
  108. }
  109. function cwd() {
  110. return $this->cwd->path;
  111. }
  112. function chdir( $path ) {
  113. if ( ! isset( $this->fs_map[ $path ] ) ) {
  114. return false;
  115. }
  116. $this->cwd = $this->fs_map[ $path ];
  117. return true;
  118. }
  119. function exists( $path ) {
  120. return isset( $this->fs_map[ $path ] ) || isset( $this->fs_map[ trailingslashit( $path ) ] );
  121. }
  122. function is_file( $file ) {
  123. return isset( $this->fs_map[ $file ] ) && $this->fs_map[ $file ]->is_file();
  124. }
  125. function is_dir( $path ) {
  126. $path = trailingslashit( $path );
  127. return isset( $this->fs_map[ $path ] ) && $this->fs_map[ $path ]->is_dir();
  128. }
  129. function dirlist( $path = '.', $include_hidden = true, $recursive = false ) {
  130. if ( empty( $path ) || '.' === $path ) {
  131. $path = $this->cwd();
  132. }
  133. if ( ! $this->exists( $path ) ) {
  134. return false;
  135. }
  136. $limit_file = false;
  137. if ( $this->is_file( $path ) ) {
  138. $limit_file = $this->locate_node( $path )->name;
  139. $path = dirname( $path ) . '/';
  140. }
  141. $ret = array();
  142. foreach ( $this->fs_map[ $path ]->children as $entry ) {
  143. if ( '.' === $entry->name || '..' === $entry->name ) {
  144. continue;
  145. }
  146. if ( ! $include_hidden && '.' === $entry->name ) {
  147. continue;
  148. }
  149. if ( $limit_file && $entry->name !== $limit_file ) {
  150. continue;
  151. }
  152. $struc = array();
  153. $struc['name'] = $entry->name;
  154. $struc['type'] = $entry->type;
  155. if ( 'd' === $struc['type'] ) {
  156. if ( $recursive ) {
  157. $struc['files'] = $this->dirlist( trailingslashit( $path ) . trailingslashit( $struc['name'] ), $include_hidden, $recursive );
  158. } else {
  159. $struc['files'] = array();
  160. }
  161. }
  162. $ret[ $entry->name ] = $struc;
  163. }
  164. return $ret;
  165. }
  166. }
  167. class MockFS_Node {
  168. public $name; // The "name" of the entry, does not include a slash (exception, root).
  169. public $type; // The type of the entry 'f' for file, 'd' for directory.
  170. public $path; // The full path to the entry.
  171. function __construct( $path ) {
  172. $this->path = $path;
  173. $this->name = basename( $path );
  174. }
  175. function is_file() {
  176. return 'f' === $this->type;
  177. }
  178. function is_dir() {
  179. return 'd' === $this->type;
  180. }
  181. }
  182. class MockFS_Directory_Node extends MockFS_Node {
  183. public $type = 'd';
  184. public $children = array(); // The child nodes of this directory.
  185. }
  186. class MockFS_File_Node extends MockFS_Node {
  187. public $type = 'f';
  188. public $contents = ''; // The contents of the file.
  189. function __construct( $path, $contents = '' ) {
  190. parent::__construct( $path );
  191. $this->contents = $contents;
  192. }
  193. }