fluent api for installer options

main
digitalkaoz 12 years ago
parent edf93f1fcc
commit 673dd6312b

@ -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)

@ -32,7 +32,7 @@ class InstallCommand extends Command
->setDefinition(array( ->setDefinition(array(
new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), 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('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).'), new InputOption('install-suggests', null, InputOption::VALUE_NONE, 'Also install suggested packages (ignored when installing from an existing lock file).'),
)) ))
->setHelp(<<<EOT ->setHelp(<<<EOT
@ -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('install-recommends'))
(Boolean) $input->getOption('install-suggests') ->setInstallSuggests($input->getOption('install-suggests'))
); ;
return $install->run();
} }
} }

@ -31,7 +31,7 @@ class UpdateCommand extends Command
->setDefinition(array( ->setDefinition(array(
new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), 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('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.'), new InputOption('install-suggests', null, InputOption::VALUE_NONE, 'Also install suggested packages.'),
)) ))
->setHelp(<<<EOT ->setHelp(<<<EOT
@ -53,13 +53,15 @@ EOT
$eventDispatcher = new EventDispatcher($composer, $io); $eventDispatcher = new EventDispatcher($composer, $io);
$install = Installer::create($io, $composer, $eventDispatcher); $install = Installer::create($io, $composer, $eventDispatcher);
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('install-recommends'))
(Boolean)$input->getOption('install-suggests'), ->setInstallSuggests($input->getOption('install-suggests'))
true ->setUpdate(true)
); ;
return $install->run();
} }
} }

@ -75,6 +75,18 @@ class Installer
*/ */
protected $eventDispatcher; protected $eventDispatcher;
protected $preferSource;
protected $dryRun;
protected $verbose;
protected $installRecommends;
protected $installSuggests;
protected $update;
/**
* @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;
}
/**
* 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;
}
} }

Loading…
Cancel
Save