<?php

class FilesHelper{

    public static function enumerate_recursive_content($path)
    {
        $files = array();
        $flags = FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS;
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, $flags));
        foreach ($iterator as $fileInfo) {
            $files[$iterator->getSubPathname()] = file_get_contents($fileInfo->getPathName());
        }
        return $files;
    }

    public static function enumerate_files($path, $recursive = true) {
        $files = array();
        if (!is_dir($path)) {
            return $files;
        }

        if ($handle = opendir($path)) {
            while (($name = readdir($handle)) !== false) {
                if (preg_match('#^\.#', $name)) {
                    continue;
                }

                if (is_dir($path . "/" . $name) && $recursive) {
                    $files = array_merge($files, FilesHelper::enumerate_files($path . "/" . $name));
                } else {
                    $files[] = array('path' => $path . '/' . $name);
                }
            }
            closedir($handle);
        }

        return $files;
    }

    public static function copy_recursive($source, $destination, $change_file = null) {
        if(is_file($source)) {
            if($change_file) {
                if (!is_dir(dirname($destination))){
                    if (!mkdir(dirname($destination), 0777, true)){
                        return;
                    }
                }
                file_put_contents($destination, call_user_func($change_file, $source));
            } else {
                copy($source, $destination);
            }
        } elseif(is_dir($source)) {
            if(!is_dir($destination)) {
                if(!mkdir($destination)) {
                    return;
                }
            }
            if ($dh = opendir($source)) {
                while (($file = readdir($dh)) !== false) {
                    if('.' == $file || '..' == $file) {
                        continue;
                    }
                    FilesHelper::copy_recursive($source . '/' . $file, $destination . '/' . $file, $change_file);
                }
                closedir($dh);
            }
        }
    }

    public static  function empty_dir($dir, $hard = false) {
        if(!is_dir($dir)) {
            return;
        }
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir,
                FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST);

        foreach ($iterator as $path) {
            if ($path->isDir()) {
                rmdir($path->__toString());
            } else {
                unlink($path->__toString());
            }
        }
        if($hard) {
            rmdir($dir);
        }
    }

    public static function normalize_path($path) {
        $root = ($path[0] === '/') ? '/' : '';
        $segments = preg_split('/[\\/\\\\]/', trim($path, '/'));
        $ret = array();
        foreach ($segments as $segment) {
            if (($segment == '.') || empty($segment)) {
                continue;
            }
            if ($segment == '..') {
                array_pop($ret);
            } else {
                array_push($ret, $segment);
            }
        }
        return $root . implode('/', $ret);
    }

    public static function create_temp_dir() {
        $temp = tempnam(sys_get_temp_dir(), uniqid());
        unlink($temp);
        mkdir($temp);
        return $temp . '/';
    }

}
