Browse files

WIP: Update Composer/Util/Filesystem. Add Filesystem object to Projec…

…t Installer
  • Loading branch information...
1 parent 5dd5332 commit b9697a10a4410683efabcc57d2a77dc486ed5d2c @bzitzow bzitzow committed with Oct 4, 2012
Showing with 43 additions and 6 deletions.
  1. +14 −6 src/Composer/Installer/ProjectInstaller.php
  2. +29 −0 src/Composer/Util/Filesystem.php
View
20 src/Composer/Installer/ProjectInstaller.php
@@ -15,6 +15,7 @@
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
@@ -58,13 +59,20 @@ public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{
$installPath = $this->installPath;
- if (file_exists($installPath)) {
- throw new \InvalidArgumentException("Project directory $installPath already exists.");
- }
- if (!file_exists(dirname($installPath))) {
- throw new \InvalidArgumentException("Project root " . dirname($installPath) . " does not exist.");
+ $fs = new FileSystem();
+
+ if ($fs->directoryExists($installPath)) {
+ if (!$fs->directoryIsEmpty($installPath)) {
+ throw new \InvalidArgumentException("Project directory \"$installPath\" must be empty.");
+ }
+ } else {
+// if (!file_exists(dirname($installPath))) {
+// throw new \InvalidArgumentException("Project root \"" . dirname($installPath) . "\" does not exist.");
+// }
+
+ mkdir($installPath, 0777, true);
}
- mkdir($installPath, 0777);
+
$this->downloadManager->download($package, $installPath);
}
View
29 src/Composer/Util/Filesystem.php
@@ -74,6 +74,35 @@ public function ensureDirectoryExists($directory)
}
}
+ public function directoryExists($dir)
+ {
+ if (!is_dir($dir)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ public function directoryIsEmpty($dir, $excluding= null)
+ {
+ if (!$this->directoryExists($dir)) {
+ throw new \RuntimeException("Directory \"$dir\" does not exist");
+ }
+
+ $excludeList = array('.', '..');
+ if (null !== $excluding) {
+
+ }
+
+ $dirContent = array_diff(scandir($dir), $excludeList);
+
+ if(count($dirContent) >= 1) {
+ return false;
+ }
+
+ return true;
+ }
+
public function rename($source, $target)
{
if (true === @rename($source, $target)) {

0 comments on commit b9697a1

Please sign in to comment.