1
0
Fork 0

Remove use of glob, fixes #3042

pull/3051/head
Jordi Boggiano 2014-06-09 13:12:42 +02:00
parent 15a99f31b3
commit e890d1bc59
3 changed files with 47 additions and 55 deletions

View File

@ -21,6 +21,7 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
/** /**
* @author Igor Wiedler <igor@wiedler.ch> * @author Igor Wiedler <igor@wiedler.ch>
@ -113,15 +114,13 @@ EOT
// remove saved installations of composer // remove saved installations of composer
if ($input->getOption('clean-backups')) { if ($input->getOption('clean-backups')) {
$files = $this->getOldInstallationFiles($rollbackDir); $finder = $this->getOldInstallationFinder($rollbackDir);
if (!empty($files)) { $fs = new Filesystem;
$fs = new Filesystem; foreach ($finder as $file) {
$file = (string) $file;
foreach ($files as $file) { $output->writeln('<info>Removing: '.$file.'</info>');
$output->writeln('<info>Removing: '.$file.'</info>'); $fs->remove($file);
$fs->remove($file);
}
} }
} }
@ -201,19 +200,25 @@ EOT
protected function getLastBackupVersion($rollbackDir) protected function getLastBackupVersion($rollbackDir)
{ {
$files = $this->getOldInstallationFiles($rollbackDir); $finder = $this->getOldInstallationFinder($rollbackDir);
if (empty($files)) { $finder->sortByName();
return false; $files = iterator_to_array($finder);
if (count($files)) {
return basename(end($files), self::OLD_INSTALL_EXT);
} }
sort($files); return false;
return basename(end($files), self::OLD_INSTALL_EXT);
} }
protected function getOldInstallationFiles($rollbackDir) protected function getOldInstallationFinder($rollbackDir)
{ {
$fs = new Filesystem; $finder = Finder::create()
return $fs->realpathGlob($rollbackDir . '/*' . self::OLD_INSTALL_EXT); ->depth(0)
->files()
->name('*' . self::OLD_INSTALL_EXT)
->in($dir);
return $finder;
} }
} }

View File

@ -13,6 +13,7 @@
namespace Composer\Downloader; namespace Composer\Downloader;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Symfony\Component\Finder\Finder;
/** /**
* Base downloader for archives * Base downloader for archives
@ -52,12 +53,13 @@ abstract class ArchiveDownloader extends FileDownloader
$contentDir = $this->getFolderContent($temporaryDir); $contentDir = $this->getFolderContent($temporaryDir);
// only one dir in the archive, extract its contents out of it // only one dir in the archive, extract its contents out of it
if (1 === count($contentDir) && is_dir($contentDir[0])) { if (1 === count($contentDir) && is_dir(reset($contentDir))) {
$contentDir = $this->getFolderContent($contentDir[0]); $contentDir = $this->getFolderContent((string) reset($contentDir));
} }
// move files back out of the temp dir // move files back out of the temp dir
foreach ($contentDir as $file) { foreach ($contentDir as $file) {
$file = (string) $file;
$this->filesystem->rename($file, $path . '/' . basename($file)); $this->filesystem->rename($file, $path . '/' . basename($file));
} }
@ -133,10 +135,12 @@ abstract class ArchiveDownloader extends FileDownloader
*/ */
private function getFolderContent($dir) 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 iterator_to_array($finder);
return basename($el) !== '.' && basename($el) !== '..';
}));
} }
} }

View File

@ -14,6 +14,7 @@ namespace Composer\Util;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
use RecursiveIteratorIterator; use RecursiveIteratorIterator;
use Symfony\Component\Finder\Finder;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -41,29 +42,6 @@ class Filesystem
return false; 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 * Checks if a directory is empty
* *
@ -72,9 +50,13 @@ class Filesystem
*/ */
public function isDirEmpty($dir) 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) public function emptyDirectory($dir, $ensureDirectoryExists = true)
@ -84,13 +66,14 @@ class Filesystem
} }
if (is_dir($dir)) { if (is_dir($dir)) {
foreach ($this->realpathGlob(rtrim($dir, '\\/').'/*') as $path) { $finder = Finder::create()
$this->remove($path); ->ignoreVCS(false)
} ->ignoreDotFiles(false)
foreach ($this->realpathGlob(rtrim($dir, '\\/').'/.*') as $path) { ->depth(0)
if (basename($path) !== '..' && basename($path) !== '.') { ->in($dir);
$this->remove($path);
} foreach ($finder as $path) {
$this->remove((string) $path);
} }
} }
} }