From e890d1bc5900f06a00dcd19e3504b2e1edc0f33e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 9 Jun 2014 13:12:42 +0200 Subject: [PATCH] Remove use of glob, fixes #3042 --- src/Composer/Command/SelfUpdateCommand.php | 39 ++++++++------- src/Composer/Downloader/ArchiveDownloader.php | 16 ++++--- src/Composer/Util/Filesystem.php | 47 ++++++------------- 3 files changed, 47 insertions(+), 55 deletions(-) diff --git a/src/Composer/Command/SelfUpdateCommand.php b/src/Composer/Command/SelfUpdateCommand.php index e1cbdbbda..cad41df51 100644 --- a/src/Composer/Command/SelfUpdateCommand.php +++ b/src/Composer/Command/SelfUpdateCommand.php @@ -21,6 +21,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Finder\Finder; /** * @author Igor Wiedler @@ -113,15 +114,13 @@ EOT // remove saved installations of composer if ($input->getOption('clean-backups')) { - $files = $this->getOldInstallationFiles($rollbackDir); + $finder = $this->getOldInstallationFinder($rollbackDir); - if (!empty($files)) { - $fs = new Filesystem; - - foreach ($files as $file) { - $output->writeln('Removing: '.$file.''); - $fs->remove($file); - } + $fs = new Filesystem; + foreach ($finder as $file) { + $file = (string) $file; + $output->writeln('Removing: '.$file.''); + $fs->remove($file); } } @@ -201,19 +200,25 @@ EOT protected function getLastBackupVersion($rollbackDir) { - $files = $this->getOldInstallationFiles($rollbackDir); - if (empty($files)) { - return false; + $finder = $this->getOldInstallationFinder($rollbackDir); + $finder->sortByName(); + $files = iterator_to_array($finder); + + if (count($files)) { + return basename(end($files), self::OLD_INSTALL_EXT); } - sort($files); - - return basename(end($files), self::OLD_INSTALL_EXT); + return false; } - protected function getOldInstallationFiles($rollbackDir) + protected function getOldInstallationFinder($rollbackDir) { - $fs = new Filesystem; - return $fs->realpathGlob($rollbackDir . '/*' . self::OLD_INSTALL_EXT); + $finder = Finder::create() + ->depth(0) + ->files() + ->name('*' . self::OLD_INSTALL_EXT) + ->in($dir); + + return $finder; } } diff --git a/src/Composer/Downloader/ArchiveDownloader.php b/src/Composer/Downloader/ArchiveDownloader.php index 71fd38ccd..db1fc674c 100644 --- a/src/Composer/Downloader/ArchiveDownloader.php +++ b/src/Composer/Downloader/ArchiveDownloader.php @@ -13,6 +13,7 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; +use Symfony\Component\Finder\Finder; /** * Base downloader for archives @@ -52,12 +53,13 @@ abstract class ArchiveDownloader extends FileDownloader $contentDir = $this->getFolderContent($temporaryDir); // only one dir in the archive, extract its contents out of it - if (1 === count($contentDir) && is_dir($contentDir[0])) { - $contentDir = $this->getFolderContent($contentDir[0]); + if (1 === count($contentDir) && is_dir(reset($contentDir))) { + $contentDir = $this->getFolderContent((string) reset($contentDir)); } // move files back out of the temp dir foreach ($contentDir as $file) { + $file = (string) $file; $this->filesystem->rename($file, $path . '/' . basename($file)); } @@ -133,10 +135,12 @@ abstract class ArchiveDownloader extends FileDownloader */ private function getFolderContent($dir) { - $files = array_merge($this->filesystem->realpathGlob($dir . '/.*'), $this->filesystem->realpathGlob($dir . '/*')); + $finder = Finder::create() + ->ignoreVCS(false) + ->ignoreDotFiles(false) + ->depth(0) + ->in($dir); - return array_values(array_filter($files, function ($el) { - return basename($el) !== '.' && basename($el) !== '..'; - })); + return iterator_to_array($finder); } } diff --git a/src/Composer/Util/Filesystem.php b/src/Composer/Util/Filesystem.php index fab4c5793..b567c4b31 100644 --- a/src/Composer/Util/Filesystem.php +++ b/src/Composer/Util/Filesystem.php @@ -14,6 +14,7 @@ namespace Composer\Util; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; +use Symfony\Component\Finder\Finder; /** * @author Jordi Boggiano @@ -41,29 +42,6 @@ class Filesystem return false; } - /** - * Force the results of a glob to be realpaths. - * - * @param string $pattern - * @param int $flags - * @return array - */ - public function realpathGlob($pattern, $flags = 0) - { - $matches = glob($pattern, $flags); - if (!$matches) { - return array(); - } - - return array_map(function ($path) { - if (basename($path) === '.' || basename($path) === '..') { - return $path; - } - - return realpath($path); - }, $matches); - } - /** * Checks if a directory is empty * @@ -72,9 +50,13 @@ class Filesystem */ public function isDirEmpty($dir) { - $dir = rtrim($dir, '/\\'); + $finder = Finder::create() + ->ignoreVCS(false) + ->ignoreDotFiles(false) + ->depth(0) + ->in($dir); - return count($this->realpathGlob($dir.'/*')) === 0 && count($this->realpathGlob($dir.'/.*')) === 2; + return count($finder) === 0; } public function emptyDirectory($dir, $ensureDirectoryExists = true) @@ -84,13 +66,14 @@ class Filesystem } if (is_dir($dir)) { - foreach ($this->realpathGlob(rtrim($dir, '\\/').'/*') as $path) { - $this->remove($path); - } - foreach ($this->realpathGlob(rtrim($dir, '\\/').'/.*') as $path) { - if (basename($path) !== '..' && basename($path) !== '.') { - $this->remove($path); - } + $finder = Finder::create() + ->ignoreVCS(false) + ->ignoreDotFiles(false) + ->depth(0) + ->in($dir); + + foreach ($finder as $path) { + $this->remove((string) $path); } } }