diff --git a/src/Composer/Downloader/DownloaderInterface.php b/src/Composer/Downloader/DownloaderInterface.php index d399ebf88..4416ac46e 100644 --- a/src/Composer/Downloader/DownloaderInterface.php +++ b/src/Composer/Downloader/DownloaderInterface.php @@ -20,4 +20,5 @@ use Composer\Package\PackageInterface; interface DownloaderInterface { function download(PackageInterface $package, $path, $url, $checksum = null); + function isDownloaded(PackageInterface $package, $path); } diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index 90fa49baa..bb22bc0be 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -28,18 +28,17 @@ class GitDownloader implements DownloaderInterface public function download(PackageInterface $package, $path, $url, $checksum = null) { - if (!is_dir($path)) { - if (file_exists($path)) { - throw new \UnexpectedValueException($path.' exists and is not a directory.'); - } - if (!mkdir($path, 0777, true)) { - throw new \UnexpectedValueException($path.' does not exist and could not be created.'); - } - } if ($this->clone) { system('git clone '.escapeshellarg($url).' -b master '.escapeshellarg($path.'/'.$package->getName())); } else { system('git archive --format=tar --prefix='.escapeshellarg($package->getName()).' --remote='.escapeshellarg($url).' master | tar -xf -'); } } + + public function isDownloaded(PackageInterface $package, $path) + { + $targetPath = $path . '/' . $package->getName(); + + return is_dir($targetPath); + } } diff --git a/src/Composer/Downloader/PearDownloader.php b/src/Composer/Downloader/PearDownloader.php index 0f4c86289..c93519b1c 100644 --- a/src/Composer/Downloader/PearDownloader.php +++ b/src/Composer/Downloader/PearDownloader.php @@ -66,4 +66,11 @@ class PearDownloader implements DownloaderInterface } chdir($cwd); } + + public function isDownloaded(PackageInterface $package, $path) + { + $targetPath = $path . '/' . $package->getName(); + + return is_dir($targetPath); + } } diff --git a/src/Composer/Downloader/ZipDownloader.php b/src/Composer/Downloader/ZipDownloader.php index 94a7eac0e..098ba151a 100644 --- a/src/Composer/Downloader/ZipDownloader.php +++ b/src/Composer/Downloader/ZipDownloader.php @@ -73,4 +73,11 @@ class ZipDownloader implements DownloaderInterface throw new \UnexpectedValueException($zipName.' is not a valid zip archive, got error code '.$retval); } } + + public function isDownloaded(PackageInterface $package, $path) + { + $targetPath = $path . '/' . $package->getName(); + + return is_dir($targetPath); + } } diff --git a/src/Composer/Installer/LibraryInstaller.php b/src/Composer/Installer/LibraryInstaller.php index b2a954ca6..e45aa6586 100644 --- a/src/Composer/Installer/LibraryInstaller.php +++ b/src/Composer/Installer/LibraryInstaller.php @@ -18,15 +18,18 @@ use Composer\Composer; /** * @author Jordi Boggiano + * @author Konstantin Kudryashov */ class LibraryInstaller implements InstallerInterface { private $dir; private $composer; + private $preferSource; - public function __construct($dir = 'vendor') + public function __construct($dir = 'vendor', $preferSource = false) { $this->dir = $dir; + $this->preferSource = $preferSource; } public function setComposer(Composer $composer) @@ -36,39 +39,52 @@ class LibraryInstaller implements InstallerInterface public function install(PackageInterface $package) { - if ($package->getDistType()) { + if (!is_dir($this->dir)) { + if (file_exists($this->dir)) { + throw new \UnexpectedValueException($this->dir.' exists and is not a directory.'); + } + if (!mkdir($this->dir, 0777, true)) { + throw new \UnexpectedValueException($this->path.' does not exist and could not be created.'); + } + } - $this->composer->getDownloader($package->getDistType())->download( + if (!($this->preferSource && $package->getSourceType()) && $package->getDistType()) { + $downloader = $this->composer->getDownloader($package->getDistType()); + + return $downloader->download( $package, $this->dir, $package->getDistUrl(), $package->getDistSha1Checksum() ); + } - } elseif ($package->getSourceType()) { + if ($package->getSourceType()) { + $downloader = $this->composer->getDownloader($package->getSourceType()); - $this->composer->getDownloader($package->getSourceType())->download( + return $downloader->download( $package, $this->dir, $package->getSourceUrl() ); - - } else { - throw new \InvalidArgumentException( - 'Type must be one of (dist, source), '.$type.' given.' - ); } - return true; + throw new \InvalidArgumentException('Package should have dist or source specified'); } public function isInstalled(PackageInterface $package) { - // TODO: implement installation check - } + if ($package->getSourceType()) { + $downloader = $this->composer->getDownloader($package->getSourceType()); - public function update(PackageInterface $package) - { - // TODO: implement package update - } + if ($downloader->isDownloaded($package, $this->dir)) { + return true; + } + } - public function remove(PackageInterface $package) - { - // TODO: implement package removal + if ($package->getDistType()) { + $downloader = $this->composer->getDownloader($package->getDistType()); + + if ($downloader->isDownloaded($package, $this->dir)) { + return true; + } + } + + return false; } }