/** * Search, inside a dir, for a file pattern using glob(* and ?) * @param string path * @param bool recursive * @return array path to files */ public static function getFilesByGlob($sPath, $bRecursive = false) { if (!$bRecursive) { return glob($sPath); } else { $sDir = dirname($sPath) ? realpath(dirname($sPath)) : realpath('./'); $sGlob = basename($sPath); $dh = @opendir($sDir); if (!$dh) { throw new Exception("Cannot open directory '{$sPath}'"); } $aMatches = glob($sDir . '/' . $sGlob); while ($entry = @readdir($dh)) { if ($entry == '.' or $entry == '..') { continue; } elseif (is_dir($sDir . '/' . $entry)) { $aMatches = array_merge($aMatches, PHP_Beautifier_Common::getFilesByGlob($sDir . '/' . $entry . '/' . $sGlob, true)); } } return $aMatches; } }
/** * setInputFilePost * * @access private * @return void */ private function setInputFilePost() { $bCli = php_sapi_name() == 'cli'; // ArrayNested->off() if ($bCli and $this->mPreInputFiles == STDIN) { $mInputFiles = array(STDIN); } else { $mInputFiles = array(); foreach ($this->mPreInputFiles as $sPath) { $mInputFiles = array_merge($mInputFiles, PHP_Beautifier_Common::getFilesByGlob($sPath, $this->bRecursive)); } } // now, we create stream references for compressed files.... foreach ($mInputFiles as $sFile) { // First, tar files if (!($bCli and $sFile == STDIN) and preg_match("/(.tgz|\\.tar\\.gz|\\.tar\\.bz2|\\.tar)\$/", $sFile, $aMatch)) { if (strpos($aMatch[1], 'gz') !== false) { $sCompress = 'gz'; } elseif (strpos($aMatch[1], 'bz2') !== false) { $sCompress = 'bz2'; } elseif (strpos($aMatch[1], 'tar') !== false) { $sCompress = false; } $oTar = new Archive_Tar($sFile, $sCompress); foreach ($oTar->listContent() as $aInput) { if (empty($aInput['typeflag'])) { $this->mInputFiles[] = 'tarz://' . $sFile . '#' . $aInput['filename']; } } } else { $this->mInputFiles[] = $sFile; } } if (!$this->mInputFiles) { throw new Exception("Can't match any file"); } return true; // ArrayNested->on() }
function testgetFilesByGlob() { $sDir = PHP_Beautifier_Common::normalizeDir(dirname(__FILE__)); $aExpected = array($sDir . 'BeautifierCommonTest.php'); $this->assertEquals($aExpected, PHP_Beautifier_Common::getFilesByGlob($sDir . basename(__FILE__, '.php') . '.???', false)); }