diff --git a/src/Composer/Command/CreateProjectCommand.php b/src/Composer/Command/CreateProjectCommand.php index ccf14fde3..a4c14b162 100644 --- a/src/Composer/Command/CreateProjectCommand.php +++ b/src/Composer/Command/CreateProjectCommand.php @@ -152,6 +152,7 @@ EOT } $composer = Factory::create($io, null, $disablePlugins); + $fs = new Filesystem(); if ($noScripts === false) { // dispatch event @@ -187,7 +188,6 @@ EOT } try { - $fs = new Filesystem(); $dirs = iterator_to_array($finder); unset($finder); foreach ($dirs as $dir) { @@ -222,10 +222,10 @@ EOT chdir($oldCwd); $vendorComposerDir = $composer->getConfig()->get('vendor-dir').'/composer'; - if (is_dir($vendorComposerDir) && glob($vendorComposerDir.'/*') === array() && count(glob($vendorComposerDir.'/.*')) === 2) { + if (is_dir($vendorComposerDir) && $fs->isDirEmpty($vendorComposerDir)) { @rmdir($vendorComposerDir); $vendorDir = $composer->getConfig()->get('vendor-dir'); - if (is_dir($vendorDir) && glob($vendorDir.'/*') === array() && count(glob($vendorDir.'/.*')) === 2) { + if (is_dir($vendorDir) && $fs->isDirEmpty($vendorDir)) { @rmdir($vendorDir); } } diff --git a/src/Composer/Downloader/ArchiveDownloader.php b/src/Composer/Downloader/ArchiveDownloader.php index f53798a00..3f1cc4117 100644 --- a/src/Composer/Downloader/ArchiveDownloader.php +++ b/src/Composer/Downloader/ArchiveDownloader.php @@ -132,7 +132,7 @@ abstract class ArchiveDownloader extends FileDownloader */ private function listFiles($dir) { - $files = array_merge(glob($dir . '/.*'), glob($dir . '/*')); + $files = array_merge(glob($dir . '/.*') ?: array(), glob($dir . '/*') ?: array()); return array_values(array_filter($files, function ($el) { return basename($el) !== '.' && basename($el) !== '..'; diff --git a/src/Composer/Installer/ProjectInstaller.php b/src/Composer/Installer/ProjectInstaller.php index 5d98722c8..c79238b36 100644 --- a/src/Composer/Installer/ProjectInstaller.php +++ b/src/Composer/Installer/ProjectInstaller.php @@ -15,6 +15,7 @@ namespace Composer\Installer; use Composer\Package\PackageInterface; use Composer\Downloader\DownloadManager; use Composer\Repository\InstalledRepositoryInterface; +use Composer\Util\Filesystem; /** * Project Installer is used to install a single package into a directory as @@ -26,11 +27,13 @@ class ProjectInstaller implements InstallerInterface { private $installPath; private $downloadManager; + private $filesystem; public function __construct($installPath, DownloadManager $dm) { $this->installPath = rtrim(strtr($installPath, '\\', '/'), '/').'/'; $this->downloadManager = $dm; + $this->filesystem = new Filesystem; } /** @@ -58,7 +61,7 @@ class ProjectInstaller implements InstallerInterface public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { $installPath = $this->installPath; - if (file_exists($installPath) && (count(glob($installPath.'*')) || (count(glob($installPath.'.*')) > 2))) { + if (file_exists($installPath) && !$this->filesystem->isDirEmpty($installPath)) { throw new \InvalidArgumentException("Project directory $installPath is not empty."); } if (!is_dir($installPath)) { diff --git a/src/Composer/Util/Filesystem.php b/src/Composer/Util/Filesystem.php index bdef6e402..9b7f04901 100644 --- a/src/Composer/Util/Filesystem.php +++ b/src/Composer/Util/Filesystem.php @@ -41,6 +41,19 @@ class Filesystem return false; } + /** + * Checks if a directory is empty + * + * @param string $dir + * @return bool + */ + public function isDirEmpty($dir) + { + $dir = rtrim($dir, '/\\'); + + return count(glob($dir.'/*') ?: array()) === 0 && count(glob($dir.'/.*') ?: array()) === 2; + } + /** * Recursively remove a directory *