2015-07-23 17:44:24 +02:00
|
|
|
<?php namespace Todaymade\Daux;
|
2015-04-22 12:23:57 +02:00
|
|
|
|
2016-09-06 23:11:32 +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
|
|
|
/**
|
|
|
|
* Remove a directory recursively
|
|
|
|
*
|
|
|
|
* @param string $dir
|
|
|
|
*/
|
2016-09-06 23:11:32 +02:00
|
|
|
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
|
|
|
/**
|
|
|
|
* Copy files recursively
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $destination
|
|
|
|
*/
|
2016-02-15 21:14:48 +01:00
|
|
|
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);
|
2016-09-06 23:11:32 +02:00
|
|
|
|
|
|
|
if (!$dir) {
|
|
|
|
throw new RuntimeException("Cannot copy '$source' to '$destination'");
|
|
|
|
}
|
|
|
|
|
2018-07-20 22:11:16 +02:00
|
|
|
$aExclDirs = array(
|
|
|
|
'scss',
|
|
|
|
'templates'
|
|
|
|
);
|
|
|
|
$aExclFiles = array(
|
|
|
|
'config.json'
|
|
|
|
);
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
while (false !== ($file = readdir($dir))) {
|
|
|
|
if ($file != '.' && $file != '..') {
|
2018-07-20 22:11:16 +02:00
|
|
|
if (is_dir($source . DIRECTORY_SEPARATOR . $file) && false == in_array($file, $aExclDirs)) {
|
2015-08-15 16:31:36 +02:00
|
|
|
static::copyRecursive(
|
|
|
|
$source . DIRECTORY_SEPARATOR . $file,
|
|
|
|
$destination . DIRECTORY_SEPARATOR . $file
|
|
|
|
);
|
2018-07-20 22:11:16 +02:00
|
|
|
} elseif (false == in_array($file, $aExclFiles)) {
|
2015-08-15 16:31:36 +02:00
|
|
|
copy($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
|
2015-04-22 12:23:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dir);
|
|
|
|
}
|
|
|
|
}
|