mock-fs.php 5.8 KB

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