Banago\PHPloy\PHPloy::directoryToArray PHP Method

directoryToArray() public method

Get an array that represents directory tree Credit: http://php.net/manual/en/function.scandir.php#109140.
public directoryToArray ( string $directory, boolean $recursive = true, boolean $listDirs = false, boolean $listFiles = true, string $exclude = '' ) : array
$directory string Directory path
$recursive boolean Include sub directories
$listDirs boolean Include directories on listing
$listFiles boolean Include files on listing
$exclude string Exclude paths that matches this regex
return array
    public function directoryToArray($directory, $recursive = true, $listDirs = false, $listFiles = true, $exclude = '')
    {
        $arrayItems = [];
        $skipByExclude = false;
        $handle = opendir($directory);
        if ($handle) {
            while (false !== ($file = readdir($handle))) {
                preg_match("/(^(([\\.]){1,2})\$|(\\.(svn|git|md))|(Thumbs\\.db|\\.DS_STORE))\$/iu", $file, $skip);
                if ($exclude) {
                    preg_match($exclude, $file, $skipByExclude);
                }
                if (!$skip && !$skipByExclude) {
                    if (is_dir($directory . '/' . $file)) {
                        if ($recursive) {
                            $arrayItems = array_merge($arrayItems, $this->directoryToArray($directory . '/' . $file, $recursive, $listDirs, $listFiles, $exclude));
                        }
                        if ($listDirs) {
                            $file = $directory . '/' . $file;
                            $arrayItems[] = $file;
                        }
                    } else {
                        if ($listFiles) {
                            $file = $directory . '/' . $file;
                            $arrayItems[] = $file;
                        }
                    }
                }
            }
            closedir($handle);
        }
        return $arrayItems;
    }