daux.io/libs/Generator/Helper.php

72 regels
1.9 KiB
PHP

2015-04-22 12:23:57 +02:00
<?php namespace Todaymade\Daux\Generator;
2015-04-23 00:32:30 +02:00
class Helper
{
2015-07-17 23:38:06 +02:00
/**
* Copy all files from $path to $local_base
*
* @param string $path
* @param string $local_base
*/
2015-04-23 00:32:30 +02:00
public static function copyAssets($path, $local_base)
{
2015-07-18 14:01:16 +02:00
if (is_dir($path)) {
static::rmdir($path);
} else {
mkdir($path);
}
2015-04-23 00:32:30 +02:00
2015-07-20 15:59:52 +02:00
mkdir($path . DIRECTORY_SEPARATOR . 'resources');
static::copyRecursive(
$local_base . DIRECTORY_SEPARATOR . 'resources',
$path . DIRECTORY_SEPARATOR . 'resources'
);
2015-04-22 12:23:57 +02:00
}
2015-07-17 23:38:06 +02:00
/**
* Remove a directory recursively
*
* @param string $dir
*/
2015-07-18 21:23:02 +02:00
protected 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
*/
2015-07-18 21:23:02 +02:00
protected 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);
while (false !== ($file = readdir($dir))) {
if ($file != '.' && $file != '..') {
if (is_dir($source . '/' . $file)) {
static::copyRecursive($source . '/' . $file, $destination . '/' . $file);
2015-04-23 00:32:30 +02:00
} else {
2015-07-17 23:38:06 +02:00
copy($source . '/' . $file, $destination . '/' . $file);
2015-04-22 12:23:57 +02:00
}
}
}
closedir($dir);
}
}