1
0
Fork 0

Merge pull request #432 from digitalkaoz/issue_428

fluent api for installer options
pull/420/merge
Jordi Boggiano 2012-03-11 08:55:44 -07:00
commit 6b453906f0
4 changed files with 140 additions and 47 deletions

View File

@ -120,7 +120,9 @@ EOT
$composer = Factory::create($io); $composer = Factory::create($io);
$installer = Installer::create($io, $composer); $installer = Installer::create($io, $composer);
$installer->run($preferSource); $installer
->setPreferSource($preferSource)
->run();
} }
protected function createDownloadManager(IOInterface $io) protected function createDownloadManager(IOInterface $io)

View File

@ -53,12 +53,14 @@ EOT
$io = $this->getApplication()->getIO(); $io = $this->getApplication()->getIO();
$install = Installer::create($io, $composer); $install = Installer::create($io, $composer);
return $install->run( $install
(Boolean) $input->getOption('prefer-source'), ->setDryRun($input->getOption('dry-run'))
(Boolean) $input->getOption('dry-run'), ->setVerbose($input->getOption('verbose'))
(Boolean) $input->getOption('verbose'), ->setPreferSource($input->getOption('prefer-source'))
(Boolean) $input->getOption('no-install-recommends'), ->setInstallRecommends(!$input->getOption('no-install-recommends'))
(Boolean) $input->getOption('install-suggests') ->setInstallSuggests($input->getOption('install-suggests'))
); ;
return $install->run();
} }
} }

View File

@ -51,13 +51,15 @@ EOT
$io = $this->getApplication()->getIO(); $io = $this->getApplication()->getIO();
$install = Installer::create($io, $composer); $install = Installer::create($io, $composer);
return $install->run( $install
(Boolean)$input->getOption('prefer-source'), ->setDryRun($input->getOption('dry-run'))
(Boolean)$input->getOption('dry-run'), ->setVerbose($input->getOption('verbose'))
(Boolean)$input->getOption('verbose'), ->setPreferSource($input->getOption('prefer-source'))
(Boolean)$input->getOption('no-install-recommends'), ->setInstallRecommends(!$input->getOption('no-install-recommends'))
(Boolean)$input->getOption('install-suggests'), ->setInstallSuggests($input->getOption('install-suggests'))
true ->setUpdate(true)
); ;
return $install->run();
} }
} }

View File

