/** * Combine any amount of strings into a path. * * @param string|array ...$paths One or as many parameters as you need. Both strings and arrays of strings can be mixed. * * @return string The combined paths. Note that the directory separators will be changed to reflect the local system. */ public static function combine(...$paths) { // Flatten into simple array $paths = Arrays::flatten(...$paths); // Keep non-empty elements $paths = array_filter($paths, 'strlen'); // Split into scheme:// and rest $scheme = self::extractScheme($paths); // Implode, localize and simplify everything after scheme:// $path = implode(DIRECTORY_SEPARATOR, $paths); $path = self::localize($path); $quotedSeparator = preg_quote(DIRECTORY_SEPARATOR); $pattern = '#' . $quotedSeparator . '+#'; $path = preg_replace($pattern, $quotedSeparator, $path); return $scheme . $path; }
/** * Permet de lire le contenu d'un répertoire lorsqu'on n'a pas accès à la SPL FileSystemIterator (version PHP < 5.3) * * @param string $path le chemin du répertoire * @return array tableau contenant tout le contenu du répertoire */ public static function readdir($path) { // initialisation variable de retour $ret = array(); // on gère par sécurité la fin du path pour ajouter ou pas le / if ('/' != substr($path, -1)) { $path .= '/'; } // on vérifie que $path est bien un répertoire if (is_dir($path)) { // ouverture du répertoire if ($dir = opendir($path)) { // on parcours le répertoire while (false !== ($dirElt = readdir($dir))) { if ($dirElt != '.' && $dirElt != '..') { if (!is_dir($path . $dirElt)) { $ret[] = $path . $dirElt; } else { $ret[] = static::readdir($path . $dirElt); } } } // fermeture du répertoire closedir($dir); } else { throw new Exception('error while opening ' . $path); } } else { throw new Exception($path . ' is not a directory'); } return Arrays::flatten($ret); }
public function flatten() { $this->_array = Arrays::flatten($this->_array); return $this; }