8
0
daux.io/libs/GeneratorHelper.php

61 Zeilen
1.7 KiB
PHP

2015-07-23 17:44:24 +02:00
<?php namespace Todaymade\Daux;
2015-04-22 12:23:57 +02:00
use RuntimeException;
2015-07-23 17:44:24 +02:00
class GeneratorHelper
2015-04-23 00:32:30 +02:00
{
2015-07-17 23:38:06 +02:00
/**
2020-04-22 22:24:52 +02:00
* Remove a directory recursively.
2015-07-17 23:38:06 +02:00
*
* @param string $dir
*/
public static function rmdir($dir)
2015-04-23 00:32:30 +02:00
{
2015-04-22 12:23:57 +02:00
$it = new \RecursiveDirectoryIterator($dir);
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
2015-04-23 00:32:30 +02:00
foreach ($files as $file) {
if ($file->getFilename() === '.' || $file->getFilename() === '..') {
continue;
}
if ($file->isDir()) {
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
2015-04-22 12:23:57 +02:00
}
}
2015-07-17 23:38:06 +02:00
/**
2020-04-22 22:24:52 +02:00
* Copy files recursively.
2015-07-17 23:38:06 +02:00
*
* @param string $source
* @param string $destination
*/
public static function copyRecursive($source, $destination)
2015-04-23 00:32:30 +02:00
{
2015-07-18 14:01:16 +02:00
if (!is_dir($destination)) {
mkdir($destination);
}
2015-07-17 23:38:06 +02:00
$dir = opendir($source);
2019-12-07 16:32:38 +01:00
if ($dir === false) {
throw new RuntimeException("Cannot copy '$source' to '$destination'");
}
2015-07-17 23:38:06 +02:00
while (false !== ($file = readdir($dir))) {
if ($file != '.' && $file != '..') {
if (is_dir($source . DIRECTORY_SEPARATOR . $file)) {
static::copyRecursive(
$source . DIRECTORY_SEPARATOR . $file,
$destination . DIRECTORY_SEPARATOR . $file
);
2015-04-23 00:32:30 +02:00
} else {
copy($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
2015-04-22 12:23:57 +02:00
}
}
}
closedir($dir);
}
}