@ -75,6 +75,18 @@ class Installer
*/ */
protected $eventDispatcher; protected $eventDispatcher;
protected $preferSource = false;
protected $dryRun = false;
protected $verbose = false;
protected $installRecommends = true;
protected $installSuggests = false;
protected $update = false;
/**
* @var RepositoryInterface
*/
protected $additionalInstalledRepository;
/** /**
* Constructor * Constructor
* *
@ -99,37 +111,27 @@ class Installer
/** /**
* Run installation (or update) * 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) { if ($this->dryRun) {
$verbose = true; $this->verbose = true;
} }
if ($preferSource) { if ($this->preferSource) {
$this->downloadManager->setPreferSource(true); $this->downloadManager->setPreferSource(true);
} }
$this->repositoryManager = $this->repositoryManager;
// create local repo, this contains all packages that are installed in the local project // create local repo, this contains all packages that are installed in the local project
$localRepo = $this->repositoryManager->getLocalRepository(); $localRepo = $this->repositoryManager->getLocalRepository();
// create installed repo, this contains all local packages + platform packages (php & extensions) // create installed repo, this contains all local packages + platform packages (php & extensions)
$installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository())); $installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository()));
if ($additionalInstalledRepository) { if ($this->additionalInstalledRepository) {
$installedRepo->addRepository($additionalInstalledRepository); $installedRepo->addRepository($this->additionalInstalledRepository);
} }
// prepare aliased packages // prepare aliased packages
if (!$update && $this->locker->isLocked()) { if (!$this->update && $this->locker->isLocked()) {
$aliases = $this->locker->getAliases(); $aliases = $this->locker->getAliases();
} else { } else {
$aliases = $this->package->getAliases(); $aliases = $this->package->getAliases();
@ -152,20 +154,20 @@ class Installer
} }
// dispatch pre event // dispatch pre event
if (!$dryRun) { if (!$this->dryRun) {
$eventName = $update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
$this->eventDispatcher->dispatchCommandEvent($eventName); $this->eventDispatcher->dispatchCommandEvent($eventName);
} }
// creating requirements request // creating requirements request
$installFromLock = false; $installFromLock = false;
$request = new Request($pool); $request = new Request($pool);
if ($update) { if ($this->update) {
$this->io->write('<info>Updating dependencies</info>'); $this->io->write('<info>Updating dependencies</info>');
$request->updateAll(); $request->updateAll();
$links = $this->collectLinks($noInstallRecommends, $installSuggests); $links = $this->collectLinks();
foreach ($links as $link) { foreach ($links as $link) {
$request->install($link->getTarget(), $link->getConstraint()); $request->install($link->getTarget(), $link->getConstraint());
@ -192,7 +194,7 @@ class Installer
} else { } else {
$this->io->write('<info>Installing dependencies</info>'); $this->io->write('<info>Installing dependencies</info>');
$links = $this->collectLinks($noInstallRecommends, $installSuggests); $links = $this->collectLinks();
foreach ($links as $link) { foreach ($links as $link) {
$request->install($link->getTarget(), $link->getConstraint()); $request->install($link->getTarget(), $link->getConstraint());
@ -207,7 +209,7 @@ class Installer
$operations = $solver->solve($request); $operations = $solver->solve($request);
// force dev packages to be updated to latest reference on update // force dev packages to be updated to latest reference on update
if ($update) { if ($this->update) {
foreach ($localRepo->getPackages() as $package) { foreach ($localRepo->getPackages() as $package) {
if ($package instanceof AliasPackage) { if ($package instanceof AliasPackage) {
$package = $package->getAliasOf(); $package = $package->getAliasOf();
@ -249,10 +251,10 @@ class Installer
} }
foreach ($operations as $operation) { foreach ($operations as $operation) {
if ($verbose) { if ($this->verbose) {
$this->io->write((string) $operation); $this->io->write((string) $operation);
} }
if (!$dryRun) { if (!$this->dryRun) {
$this->eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType())), $operation); $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 // if installing from lock, restore dev packages' references to their locked state
@ -279,8 +281,8 @@ class Installer
} }
} }
if (!$dryRun) { if (!$this->dryRun) {
if ($update || !$this->locker->isLocked()) { if ($this->update || !$this->locker->isLocked()) {
$this->locker->setLockData($localRepo->getPackages(), $aliases); $this->locker->setLockData($localRepo->getPackages(), $aliases);
$this->io->write('<info>Writing lock file</info>'); $this->io->write('<info>Writing lock file</info>');
} }
@ -292,20 +294,20 @@ class Installer
$generator->dump($localRepo, $this->package, $this->installationManager, $this->installationManager->getVendorPath().'/.composer'); $generator->dump($localRepo, $this->package, $this->installationManager, $this->installationManager->getVendorPath().'/.composer');
// dispatch post event // 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); $this->eventDispatcher->dispatchCommandEvent($eventName);
} }
} }
private function collectLinks($noInstallRecommends, $installSuggests) private function collectLinks()
{ {
$links = $this->package->getRequires(); $links = $this->package->getRequires();
if (!$noInstallRecommends) { if ($this->installRecommends) {
$links = array_merge($links, $this->package->getRecommends()); $links = array_merge($links, $this->package->getRecommends());
} }
if ($installSuggests) { if ($this->installSuggests) {
$links = array_merge($links, $this->package->getSuggests()); $links = array_merge($links, $this->package->getSuggests());
} }
@ -334,4 +336,89 @@ class Installer
$eventDispatcher $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;
}
/**
* install recommend packages
*
* @param boolean $noInstallRecommends
* @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;
}
} }