Interfaces
1 week ago
trait-bufferedwritestreamviaputcontents.php
1 week ago
trait-copydirectoryrecursive.php
1 week ago
trait-copyfileviastreaming.php
1 week ago
trait-copyrecursiveviastreaming.php
1 week ago
trait-getcontentsviareadstream.php
1 week ago
trait-internalizewritestream.php
1 week ago
trait-mkdirrecursive.php
1 week ago
trait-putcontentsviawritestream.php
1 week ago
trait-readonlyfilesystem.php
1 week ago
trait-renamefileviacopyandrm.php
1 week ago
trait-rmdirrecursive.php
1 week ago
trait-mkdirrecursive.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\Filesystem\Mixin; |
| 4 | |
| 5 | use WordPress\Filesystem\FilesystemException; |
| 6 | |
| 7 | use function WordPress\Filesystem\wp_join_unix_paths; |
| 8 | use function WordPress\Filesystem\wp_unix_path_segments; |
| 9 | |
| 10 | /** |
| 11 | * Implements a recursive mkdir() function by calling mkdir_single() for |
| 12 | * each non-existing segment of the path. |
| 13 | */ |
| 14 | trait MkdirRecursive { |
| 15 | |
| 16 | public function mkdir( $path, $options = array() ) { |
| 17 | // Windows paths compatibility for LocalFilesystem:. |
| 18 | if ( method_exists( $this, 'normalize_path' ) ) { |
| 19 | $path = $this->normalize_path( $path ); |
| 20 | } |
| 21 | |
| 22 | $recursive = $options['recursive'] ?? false; |
| 23 | if ( ! $recursive ) { |
| 24 | $this->mkdir_single( $path, $options ); |
| 25 | |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * We'll only be checking the subpaths of the filesystem root, |
| 31 | * while assuming that the root itself already exists. |
| 32 | * |
| 33 | * Before we may proceed, we need to confirm our assumption that |
| 34 | * $path is within the filesystem root. |
| 35 | * |
| 36 | * ChrootLayer typically takes care of this. The code below is just |
| 37 | * extra sanity checking before we run a bunch of string operations on |
| 38 | * $path with the assumption that it started with $root. |
| 39 | */ |
| 40 | $root = rtrim( $this->get_root(), '/' ) . '/'; |
| 41 | $path = rtrim( $path, '/' ) . '/'; |
| 42 | |
| 43 | // Windows paths compatibility for LocalFilesystem:. |
| 44 | if ( method_exists( $this, 'normalize_path' ) ) { |
| 45 | $root = $this->normalize_path( $root ); |
| 46 | $path = $this->normalize_path( $path ); |
| 47 | } |
| 48 | // Assert to be extra sure the operation is safe:. |
| 49 | if ( 0 !== strncmp( $path, $root, strlen( $root ) ) ) { |
| 50 | throw new FilesystemException( sprintf( 'Path %s is not within the root %s', $path, $root ) ); |
| 51 | } |
| 52 | |
| 53 | $child_path = substr( $path, strlen( $root ) ); |
| 54 | $segments = wp_unix_path_segments( $child_path ); |
| 55 | for ( $i = 0; $i < count( $segments ); $i++ ) { |
| 56 | $parent_path = wp_join_unix_paths( |
| 57 | $root, |
| 58 | ...array_slice( $segments, 0, $i + 1 ) |
| 59 | ); |
| 60 | if ( ! $this->exists( $parent_path ) ) { |
| 61 | $this->mkdir_single( $parent_path, $options ); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private function parent_paths( $path ) { |
| 67 | $segments = wp_unix_path_segments( $path ); |
| 68 | $paths = array(); |
| 69 | for ( $i = 0; $i < count( $segments ) - 1; $i++ ) { |
| 70 | $paths[] = $segments[ $i ]; |
| 71 | yield wp_join_unix_paths( ...$paths ); |
| 72 | } |
| 73 | yield $path; |
| 74 | } |
| 75 | |
| 76 | abstract protected function get_root(): string; |
| 77 | |
| 78 | abstract protected function mkdir_single( $path, $options = array() ); |
| 79 | } |
| 80 |