daux.io/libs/GeneratorHelper.php

69 regels
1.9 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
/**
* Remove a directory recursively
*
* @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
/**
* Copy files recursively
*
* @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);
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)) {
static::copyRecursive(
$source . DIRECTORY_SEPARATOR . $file,
$destination . DIRECTORY_SEPARATOR . $file
);
2018-07-20 22:11:16 +02:00
} elseif (false == in_array($file, $aExclFiles)) {
copy($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
2015-04-22 12:23:57 +02:00
}
}
}
closedir($dir);
}
}