From 673dd6312b3bc8cb2b244d3d7f48bc43e12b1c80 Mon Sep 17 00:00:00 2001 From: digitalkaoz Date: Sat, 10 Mar 2012 18:08:36 +0100 Subject: [PATCH 1/3] fluent api for installer options --- src/Composer/Command/CreateProjectCommand.php | 4 +- src/Composer/Command/InstallCommand.php | 18 ++- src/Composer/Command/UpdateCommand.php | 20 +-- src/Composer/Installer.php | 149 ++++++++++++++---- 4 files changed, 142 insertions(+), 49 deletions(-) diff --git a/src/Composer/Command/CreateProjectCommand.php b/src/Composer/Command/CreateProjectCommand.php index 5c0039b77..a859ac12a 100644 --- a/src/Composer/Command/CreateProjectCommand.php +++ b/src/Composer/Command/CreateProjectCommand.php @@ -120,7 +120,9 @@ EOT $composer = Factory::create($io); $installer = Installer::create($io, $composer); - $installer->run($preferSource); + $installer + ->setPreferSource($preferSource) + ->run(); } protected function createDownloadManager(IOInterface $io) diff --git a/src/Composer/Command/InstallCommand.php b/src/Composer/Command/InstallCommand.php index d06278e7d..dd18d98f7 100644 --- a/src/Composer/Command/InstallCommand.php +++ b/src/Composer/Command/InstallCommand.php @@ -32,7 +32,7 @@ class InstallCommand extends Command ->setDefinition(array( new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), - new InputOption('no-install-recommends', null, InputOption::VALUE_NONE, 'Do not install recommended packages (ignored when installing from an existing lock file).'), + new InputOption('install-recommends', null, InputOption::VALUE_NONE, 'Also install recommended packages (ignored when installing from an existing lock file).'), new InputOption('install-suggests', null, InputOption::VALUE_NONE, 'Also install suggested packages (ignored when installing from an existing lock file).'), )) ->setHelp(<<getApplication()->getIO(); $install = Installer::create($io, $composer); - return $install->run( - (Boolean) $input->getOption('prefer-source'), - (Boolean) $input->getOption('dry-run'), - (Boolean) $input->getOption('verbose'), - (Boolean) $input->getOption('no-install-recommends'), - (Boolean) $input->getOption('install-suggests') - ); + $install + ->setDryRun($input->getOption('dry-run')) + ->setVerbose($input->getOption('verbose')) + ->setPreferSource($input->getOption('prefer-source')) + ->setInstallRecommends($input->getOption('install-recommends')) + ->setInstallSuggests($input->getOption('install-suggests')) + ; + + return $install->run(); } } diff --git a/src/Composer/Command/UpdateCommand.php b/src/Composer/Command/UpdateCommand.php index 69640bd3d..c1b75b5e9 100644 --- a/src/Composer/Command/UpdateCommand.php +++ b/src/Composer/Command/UpdateCommand.php @@ -31,7 +31,7 @@ class UpdateCommand extends Command ->setDefinition(array( new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), - new InputOption('no-install-recommends', null, InputOption::VALUE_NONE, 'Do not install recommended packages.'), + new InputOption('install-recommends', null, InputOption::VALUE_NONE, 'Also install recommended packages.'), new InputOption('install-suggests', null, InputOption::VALUE_NONE, 'Also install suggested packages.'), )) ->setHelp(<<run( - (Boolean)$input->getOption('prefer-source'), - (Boolean)$input->getOption('dry-run'), - (Boolean)$input->getOption('verbose'), - (Boolean)$input->getOption('no-install-recommends'), - (Boolean)$input->getOption('install-suggests'), - true - ); + $install + ->setDryRun($input->getOption('dry-run')) + ->setVerbose($input->getOption('verbose')) + ->setPreferSource($input->getOption('prefer-source')) + ->setInstallRecommends($input->getOption('install-recommends')) + ->setInstallSuggests($input->getOption('install-suggests')) + ->setUpdate(true) + ; + + return $install->run(); } } diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index f6fb70c1d..ece3c5fdb 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -75,6 +75,18 @@ class Installer */ protected $eventDispatcher; + protected $preferSource; + protected $dryRun; + protected $verbose; + protected $installRecommends; + protected $installSuggests; + protected $update; + + /** + * @var RepositoryInterface + */ + protected $additionalInstalledRepository; + /** * Constructor * @@ -99,37 +111,27 @@ class Installer /** * Run installation (or update) - * - * @param Boolean $preferSource - * @param Boolean $dryRun - * @param Boolean $verbose - * @param Boolean $noInstallRecommends - * @param Boolean $installSuggests - * @param Boolean $update - * @param RepositoryInterface $additionalInstalledRepository */ - public function run($preferSource = false, $dryRun = false, $verbose = false, $noInstallRecommends = false, $installSuggests = false, $update = false, RepositoryInterface $additionalInstalledRepository = null) + public function run() { - if ($dryRun) { - $verbose = true; + if ($this->dryRun) { + $this->verbose = true; } - if ($preferSource) { + if ($this->preferSource) { $this->downloadManager->setPreferSource(true); } - $this->repositoryManager = $this->repositoryManager; - // create local repo, this contains all packages that are installed in the local project $localRepo = $this->repositoryManager->getLocalRepository(); // create installed repo, this contains all local packages + platform packages (php & extensions) $installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository())); - if ($additionalInstalledRepository) { - $installedRepo->addRepository($additionalInstalledRepository); + if ($this->additionalInstalledRepository) { + $installedRepo->addRepository($this->additionalInstalledRepository); } // prepare aliased packages - if (!$update && $this->locker->isLocked()) { + if (!$this->update && $this->locker->isLocked()) { $aliases = $this->locker->getAliases(); } else { $aliases = $this->package->getAliases(); @@ -152,20 +154,20 @@ class Installer } // dispatch pre event - if (!$dryRun) { - $eventName = $update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; + if (!$this->dryRun) { + $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; $this->eventDispatcher->dispatchCommandEvent($eventName); } // creating requirements request $installFromLock = false; $request = new Request($pool); - if ($update) { + if ($this->update) { $this->io->write('Updating dependencies'); $request->updateAll(); - $links = $this->collectLinks($noInstallRecommends, $installSuggests); + $links = $this->collectLinks(); foreach ($links as $link) { $request->install($link->getTarget(), $link->getConstraint()); @@ -192,7 +194,7 @@ class Installer } else { $this->io->write('Installing dependencies'); - $links = $this->collectLinks($noInstallRecommends, $installSuggests); + $links = $this->collectLinks(); foreach ($links as $link) { $request->install($link->getTarget(), $link->getConstraint()); @@ -207,7 +209,7 @@ class Installer $operations = $solver->solve($request); // force dev packages to be updated to latest reference on update - if ($update) { + if ($this->update) { foreach ($localRepo->getPackages() as $package) { if ($package instanceof AliasPackage) { $package = $package->getAliasOf(); @@ -249,10 +251,10 @@ class Installer } foreach ($operations as $operation) { - if ($verbose) { + if ($this->verbose) { $this->io->write((string) $operation); } - if (!$dryRun) { + if (!$this->dryRun) { $this->eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType())), $operation); // if installing from lock, restore dev packages' references to their locked state @@ -279,8 +281,8 @@ class Installer } } - if (!$dryRun) { - if ($update || !$this->locker->isLocked()) { + if (!$this->dryRun) { + if ($this->update || !$this->locker->isLocked()) { $this->locker->setLockData($localRepo->getPackages(), $aliases); $this->io->write('Writing lock file'); } @@ -292,20 +294,20 @@ class Installer $generator->dump($localRepo, $this->package, $this->installationManager, $this->installationManager->getVendorPath().'/.composer'); // dispatch post event - $eventName = $update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; + $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; $this->eventDispatcher->dispatchCommandEvent($eventName); } } - private function collectLinks($noInstallRecommends, $installSuggests) + private function collectLinks() { $links = $this->package->getRequires(); - if (!$noInstallRecommends) { + if ($this->installRecommends) { $links = array_merge($links, $this->package->getRecommends()); } - if ($installSuggests) { + if ($this->installSuggests) { $links = array_merge($links, $this->package->getSuggests()); } @@ -334,4 +336,89 @@ class Installer $eventDispatcher ); } + + public function setAdditionalInstalledRepository(RepositoryInterface $additionalInstalledRepository) + { + $this->additionalInstalledRepository = $additionalInstalledRepository; + + return $this; + } + + /** + * wether to run in drymode or not + * + * @param boolean $dryRun + * @return Installer + */ + public function setDryRun($dryRun=true) + { + $this->dryRun = (boolean)$dryRun; + + return $this; + } + + /** + * also install recommend packages + * + * @param boolean $installRecommends + * @return Installer + */ + public function setInstallRecommends($installRecommends=true) + { + $this->installRecommends = (boolean)$installRecommends; + + return $this; + } + + /** + * also install suggested packages + * + * @param boolean $installSuggests + * @return Installer + */ + public function setInstallSuggests($installSuggests=true) + { + $this->installSuggests = (boolean)$installSuggests; + + return $this; + } + + /** + * prefer source installation + * + * @param boolean $preferSource + * @return Installer + */ + public function setPreferSource($preferSource=true) + { + $this->preferSource = (boolean)$preferSource; + + return $this; + } + + /** + * update packages + * + * @param boolean $update + * @return Installer + */ + public function setUpdate($update=true) + { + $this->update = (boolean)$update; + + return $this; + } + + /** + * run in verbose mode + * + * @param boolean $verbose + * @return Installer + */ + public function setVerbose($verbose=true) + { + $this->verbose = (boolean)$verbose; + + return $this; + } } From a4f6314daa437d6f240afabeb0fd31ec6c8bc0ce Mon Sep 17 00:00:00 2001 From: digitalkaoz Date: Sat, 10 Mar 2012 19:56:15 +0100 Subject: [PATCH 2/3] reverted command interfaces for installer refactoring --- src/Composer/Command/InstallCommand.php | 4 ++-- src/Composer/Command/UpdateCommand.php | 4 ++-- src/Composer/Installer.php | 16 ++++++++-------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Composer/Command/InstallCommand.php b/src/Composer/Command/InstallCommand.php index dd18d98f7..7bf90709e 100644 --- a/src/Composer/Command/InstallCommand.php +++ b/src/Composer/Command/InstallCommand.php @@ -32,7 +32,7 @@ class InstallCommand extends Command ->setDefinition(array( new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), - new InputOption('install-recommends', null, InputOption::VALUE_NONE, 'Also install recommended packages (ignored when installing from an existing lock file).'), + new InputOption('no-install-recommends', null, InputOption::VALUE_NONE, 'Do not install recommended packages (ignored when installing from an existing lock file).'), new InputOption('install-suggests', null, InputOption::VALUE_NONE, 'Also install suggested packages (ignored when installing from an existing lock file).'), )) ->setHelp(<<setDryRun($input->getOption('dry-run')) ->setVerbose($input->getOption('verbose')) ->setPreferSource($input->getOption('prefer-source')) - ->setInstallRecommends($input->getOption('install-recommends')) + ->setInstallRecommends(!$input->getOption('no-install-recommends')) ->setInstallSuggests($input->getOption('install-suggests')) ; diff --git a/src/Composer/Command/UpdateCommand.php b/src/Composer/Command/UpdateCommand.php index c1b75b5e9..e7c581174 100644 --- a/src/Composer/Command/UpdateCommand.php +++ b/src/Composer/Command/UpdateCommand.php @@ -31,7 +31,7 @@ class UpdateCommand extends Command ->setDefinition(array( new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), - new InputOption('install-recommends', null, InputOption::VALUE_NONE, 'Also install recommended packages.'), + new InputOption('no-install-recommends', null, InputOption::VALUE_NONE, 'Do not install recommended packages.'), new InputOption('install-suggests', null, InputOption::VALUE_NONE, 'Also install suggested packages.'), )) ->setHelp(<<setDryRun($input->getOption('dry-run')) ->setVerbose($input->getOption('verbose')) ->setPreferSource($input->getOption('prefer-source')) - ->setInstallRecommends($input->getOption('install-recommends')) + ->setInstallRecommends(!$input->getOption('no-install-recommends')) ->setInstallSuggests($input->getOption('install-suggests')) ->setUpdate(true) ; diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index ece3c5fdb..4af678d6d 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -75,12 +75,12 @@ class Installer */ protected $eventDispatcher; - protected $preferSource; - protected $dryRun; - protected $verbose; - protected $installRecommends; - protected $installSuggests; - protected $update; + protected $preferSource = false; + protected $dryRun = false; + protected $verbose = false; + protected $installRecommends = true; + protected $installSuggests = false; + protected $update = false; /** * @var RepositoryInterface @@ -358,9 +358,9 @@ class Installer } /** - * also install recommend packages + * install recommend packages * - * @param boolean $installRecommends + * @param boolean $noInstallRecommends * @return Installer */ public function setInstallRecommends($installRecommends=true) From d502065cb7aa4bc6be9e0485a0e1dbedbf62ba23 Mon Sep 17 00:00:00 2001 From: digitalkaoz Date: Sat, 10 Mar 2012 20:14:54 +0100 Subject: [PATCH 3/3] fixed cs --- src/Composer/Installer.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index 4af678d6d..a0f85576a 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -352,7 +352,7 @@ class Installer */ public function setDryRun($dryRun=true) { - $this->dryRun = (boolean)$dryRun; + $this->dryRun = (boolean) $dryRun; return $this; } @@ -365,7 +365,7 @@ class Installer */ public function setInstallRecommends($installRecommends=true) { - $this->installRecommends = (boolean)$installRecommends; + $this->installRecommends = (boolean) $installRecommends; return $this; } @@ -378,7 +378,7 @@ class Installer */ public function setInstallSuggests($installSuggests=true) { - $this->installSuggests = (boolean)$installSuggests; + $this->installSuggests = (boolean) $installSuggests; return $this; } @@ -391,7 +391,7 @@ class Installer */ public function setPreferSource($preferSource=true) { - $this->preferSource = (boolean)$preferSource; + $this->preferSource = (boolean) $preferSource; return $this; } @@ -404,7 +404,7 @@ class Installer */ public function setUpdate($update=true) { - $this->update = (boolean)$update; + $this->update = (boolean) $update; return $this; } @@ -417,7 +417,7 @@ class Installer */ public function setVerbose($verbose=true) { - $this->verbose = (boolean)$verbose; + $this->verbose = (boolean) $verbose; return $this; }