/** * Returns a list of directories within a directory. * * @return array An array containing all directories (and files if $showFiles is true). * @param string $path Path of the directory. * @param bool[optional] $showFiles Should files be included in the list. * @param array[optional] $excluded An array containing directories/files to exclude. * @param string[optional] $includeRegexp An regular expression that represents the directories/files to include in the list. Other directories will be excluded. */ public static function getList($path, $showFiles = false, array $excluded = null, $includeRegexp = null) { // redefine arguments $path = (string) $path; $showFiles = (bool) $showFiles; // validate regex if ($includeRegexp !== null) { // redefine $includeRegexp = (string) $includeRegexp; // validate if (!SpoonFilter::isValidRegexp($includeRegexp)) { throw new SpoonDirectoryException('Invalid regular expression (' . $includeRegexp . ').'); } } // define file list $directories = array(); // directory exists if (self::exists($path)) { // attempt to open directory $directory = @opendir($path); // do your thing if directory-handle isn't false if ($directory !== false) { // start reading while (($file = readdir($directory)) !== false) { // no '.' and '..' and it's a file if ($file != '.' && $file != '..') { // directory if (is_dir($path . '/' . $file)) { // exclude certain files if (count($excluded) != 0) { if (!in_array($file, $excluded)) { if ($includeRegexp !== null) { // init var $matches = array(); // is this a match? if (preg_match($includeRegexp, $file, $matches) != 0) { $directories[] = $file; } } else { $directories[] = $file; } } } else { if ($includeRegexp !== null) { // init var $matches = array(); // is this a match? if (preg_match($includeRegexp, $file, $matches) != 0) { $directories[] = $file; } } else { $directories[] = $file; } } } else { // show files if ($showFiles) { // exclude certain files if (count($excluded) != 0) { if (!in_array($file, $excluded)) { $directories[] = $file; } } else { $directories[] = $file; } } } } } } // close directory @closedir($directory); } // cough up directory listing natsort($directories); return $directories; }
public function testIsValidRegexp() { $this->assertTrue(SpoonFilter::isValidRegexp('/boobies/')); }
/** * Retrieves a list of files within a directory. * * @return array An array containing a list of files in the given directory. * @param string $path The path to the directory. * @param string[optional] $includeRegexp A regular expresion that filters the files that should be included in the list. */ public static function getList($path, $includeRegexp = null) { // redefine arguments $path = (string) $path; // validate regex if ($includeRegexp !== null) { // redefine $includeRegexp = (string) $includeRegexp; // validate if (!SpoonFilter::isValidRegexp($includeRegexp)) { throw new SpoonFileSystemException('Invalid regular expression (' . $includeRegexp . ')'); } } // define list $files = array(); // directory exists if (SpoonDirectory::exists($path)) { // attempt to open directory if ($directory = @opendir($path)) { // start reading while (($file = readdir($directory)) !== false) { // no '.' and '..' and it's a file if ($file != '.' && $file != '..' && is_file($path . '/' . $file)) { // is there a include-pattern? if ($includeRegexp !== null) { // init var $matches = array(); // is this a match? if (preg_match($includeRegexp, $file, $matches) != 0) { $files[] = $file; } } else { $files[] = $file; } } } } // close directory @closedir($directory); } // directory doesn't exist or a problem occured return $files; }