123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer\Installer;
- use Composer\Downloader\DownloadManager;
- use Composer\Repository\WritableRepositoryInterface;
- use Composer\DependencyResolver\Operation\OperationInterface;
- use Composer\Package\PackageInterface;
- /**
- * Package installation manager.
- *
- * @author Jordi Boggiano <j.boggiano@seld.be>
- * @author Konstantin Kudryashov <ever.zet@gmail.com>
- */
- class LibraryInstaller implements InstallerInterface
- {
- private $directory;
- private $downloadManager;
- private $repository;
- /**
- * Initializes library installer.
- *
- * @param string $dir relative path for packages home
- * @param DownloadManager $dm download manager
- * @param WritableRepositoryInterface $repository repository controller
- */
- public function __construct($directory, DownloadManager $dm, WritableRepositoryInterface $repository)
- {
- $this->directory = $directory;
- $this->downloadManager = $dm;
- if (!is_dir($this->directory)) {
- if (file_exists($this->directory)) {
- throw new \UnexpectedValueException(
- $this->directory.' exists and is not a directory.'
- );
- }
- if (!mkdir($this->directory, 0777, true)) {
- throw new \UnexpectedValueException(
- $this->directory.' does not exist and could not be created.'
- );
- }
- }
- $this->repository = $repository;
- }
- /**
- * {@inheritDoc}
- */
- public function isInstalled(PackageInterface $package)
- {
- return $this->repository->hasPackage($package);
- }
- /**
- * {@inheritDoc}
- */
- public function install(PackageInterface $package)
- {
- $downloadPath = $this->getInstallPath($package);
- $this->downloadManager->download($package, $downloadPath);
- $this->repository->addPackage(clone $package);
- }
- /**
- * {@inheritDoc}
- */
- public function update(PackageInterface $initial, PackageInterface $target)
- {
- if (!$this->repository->hasPackage($initial)) {
- throw new \InvalidArgumentException('Package is not installed: '.$initial);
- }
- $downloadPath = $this->getInstallPath($initial);
- $this->downloadManager->update($initial, $target, $downloadPath);
- $this->repository->removePackage($initial);
- $this->repository->addPackage($target);
- }
- /**
- * {@inheritDoc}
- */
- public function uninstall(PackageInterface $package)
- {
- if (!$this->repository->hasPackage($package)) {
- throw new \InvalidArgumentException('Package is not installed: '.$package);
- }
- $downloadPath = $this->getInstallPath($package);
- $this->downloadManager->remove($package, $downloadPath);
- $this->repository->removePackage($package);
- }
- /**
- * {@inheritDoc}
- */
- public function getInstallPath(PackageInterface $package)
- {
- if (null === $package->getTargetDir()) {
- return ($this->directory ? $this->directory.'/' : '').$package->getName();
- }
- return ($this->directory ? $this->directory.'/' : '').$package->getTargetDir();
- }
- }
|