Extracts from the filesystem all the files under a path.
If the flag only_dirs is set to true returns only the directories names.

@param string $relative_path
@param bool $only_dirs
@return array
196270
public function getFileSystemFiles( $relative_path, $only_dirs = false )
	{
		$files = array();

		// Extract directories:
		$iterator = new \DirectoryIterator( ROOT_PATH . "/$relative_path" );

		foreach ( $iterator as $file_info )
		{
			$file = $file_info->getFilename();

			// Exclude .svn, .cache and any other file starting with .
			if ( 0 !== strpos( $file, '.' ) )
			{
				if ( !$only_dirs || $file_info->isDir() )
				{
					$files[] = $file;
				}
			}
		}

		return $files;
	}
1095234
protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDirectory, $relativePath, $subParsersFilter, $exclude3rdParty)
    {
        $themesPresets = array();
        $prefix = ($relativePath === '') ? '' : "$relativePath/";
        $matches = null;
        foreach (array_merge(array(''), $this->getDirectoryStructure($rootDirectory, $exclude3rdParty)) as $child) {
            $presetsAbsDirectory = ($child === '') ? $rootDirectory : "$rootDirectory/$child";
            if (preg_match('%(?:^|/)themes/\w+/css/presets$%', $presetsAbsDirectory, $matches)) {
                $dirList = @scandir($presetsAbsDirectory);
                if ($dirList === false) {
                    throw new \Exception("Unable to parse directory $presetsAbsDirectory");
                }
                $shownChild = ($child === '') ? rtrim($prefix, '/') : ($prefix.$child);
                foreach ($dirList as $file) {
                    if (($file[0] !== '.') && preg_match('/[^.].*\.less$/i', $file)) {
                        $fileAbs = "$presetsAbsDirectory/$file";
                        if (is_file($fileAbs)) {
                            $content = @file_get_contents($fileAbs);
                            if ($content === false) {
                                throw new \Exception("Error reading file '$fileAbs'");
                            }
                            $content = str_replace("\r", "\n", str_replace("\r\n", "\n", $content));
                            // Strip multiline comments
                            $content = preg_replace_callback(
                                '|/\*.*?\*/|s',
                                function ($matches) {
                                    return str_repeat("\n", substr_count($matches[0], "\n"));
                                },
                                $content
                            );
                            foreach (array("'", '"') as $quote) {
                                if (preg_match('%(?:^|\\n|;)[ \\t]*@preset-name:\\s*'.$quote.'([^'.$quote.']*)'.$quote.'\\s*(?:;|$)%s', $content, $matches)) {
                                    $presetName = $matches[1];
                                    $presetLine = null;
                                    $p = strpos($content, $matches[0]);
                                    if ($p !== false) {
                                        $presetLine = substr_count(substr($content, 0, $p), "\n") + 1;
                                    }
                                    if (!isset($themesPresets[$presetName])) {
                                        $themesPresets[$presetName] = array();
                                    }
                                    $themesPresets[$presetName][] = array($shownChild."/$file", $presetLine);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        foreach ($themesPresets as $themesPreset => $references) {
            $translation = $translations->insert('PresetName', ucwords(str_replace(array('_', '-', '/'), ' ', $themesPreset)));
            foreach ($references as $reference) {
                $translation->addReference($reference[0], $reference[1]);
            }
        }
    }
538102
function _nlist_helper($dir, $recursive, $relativeDir)
    {
        $files = $this->_list($dir, false);

        if (!$recursive || $files === false) {
            return $files;
        }

        $result = array();
        foreach ($files as $value) {
            if ($value == '.' || $value == '..') {
                if ($relativeDir == '') {
                    $result[] = $value;
                }
                continue;
            }
            if (is_array($this->_query_stat_cache($this->_realpath($dir . '/' . $value)))) {
                $temp = $this->_nlist_helper($dir . '/' . $value, true, $relativeDir . $value . '/');
                $result = array_merge($result, $temp);
            } else {
                $result[] = $relativeDir . $value;
            }
        }

        return $result;
    }
418802
public static function scandir($path, bool $show_files=true, bool $show_dirs=true, int $depth=-1) {
		ref\file::path($path, true);
		if (! $path || ! @\is_dir($path) || (! $show_files && ! $show_dirs)) {
			return array();
		}

		// Set the depth for recursion.
		if ($depth < 0) {
			$inner_depth = -1;
		}
		elseif ($depth >= 1) {
			$inner_depth = $depth - 1;
		}
		else {
			$inner_depth = 0;
		}

		$out = array();
		if ($handle = @\opendir($path)) {
			ref\file::trailingslash($path);
			while (false !== ($file = @\readdir($handle))) {
				// Always ignore dots.
				if (('.' === $file) || ('..' === $file)) {
					continue;
				}

				// This is a file.
				if (@\is_file("{$path}{$file}")) {
					if ($show_files) {
						$out[] = "{$path}{$file}";
					}
				}
				elseif (@\is_dir("{$path}{$file}")) {
					if ($show_dirs) {
						$out[] = "{$path}{$file}/";
					}

					if ((-1 === $inner_depth) || $inner_depth > 0) {
						$out = \array_merge($out, static::scandir("{$path}{$file}", $show_files, $show_dirs, $inner_depth));
					}
				}
			}
			\closedir($handle);
		}

		\sort($out);
		return $out;
	}
414058
public function getDirectories(string $path, bool $isRecursive = false) : array
    {
        if (!$this->isDirectory($path)) {
            return [];
        }

        $directories = [];
        $iter = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::SELF_FIRST,
            RecursiveIteratorIterator::CATCH_GET_CHILD
        );

        if (!$isRecursive) {
            $iter->setMaxDepth(0);
        }

        foreach ($iter as $path => $item) {
            if ($item->isDir()) {
                $directories[] = $path;
            }
        }

        return $directories;
    }
851009
public function directoryFiles($path)
  {
    $contents = [];

    // Scan directory
    $directoryFiles = scandir($path);

    foreach ($directoryFiles as $index => $filePath)  {

      // Ommit . and ..
      if ( ! in_array($filePath, ['.', '..']))  {

        // Check if this is a directory
        if (is_dir($path . DIRECTORY_SEPARATOR . $filePath)) {

          // Rescan and get files in this directory
          $contents = array_merge($contents, self::directoryFiles($path . DIRECTORY_SEPARATOR . $filePath));

        } else  {

          // Add file to contens array
          $contents[] = $path . DIRECTORY_SEPARATOR . $filePath;
        }
      }
    }

    return $contents;
  }
857806
public function getFiles($onlySuccessful = true)
    {
        $context = $this->_context;

        $files = [];

        /** @var $_FILES array */
        foreach ($context->_FILES as $key => $file) {
            if (is_int($file['error'])) {
                if (!$onlySuccessful || $file['error'] === UPLOAD_ERR_OK) {
                    $fileInfo = [
                        'name' => $file['name'],
                        'type' => $file['type'],
                        'tmp_name' => $file['tmp_name'],
                        'error' => $file['error'],
                        'size' => $file['size'],
                    ];
                    $files[] = new File($key, $fileInfo);
                }
            } else {
                $countFiles = count($file['error']);
                /** @noinspection ForeachInvariantsInspection */
                for ($i = 0; $i < $countFiles; $i++) {
                    if (!$onlySuccessful || $file['error'][$i] === UPLOAD_ERR_OK) {
                        $fileInfo = [
                            'name' => $file['name'][$i],
                            'type' => $file['type'][$i],
                            'tmp_name' => $file['tmp_name'][$i],
                            'error' => $file['error'][$i],
                            'size' => $file['size'][$i],
                        ];
                        $files[] = new File($key, $fileInfo);
                    }
                }
            }
        }

        return $files;
    }
75804
public static function path($filename, $relativeTo = null, $checkExists = true, $ds = null)
    {
        $ds = $ds ?: DIRECTORY_SEPARATOR;

        if (is_array($filename)) {
            $pathnames = array();
            foreach ($filename as $k => $f) {
                $pathnames[$k] = self::path($f, $relativeTo, $checkExists);
            }
            return $pathnames;
        }

        $relativeTo = $relativeTo ?: self::$rootDirectory;
        $pathname = $filename;
        if ($filename{0} != '/' && !preg_match('#^[A-Z]:(\\\\|/)#', $filename)) {
            if (strlen($filename) >= 2 && substr($filename, 0, 2) == './') {
                $filename = substr($filename, 2);
            }
            $pathname = rtrim($relativeTo, $ds) . $ds . $filename;
        }
        if ($checkExists) {
            return realpath($pathname);
        }
        return $pathname;
    }
1063239
public static function getFileList(string $path, array $exts = null) : array
    {
        $files = [];
        if (is_dir($path) && ($dh = opendir($path))) {
            while (($file = readdir($dh)) !== false) {
                if ($file === '.' || $file === '..' || $file === '.DS_Store') {
                    continue;
                } elseif (is_dir("{$path}/{$file}")) {
                    $files = array_merge($files, self::getFileList("{$path}/{$file}", $exts));
                } elseif (is_null($exts)) {
                    $files[] = "{$path}/{$file}";
                } elseif (in_array(pathinfo("{$path}/{$file}", PATHINFO_EXTENSION), $exts)) {
                    $files[] = "{$path}/{$file}";
                } else {
                    continue;
                }
            }
            closedir($dh);
        }
        return $files;
    }
1028949
private function getFileResources()
    {
        $paths = array();

        foreach ($this->directories as $directory) {
            foreach ($directory as $file) {
                $relative = $this->getRelativeName($file, $directory);

                if (!isset($paths[$relative])) {
                    $paths[$relative] = $file;
                }
            }
        }

        return array_values($paths);
    }
493718
static function get_directory_list($directory_path)
    {
        $output = false;
        try {
            $output = scandir($directory_path);
            foreach ($output as $key => $file) {
                if (!is_file($directory_path.'/'.$file)) {
                    // just return files please.
                    unset($output[$key]);
                }
            }
        } catch (Exception $e) {
            echo ('Error reading directory: '.var_export($e, true).PHP_EOL);
            die();
        }
        return $output;
    }
575147
private function getDirectories($dir, $directoryName = null)
    {
        $finder = new Finder();
        $finder->directories()->in($dir);

        if (null !== $directoryName) {
            $finder->name($directoryName);
        }

        if (iterator_count($finder) > 0) {
            return iterator_to_array($finder);
        }

        return false;
    }
516978
public static function scanDirs($path, $onlyLevel = false, $lvl = 0)
    {
        static $res;

        if ($lvl == 0) {
            $res = [];
        }

        if (!is_dir($path)) {
            return [];
        }

        if (substr($path, -1) == '/') {
            $path = substr($path, 0, -1);
        }
        $dir_base_length = strlen(DIR_BASE) - 1;

        foreach (array_diff(scandir($path), ['.', '..']) as $f) {
            if (is_dir($path . '/' . $f)) {
                if (!$onlyLevel) $res[] = [
                    'path_url' => substr($path, $dir_base_length),
                    'path'     => $path,
                    'name'     => $f,
                    'full'     => $path . '/' . $f,
                    'fs'       => 0,
                    'type'     => 'dir'
                ];
                self::scanDirs($path . '/' . $f, $onlyLevel, $lvl + 1);
            } elseif ($onlyLevel) {
                $res[] = $path . '/' . $f;
            } else {
                $res[] = [
                    'path_url' => substr($path, $dir_base_length),
                    'path'     => $path,
                    'name'     => $f,
                    'full'     => $path . '/' . $f,
                    'fs'       => filesize($path . '/' . $f),
                    'type'     => 'file'
                ];
            }
        }

        if ($lvl == 0) {
            return $res;
        }

        return [];
    }
257407
public function getRelativePath(File $base_dir) {
        return FileSystem::getFileSystem()->makePathRelative($this->file->getAbsoluteFile()->getParent(), $base_dir->getAbsolutePath()) . $this->file->getName();
    }
314802
public function getFiles(string $path, bool $isRecursive = false) : array
    {
        if (!$this->isDirectory($path)) {
            return [];
        }

        $files = [];
        $iter = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::SELF_FIRST,
            RecursiveIteratorIterator::CATCH_GET_CHILD
        );

        if (!$isRecursive) {
            $iter->setMaxDepth(0);
        }

        foreach ($iter as $path => $item) {
            if ($item->isFile()) {
                $files[] = $path;
            }
        }

        return $files;
    }
1269072
protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDirectory, $relativePath, $subParsersFilter, $exclude3rdParty)
    {
        $prefix = ($relativePath === '') ? '' : "$relativePath/";
        foreach (array_merge(array(''), $this->getDirectoryStructure($rootDirectory, $exclude3rdParty)) as $child) {
            $shownDirectory = $prefix.(($child === '') ? '' : "$child/");
            $fullDirectoryPath = ($child === '') ? $rootDirectory : "$rootDirectory/$child";
            $contents = @scandir($fullDirectoryPath);
            if ($contents === false) {
                throw new \Exception("Unable to parse directory $fullDirectoryPath");
            }
            foreach ($contents as $file) {
                if ($file[0] !== '.') {
                    $fullFilePath = "$fullDirectoryPath/$file";
                    if (preg_match('/^(.*)\.xml$/', $file) && is_file($fullFilePath)) {
                        static::parseXml($translations, $fullFilePath, $shownDirectory.$file);
                    }
                }
            }
        }
    }
1290032
public function scan_dir_namespace_class( $dir, $split = false, $relative = '' ) {
		$dir  = rtrim( $dir, DS );
		$list = [];
		if ( is_dir( $dir ) ) {
			foreach ( scandir( $dir ) as $file ) {
				if ( $file === '.' || $file === '..' || $file === 'base.php' ) {
					continue;
				}

				$path = rtrim( $dir, DS ) . DS . $file;
				if ( is_file( $path ) ) {
					if ( $this->ends_with( $file, '.php' ) ) {
						if ( $split ) {
							$list[] = [ $relative, ucfirst( $this->app->get_page_slug( $file ) ) ];
						} else {
							$list[] = $relative . ucfirst( $this->app->get_page_slug( $file ) );
						}
					}
				} elseif ( is_dir( $path ) ) {
					$list = array_merge( $list, $this->scan_dir_namespace_class( $path, $split, $relative . ucfirst( $file ) . '\\' ) );
				}
			}
		}

		return $list;
	}