/** * Lists all files and directories in a directory * * @param integer $depth * @param mixed $filter * @param string $type * @param boolean $asHandlers * * @return array */ public function listContents($depth = 0, $filter = null, $type = 'all', $asHandlers = false) { $pattern = $this->path . '/*'; if (is_array($filter)) { $filters = $filter; $filter = new Filter(); foreach ($filters as $f => $type) { if (!is_int($f)) { $f = $type; $type = null; } $expected = true; if (strpos($f, '!') === 0) { $f = substr($f, 1); $expected = false; } $filter->addFilter($f, $expected, $type); } } if ($filter instanceof \Closure) { $callback = $filter; $filter = new Filter(); $callback($filter); } if (!$filter) { $filter = new Filter(); } $flags = GLOB_MARK; if ($type === 'file' and !pathinfo($pattern, PATHINFO_EXTENSION)) { // Add an extension wildcard $pattern .= '.*'; } elseif ($type === 'dir') { $flags = GLOB_MARK | GLOB_ONLYDIR; } $contents = glob($pattern, $flags); // Filter the content. $contents = $filter->filter($contents); // Lower the depth for a recursive call if ($depth and $depth !== true) { $depth--; } $formatted = array(); foreach ($contents as $item) { if ($filter->isCorrectType('dir', $item)) { $_contents = array(); if (($depth === true or $depth === 0) and !$asHandlers) { $dir = new Directory($item); $_contents = $dir->listContents($item, $filter, $depth, $type); } if ($asHandlers) { $formatted[] = new Directory($item); } else { $formatted[$item] = $_contents; } } elseif ($filter->isCorrectType('file', $item)) { if ($asHandlers) { $item = new File($item); } $formatted[] = $item; } } return $formatted; }