Skip to content

Commit 988caa3

Browse files
committed
rewrite remove_dir and copy_dir
1 parent 000bd3a commit 988caa3

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

src/Context/FeatureContext.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,11 +1280,24 @@ public function move_files( $src, $dest ): void {
12801280
* @param string $dir
12811281
*/
12821282
public static function remove_dir( $dir ): void {
1283-
if ( self::is_windows() ) {
1284-
Process::create( Utils\esc_cmd( 'del /f /q %s', $dir ) )->run_check();
1285-
} else {
1286-
Process::create( Utils\esc_cmd( 'rm -rf %s', $dir ) )->run_check();
1283+
if ( ! file_exists( $dir ) ) {
1284+
return;
12871285
}
1286+
1287+
if ( ! is_dir( $dir ) ) {
1288+
unlink( $dir );
1289+
return;
1290+
}
1291+
1292+
foreach ( scandir( $dir ) as $item ) {
1293+
if ( '.' === $item || '..' === $item ) {
1294+
continue;
1295+
}
1296+
1297+
self::remove_dir( $dir . DIRECTORY_SEPARATOR . $item );
1298+
}
1299+
1300+
rmdir( $dir );
12881301
}
12891302

12901303
/**
@@ -1294,13 +1307,21 @@ public static function remove_dir( $dir ): void {
12941307
* @param string $dest_dir
12951308
*/
12961309
public static function copy_dir( $src_dir, $dest_dir ): void {
1297-
$shell_command = Utils\esc_cmd( 'cp -r %s/* %s', $src_dir, $dest_dir );
1298-
if ( 'Darwin' === PHP_OS ) {
1299-
$shell_command = Utils\esc_cmd( 'cp -R %s/* %s', $src_dir, $dest_dir );
1300-
} elseif ( self::is_windows() ) {
1301-
$shell_command = Utils\esc_cmd( 'copy /y %s %s', $src_dir, $dest_dir );
1310+
$iterator = new \RecursiveIteratorIterator(
1311+
new \RecursiveDirectoryIterator( $src_dir, \RecursiveDirectoryIterator::SKIP_DOTS ),
1312+
\RecursiveIteratorIterator::SELF_FIRST
1313+
);
1314+
1315+
foreach ( $iterator as $item ) {
1316+
$dest_path = $dest_dir . DIRECTORY_SEPARATOR . $iterator->getSubPathname();
1317+
if ( $item->isDir() ) {
1318+
if ( ! is_dir( $dest_path ) ) {
1319+
mkdir( $dest_path, 0777, true );
1320+
}
1321+
} else {
1322+
copy( $item->getPathname(), $dest_path );
1323+
}
13021324
}
1303-
Process::create( $shell_command )->run_check();
13041325
}
13051326

13061327
/**

0 commit comments

Comments
 (0)