From 6caa77fbbfa09037a2591f63852960ec9c8a8b37 Mon Sep 17 00:00:00 2001 From: everzet Date: Sat, 17 Sep 2011 15:55:28 +0300 Subject: [PATCH] installer refactoring --- src/Composer/Command/Command.php | 21 ++++- .../Console/Package/VerboseManager.php | 40 --------- src/Composer/Installer/InstallerInterface.php | 24 +++-- src/Composer/Installer/LibraryInstaller.php | 38 +++++--- src/Composer/Installer/Operation.php | 57 ++++++++++++ src/Composer/Package/Manager.php | 87 ------------------- 6 files changed, 115 insertions(+), 152 deletions(-) delete mode 100644 src/Composer/Console/Package/VerboseManager.php create mode 100644 src/Composer/Installer/Operation.php delete mode 100644 src/Composer/Package/Manager.php diff --git a/src/Composer/Command/Command.php b/src/Composer/Command/Command.php index 2fb47285c..bde9bfaf1 100644 --- a/src/Composer/Command/Command.php +++ b/src/Composer/Command/Command.php @@ -13,11 +13,17 @@ namespace Composer\Command; use Symfony\Component\Console\Command\Command as BaseCommand; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Composer\DependencyResolver\Request; +use Composer\DependencyResolver\Solver; +use Composer\Installer\Operation; /** * Base class for Composer commands * * @author Ryan Weaver + * @authro Konstantin Kudryashov */ abstract class Command extends BaseCommand { @@ -28,4 +34,17 @@ abstract class Command extends BaseCommand { return $this->getApplication()->getComposer(); } -} \ No newline at end of file + + protected function solveDependencies(Request $request, Solver $solver) + { + $operations = array(); + foreach ($solver->solve($request) as $task) { + $installer = $this->getComposer()->getInstaller($task['package']->getType()); + $operation = new Operation($installer, $task['job'], $task['package']); + + $operations[] = $operation; + } + + return $operations; + } +} diff --git a/src/Composer/Console/Package/VerboseManager.php b/src/Composer/Console/Package/VerboseManager.php deleted file mode 100644 index 7562ec57d..000000000 --- a/src/Composer/Console/Package/VerboseManager.php +++ /dev/null @@ -1,40 +0,0 @@ -composer = $composer; - } - - public function install(PackageInterface $package) - { - $this->output->writeln('> Installing '.$package->getName()); - - parent::install($package); - } - - public function update(PackageInterface $package) - { - $this->output->writeln('> Updating '.$package->getName()); - - parent::update($package); - } - - public function remove(PackageInterface $package) - { - $this->output->writeln('> Removing '.$package->getName()); - - parent::remove($package); - } -} diff --git a/src/Composer/Installer/InstallerInterface.php b/src/Composer/Installer/InstallerInterface.php index 6a3ac6fbd..fc370b18f 100644 --- a/src/Composer/Installer/InstallerInterface.php +++ b/src/Composer/Installer/InstallerInterface.php @@ -12,22 +12,18 @@ namespace Composer\Installer; -use Composer\Downloader\DownloaderInterface; use Composer\Package\PackageInterface; +use Composer\Composer; /** - * Package Installer - * - * @author Kirill chEbba Chebunin - */ -interface InstallerInterface + * @author Konstantin Kudryashov + */ +class InstallerInterface { - /** - * Install package - * - * @param PackageInterface $package - * @param DownloaderInterface $downloader - * @param string $type - */ - function install(PackageInterface $package, DownloaderInterface $downloader, $type); + function setComposer(Composer $composer); + + function isInstalled(PackageInterface $package); + function install(PackageInterface $package); + function update(PackageInterface $package); + function remove(PackageInterface $package); } diff --git a/src/Composer/Installer/LibraryInstaller.php b/src/Composer/Installer/LibraryInstaller.php index 1e7ed2583..b2a954ca6 100644 --- a/src/Composer/Installer/LibraryInstaller.php +++ b/src/Composer/Installer/LibraryInstaller.php @@ -14,42 +14,60 @@ namespace Composer\Installer; use Composer\Downloader\DownloaderInterface; use Composer\Package\PackageInterface; +use Composer\Composer; /** * @author Jordi Boggiano */ class LibraryInstaller implements InstallerInterface { - protected $dir; + private $dir; + private $composer; public function __construct($dir = 'vendor') { $this->dir = $dir; } - public function install(PackageInterface $package, DownloaderInterface $downloader, $type) + public function setComposer(Composer $composer) { - if ($type === 'dist') { - $downloader->download($package, $this->dir, $package->getDistUrl(), $package->getDistSha1Checksum()); - } elseif ($type === 'source') { - $downloader->download($package, $this->dir, $package->getSourceUrl()); + $this->composer = $composer; + } + + public function install(PackageInterface $package) + { + if ($package->getDistType()) { + + $this->composer->getDownloader($package->getDistType())->download( + $package, $this->dir, $package->getDistUrl(), $package->getDistSha1Checksum() + ); + + } elseif ($package->getSourceType()) { + + $this->composer->getDownloader($package->getSourceType())->download( + $package, $this->dir, $package->getSourceUrl() + ); + } else { - throw new \InvalidArgumentException('Type must be one of (dist, source), '.$type.' given.'); + throw new \InvalidArgumentException( + 'Type must be one of (dist, source), '.$type.' given.' + ); } + return true; } - public function isInstalled(PackageInterface $package, $downloader, $type) + public function isInstalled(PackageInterface $package) { // TODO: implement installation check } - public function update(PackageInterface $package, $downloader, $type) + public function update(PackageInterface $package) { // TODO: implement package update } - public function remove(PackageInterface $package, $downloader, $type) + public function remove(PackageInterface $package) { // TODO: implement package removal } diff --git a/src/Composer/Installer/Operation.php b/src/Composer/Installer/Operation.php new file mode 100644 index 000000000..b442ff743 --- /dev/null +++ b/src/Composer/Installer/Operation.php @@ -0,0 +1,57 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Installer; + +use Composer\Installer\InstallerInterface; +use Composer\Package\PackageInterface; + +/** + * Installer operation command + * + * @author Konstantin Kudryashov + */ +class Operation +{ + private $installer; + private $type; + private $package; + + public function __construct(InstallerInterface $installer, $type, PackageInterface $package) + { + $type = strtolower($type); + if (!in_array($type, array('install', 'update', 'remove'))) { + throw new \UnexpectedValueException('Unhandled operation type: ' . $type); + } + + $this->installer = $installer; + $this->type = $type; + $this->package = $package; + } + + public function getType() + { + return $this->type; + } + + public function getPackage() + { + return $this->package; + } + + public function execute() + { + $method = $this->getType(); + + return $this->installer->$method($this->getPackage()); + } +} diff --git a/src/Composer/Package/Manager.php b/src/Composer/Package/Manager.php deleted file mode 100644 index db0f73ebb..000000000 --- a/src/Composer/Package/Manager.php +++ /dev/null @@ -1,87 +0,0 @@ -composer = $composer; - } - - public function isInstalled(PackageInterface $package) - { - $installer = $this->composer->getInstaller($package->getType()); - $downloader = $this->getDownloaderForPackage($package); - $packageType = $this->getTypeForPackage($package); - - return $installer->isInstalled($package, $downloader, $packageType); - } - - public function install(PackageInterface $package) - { - $installer = $this->composer->getInstaller($package->getType()); - $downloader = $this->getDownloaderForPackage($package); - $packageType = $this->getTypeForPackage($package); - - if (!$installer->install($package, $downloader, $packageType)) { - throw new \LogicException($package->getName().' could not be installed.'); - } - } - - public function update(PackageInterface $package) - { - $installer = $this->composer->getInstaller($package->getType()); - $downloader = $this->getDownloaderForPackage($package); - $packageType = $this->getTypeForPackage($package); - - if (!$installer->update($package, $downloader, $packageType)) { - throw new \LogicException($package->getName().' could not be updated.'); - } - } - - public function remove(PackageInterface $package) - { - $installer = $this->composer->getInstaller($package->getType()); - $downloader = $this->getDownloaderForPackage($package); - $packageType = $this->getTypeForPackage($package); - - if (!$installer->remove($package, $downloader, $packageType)) { - throw new \LogicException($package->getName().' could not be removed.'); - } - } - - private function getDownloaderForPackage(PackageInterface $package) - { - if ($package->getDistType()) { - $downloader = $this->composer->getDownloader($package->getDistType); - } elseif ($package->getSourceType()) { - $downloader = $this->copmoser->getDownloader($package->getSourceType()); - } else { - throw new \UnexpectedValueException( - 'Package '.$package->getName().' has no source or dist URL.' - ); - } - - return $downloader; - } - - private function getTypeForPackage(PackageInterface $package) - { - if ($package->getDistType()) { - $type = 'dist'; - } elseif ($package->getSourceType()) { - $type = 'source'; - } else { - throw new \UnexpectedValueException( - 'Package '.$package->getName().' has no source or dist URL.' - ); - } - - return $type; - } -}