daux.io/libs/GeneratorHelper.php

75 lignes
2.0 KiB
PHP
Brut Vue normale Historique

2015-07-23 17:44:24 +02:00
<?php namespace Todaymade\Daux;
2015-04-22 12:23:57 +02:00
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
/**
* 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-08-09 22:18:23 +02:00
mkdir($path . DIRECTORY_SEPARATOR . 'themes');
2015-07-20 15:59:52 +02:00
static::copyRecursive(
2015-08-09 22:18:23 +02:00
$local_base . DIRECTORY_SEPARATOR . 'themes',
$path . DIRECTORY_SEPARATOR . 'themes'
2015-07-20 15:59:52 +02:00
);
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 . 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);
}
}