From d1dea702c9be6e07e07ded85bd4b1e4562bfd39d Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Fri, 2 Mar 2012 21:35:40 -0800 Subject: [PATCH 1/6] Moving install related code to Composer\Install --- src/Composer/Command/InstallCommand.php | 229 +-------------------- src/Composer/Command/UpdateCommand.php | 12 +- src/Composer/Install.php | 252 ++++++++++++++++++++++++ 3 files changed, 258 insertions(+), 235 deletions(-) create mode 100644 src/Composer/Install.php diff --git a/src/Composer/Command/InstallCommand.php b/src/Composer/Command/InstallCommand.php index 6b6120bb8..4a0070abd 100644 --- a/src/Composer/Command/InstallCommand.php +++ b/src/Composer/Command/InstallCommand.php @@ -12,30 +12,11 @@ namespace Composer\Command; -use Composer\Script\ScriptEvents; +use Composer\Install; use Composer\Script\EventDispatcher; -use Composer\Autoload\AutoloadGenerator; -use Composer\Composer; -use Composer\DependencyResolver; -use Composer\DependencyResolver\Pool; -use Composer\DependencyResolver\Request; -use Composer\DependencyResolver\Operation; -use Composer\Package\AliasPackage; -use Composer\Package\MemoryPackage; -use Composer\Package\Link; -use Composer\Package\LinkConstraint\VersionConstraint; -use Composer\Package\PackageInterface; -use Composer\Repository\ArrayRepository; -use Composer\Repository\CompositeRepository; -use Composer\Repository\PlatformRepository; -use Composer\Repository\RepositoryInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Composer\DependencyResolver\Operation\InstallOperation; -use Composer\DependencyResolver\Operation\UpdateOperation; -use Composer\DependencyResolver\Solver; -use Composer\IO\IOInterface; /** * @author Jordi Boggiano @@ -72,8 +53,9 @@ EOT $composer = $this->getComposer(); $io = $this->getApplication()->getIO(); $eventDispatcher = new EventDispatcher($composer, $io); + $install = new Install; - return $this->install( + return $install->run( $io, $composer, $eventDispatcher, @@ -84,209 +66,4 @@ EOT (Boolean)$input->getOption('install-suggests') ); } - - public function install(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher, $preferSource = false, $dryRun = false, $verbose = false, $noInstallRecommends = false, $installSuggests = false, $update = false, RepositoryInterface $additionalInstalledRepository = null) - { - if ($dryRun) { - $verbose = true; - } - - if ($preferSource) { - $composer->getDownloadManager()->setPreferSource(true); - } - - $repoManager = $composer->getRepositoryManager(); - - // create local repo, this contains all packages that are installed in the local project - $localRepo = $repoManager->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); - } - - // prepare aliased packages - if (!$update && $composer->getLocker()->isLocked()) { - $aliases = $composer->getLocker()->getAliases(); - } else { - $aliases = $composer->getPackage()->getAliases(); - } - foreach ($aliases as $alias) { - foreach ($repoManager->findPackages($alias['package'], $alias['version']) as $package) { - $package->getRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias'])); - } - foreach ($repoManager->getLocalRepository()->findPackages($alias['package'], $alias['version']) as $package) { - $repoManager->getLocalRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias'])); - $repoManager->getLocalRepository()->removePackage($package); - } - } - - // creating repository pool - $pool = new Pool; - $pool->addRepository($installedRepo); - foreach ($repoManager->getRepositories() as $repository) { - $pool->addRepository($repository); - } - - // dispatch pre event - if (!$dryRun) { - $eventName = $update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; - $eventDispatcher->dispatchCommandEvent($eventName); - } - - // creating requirements request - $installFromLock = false; - $request = new Request($pool); - if ($update) { - $io->write('Updating dependencies'); - - $request->updateAll(); - - $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests); - - foreach ($links as $link) { - $request->install($link->getTarget(), $link->getConstraint()); - } - } elseif ($composer->getLocker()->isLocked()) { - $installFromLock = true; - $io->write('Installing from lock file'); - - if (!$composer->getLocker()->isFresh()) { - $io->write('Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies'); - } - - foreach ($composer->getLocker()->getLockedPackages() as $package) { - $version = $package->getVersion(); - foreach ($aliases as $alias) { - if ($alias['package'] === $package->getName() && $alias['version'] === $package->getVersion()) { - $version = $alias['alias']; - break; - } - } - $constraint = new VersionConstraint('=', $version); - $request->install($package->getName(), $constraint); - } - } else { - $io->write('Installing dependencies'); - - $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests); - - foreach ($links as $link) { - $request->install($link->getTarget(), $link->getConstraint()); - } - } - - // prepare solver - $installationManager = $composer->getInstallationManager(); - $policy = new DependencyResolver\DefaultPolicy(); - $solver = new DependencyResolver\Solver($policy, $pool, $installedRepo); - - // solve dependencies - $operations = $solver->solve($request); - - // force dev packages to be updated to latest reference on update - if ($update) { - foreach ($localRepo->getPackages() as $package) { - if ($package instanceof AliasPackage) { - $package = $package->getAliasOf(); - } - - // skip non-dev packages - if (!$package->isDev()) { - continue; - } - - // skip packages that will be updated/uninstalled - foreach ($operations as $operation) { - if (('update' === $operation->getJobType() && $package === $operation->getInitialPackage()) - || ('uninstall' === $operation->getJobType() && $package === $operation->getPackage()) - ) { - continue 2; - } - } - - // force update - $newPackage = $repoManager->findPackage($package->getName(), $package->getVersion()); - if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) { - $operations[] = new UpdateOperation($package, $newPackage); - } - } - } - - // anti-alias local repository to allow updates to work fine - foreach ($repoManager->getLocalRepository()->getPackages() as $package) { - if ($package instanceof AliasPackage) { - $repoManager->getLocalRepository()->addPackage(clone $package->getAliasOf()); - $repoManager->getLocalRepository()->removePackage($package); - } - } - - // execute operations - if (!$operations) { - $io->write('Nothing to install/update'); - } - - foreach ($operations as $operation) { - if ($verbose) { - $io->write((string) $operation); - } - if (!$dryRun) { - $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 ($installFromLock) { - $package = null; - if ('update' === $operation->getJobType()) { - $package = $operation->getTargetPackage(); - } elseif ('install' === $operation->getJobType()) { - $package = $operation->getPackage(); - } - if ($package && $package->isDev()) { - $lockData = $composer->getLocker()->getLockData(); - foreach ($lockData['packages'] as $lockedPackage) { - if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) { - $package->setSourceReference($lockedPackage['source-reference']); - break; - } - } - } - } - $installationManager->execute($operation); - - $eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType())), $operation); - } - } - - if (!$dryRun) { - if ($update || !$composer->getLocker()->isLocked()) { - $composer->getLocker()->setLockData($localRepo->getPackages(), $aliases); - $io->write('Writing lock file'); - } - - $localRepo->write(); - - $io->write('Generating autoload files'); - $generator = new AutoloadGenerator; - $generator->dump($localRepo, $composer->getPackage(), $installationManager, $installationManager->getVendorPath().'/.composer'); - - // dispatch post event - $eventName = $update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; - $eventDispatcher->dispatchCommandEvent($eventName); - } - } - - private function collectLinks(PackageInterface $package, $noInstallRecommends, $installSuggests) - { - $links = $package->getRequires(); - - if (!$noInstallRecommends) { - $links = array_merge($links, $package->getRecommends()); - } - - if ($installSuggests) { - $links = array_merge($links, $package->getSuggests()); - } - - return $links; - } } diff --git a/src/Composer/Command/UpdateCommand.php b/src/Composer/Command/UpdateCommand.php index 7e1e8e0c1..4ead94d7e 100644 --- a/src/Composer/Command/UpdateCommand.php +++ b/src/Composer/Command/UpdateCommand.php @@ -12,13 +12,7 @@ namespace Composer\Command; -use Composer\Autoload\AutoloadGenerator; -use Composer\DependencyResolver; -use Composer\DependencyResolver\Pool; -use Composer\DependencyResolver\Request; -use Composer\DependencyResolver\Operation; -use Composer\Package\LinkConstraint\VersionConstraint; -use Composer\Repository\PlatformRepository; +use Composer\Install; use Composer\Script\EventDispatcher; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -54,12 +48,12 @@ EOT protected function execute(InputInterface $input, OutputInterface $output) { - $installCommand = $this->getApplication()->find('install'); $composer = $this->getComposer(); $io = $this->getApplication()->getIO(); $eventDispatcher = new EventDispatcher($composer, $io); + $install = new Install; - return $installCommand->install( + return $install->run( $io, $composer, $eventDispatcher, diff --git a/src/Composer/Install.php b/src/Composer/Install.php new file mode 100644 index 000000000..d88231cd8 --- /dev/null +++ b/src/Composer/Install.php @@ -0,0 +1,252 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer; + +use Composer\Autoload\AutoloadGenerator; +use Composer\DependencyResolver\DefaultPolicy; +use Composer\DependencyResolver\Operation\UpdateOperation; +use Composer\DependencyResolver\Pool; +use Composer\DependencyResolver\Request; +use Composer\DependencyResolver\Solver; +use Composer\IO\IOInterface; +use Composer\Package\AliasPackage; +use Composer\Package\Link; +use Composer\Package\LinkConstraint\VersionConstraint; +use Composer\Package\PackageInterface; +use Composer\Repository\CompositeRepository; +use Composer\Repository\PlatformRepository; +use Composer\Repository\RepositoryInterface; +use Composer\Script\EventDispatcher; +use Composer\Script\ScriptEvents; + +class Install +{ + /** + * Run installation (or update) + * + * @param IOInterface $io + * @param Composer $composer + * @param EventDispatcher $eventDispatcher + * @param bool $preferSource + * @param bool $dryRun + * @param bool $verbose + * @param bool $noInstallRecommends + * @param bool $installSuggests + * @param bool $update + * @param RepositoryInterface $additionalInstalledRepository + */ + public function run(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher, $preferSource = false, $dryRun = false, $verbose = false, $noInstallRecommends = false, $installSuggests = false, $update = false, RepositoryInterface $additionalInstalledRepository = null) + { + if ($dryRun) { + $verbose = true; + } + + if ($preferSource) { + $composer->getDownloadManager()->setPreferSource(true); + } + + $repoManager = $composer->getRepositoryManager(); + + // create local repo, this contains all packages that are installed in the local project + $localRepo = $repoManager->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); + } + + // prepare aliased packages + if (!$update && $composer->getLocker()->isLocked()) { + $aliases = $composer->getLocker()->getAliases(); + } else { + $aliases = $composer->getPackage()->getAliases(); + } + foreach ($aliases as $alias) { + foreach ($repoManager->findPackages($alias['package'], $alias['version']) as $package) { + $package->getRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias'])); + } + foreach ($repoManager->getLocalRepository()->findPackages($alias['package'], $alias['version']) as $package) { + $repoManager->getLocalRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias'])); + $repoManager->getLocalRepository()->removePackage($package); + } + } + + // creating repository pool + $pool = new Pool; + $pool->addRepository($installedRepo); + foreach ($repoManager->getRepositories() as $repository) { + $pool->addRepository($repository); + } + + // dispatch pre event + if (!$dryRun) { + $eventName = $update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; + $eventDispatcher->dispatchCommandEvent($eventName); + } + + // creating requirements request + $installFromLock = false; + $request = new Request($pool); + if ($update) { + $io->write('Updating dependencies'); + + $request->updateAll(); + + $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests); + + foreach ($links as $link) { + $request->install($link->getTarget(), $link->getConstraint()); + } + } elseif ($composer->getLocker()->isLocked()) { + $installFromLock = true; + $io->write('Installing from lock file'); + + if (!$composer->getLocker()->isFresh()) { + $io->write('Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies'); + } + + foreach ($composer->getLocker()->getLockedPackages() as $package) { + $version = $package->getVersion(); + foreach ($aliases as $alias) { + if ($alias['package'] === $package->getName() && $alias['version'] === $package->getVersion()) { + $version = $alias['alias']; + break; + } + } + $constraint = new VersionConstraint('=', $version); + $request->install($package->getName(), $constraint); + } + } else { + $io->write('Installing dependencies'); + + $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests); + + foreach ($links as $link) { + $request->install($link->getTarget(), $link->getConstraint()); + } + } + + // prepare solver + $installationManager = $composer->getInstallationManager(); + $policy = new DependencyResolver\DefaultPolicy(); + $solver = new DependencyResolver\Solver($policy, $pool, $installedRepo); + + // solve dependencies + $operations = $solver->solve($request); + + // force dev packages to be updated to latest reference on update + if ($update) { + foreach ($localRepo->getPackages() as $package) { + if ($package instanceof AliasPackage) { + $package = $package->getAliasOf(); + } + + // skip non-dev packages + if (!$package->isDev()) { + continue; + } + + // skip packages that will be updated/uninstalled + foreach ($operations as $operation) { + if (('update' === $operation->getJobType() && $package === $operation->getInitialPackage()) + || ('uninstall' === $operation->getJobType() && $package === $operation->getPackage()) + ) { + continue 2; + } + } + + // force update + $newPackage = $repoManager->findPackage($package->getName(), $package->getVersion()); + if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) { + $operations[] = new UpdateOperation($package, $newPackage); + } + } + } + + // anti-alias local repository to allow updates to work fine + foreach ($repoManager->getLocalRepository()->getPackages() as $package) { + if ($package instanceof AliasPackage) { + $repoManager->getLocalRepository()->addPackage(clone $package->getAliasOf()); + $repoManager->getLocalRepository()->removePackage($package); + } + } + + // execute operations + if (!$operations) { + $io->write('Nothing to install/update'); + } + + foreach ($operations as $operation) { + if ($verbose) { + $io->write((string) $operation); + } + if (!$dryRun) { + $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 ($installFromLock) { + $package = null; + if ('update' === $operation->getJobType()) { + $package = $operation->getTargetPackage(); + } elseif ('install' === $operation->getJobType()) { + $package = $operation->getPackage(); + } + if ($package && $package->isDev()) { + $lockData = $composer->getLocker()->getLockData(); + foreach ($lockData['packages'] as $lockedPackage) { + if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) { + $package->setSourceReference($lockedPackage['source-reference']); + break; + } + } + } + } + $installationManager->execute($operation); + + $eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType())), $operation); + } + } + + if (!$dryRun) { + if ($update || !$composer->getLocker()->isLocked()) { + $composer->getLocker()->setLockData($localRepo->getPackages(), $aliases); + $io->write('Writing lock file'); + } + + $localRepo->write(); + + $io->write('Generating autoload files'); + $generator = new AutoloadGenerator; + $generator->dump($localRepo, $composer->getPackage(), $installationManager, $installationManager->getVendorPath().'/.composer'); + + // dispatch post event + $eventName = $update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; + $eventDispatcher->dispatchCommandEvent($eventName); + } + } + + private function collectLinks(PackageInterface $package, $noInstallRecommends, $installSuggests) + { + $links = $package->getRequires(); + + if (!$noInstallRecommends) { + $links = array_merge($links, $package->getRecommends()); + } + + if ($installSuggests) { + $links = array_merge($links, $package->getSuggests()); + } + + return $links; + } +} From 9404d8d593d3a052d163aea6ca1e1fcc5b3fbaf1 Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Mon, 5 Mar 2012 15:48:07 -0800 Subject: [PATCH 2/6] Constructor, factory --- src/Composer/Command/InstallCommand.php | 5 +- src/Composer/Command/UpdateCommand.php | 5 +- src/Composer/Install.php | 161 ++++++++++++++++++------ 3 files changed, 125 insertions(+), 46 deletions(-) diff --git a/src/Composer/Command/InstallCommand.php b/src/Composer/Command/InstallCommand.php index 4a0070abd..07b5d480a 100644 --- a/src/Composer/Command/InstallCommand.php +++ b/src/Composer/Command/InstallCommand.php @@ -53,12 +53,9 @@ EOT $composer = $this->getComposer(); $io = $this->getApplication()->getIO(); $eventDispatcher = new EventDispatcher($composer, $io); - $install = new Install; + $install = Install::create($io, $composer, $eventDispatcher); return $install->run( - $io, - $composer, - $eventDispatcher, (Boolean)$input->getOption('prefer-source'), (Boolean)$input->getOption('dry-run'), (Boolean)$input->getOption('verbose'), diff --git a/src/Composer/Command/UpdateCommand.php b/src/Composer/Command/UpdateCommand.php index 4ead94d7e..f07f6df20 100644 --- a/src/Composer/Command/UpdateCommand.php +++ b/src/Composer/Command/UpdateCommand.php @@ -51,12 +51,9 @@ EOT $composer = $this->getComposer(); $io = $this->getApplication()->getIO(); $eventDispatcher = new EventDispatcher($composer, $io); - $install = new Install; + $install = Install::create($io, $composer, $eventDispatcher); return $install->run( - $io, - $composer, - $eventDispatcher, (Boolean)$input->getOption('prefer-source'), (Boolean)$input->getOption('dry-run'), (Boolean)$input->getOption('verbose'), diff --git a/src/Composer/Install.php b/src/Composer/Install.php index d88231cd8..ff1d658b6 100644 --- a/src/Composer/Install.php +++ b/src/Composer/Install.php @@ -18,25 +18,90 @@ use Composer\DependencyResolver\Operation\UpdateOperation; use Composer\DependencyResolver\Pool; use Composer\DependencyResolver\Request; use Composer\DependencyResolver\Solver; +use Composer\Downloader\DownloadManager; +use Composer\Installer\InstallationManager; use Composer\IO\IOInterface; use Composer\Package\AliasPackage; use Composer\Package\Link; use Composer\Package\LinkConstraint\VersionConstraint; +use Composer\Package\Locker; use Composer\Package\PackageInterface; use Composer\Repository\CompositeRepository; use Composer\Repository\PlatformRepository; use Composer\Repository\RepositoryInterface; +use Composer\Repository\RepositoryManager; use Composer\Script\EventDispatcher; use Composer\Script\ScriptEvents; class Install { + /** + * + * @var IOInterface + */ + protected $io; + + /** + * + * @var PackageInterface + */ + protected $package; + + /** + * + * @var DownloadManager + */ + protected $downloadManager; + + /** + * + * @var RepositoryManager + */ + protected $repositoryManager; + + /** + * + * @var Locker + */ + protected $locker; + + /** + * + * @var InstallationManager + */ + protected $installationManager; + + /** + * + * @var EventDispatcher + */ + protected $eventDispatcher; + + /** + * Constructor + * + * @param IOInterface $io + * @param PackageInterface $package + * @param DownloadManager $downloadManager + * @param RepositoryManager $repositoryManager + * @param Locker $locker + * @param InstallationManager $installationManager + * @param EventDispatcher $eventDispatcher + */ + public function __construct(IOInterface $io, PackageInterface $package, DownloadManager $downloadManager, RepositoryManager $repositoryManager, Locker $locker, InstallationManager $installationManager, EventDispatcher $eventDispatcher) + { + $this->io = $io; + $this->package = $package; + $this->downloadManager = $downloadManager; + $this->repositoryManager = $repositoryManager; + $this->locker = $locker; + $this->installationManager = $installationManager; + $this->eventDispatcher = $eventDispatcher; + } + /** * Run installation (or update) * - * @param IOInterface $io - * @param Composer $composer - * @param EventDispatcher $eventDispatcher * @param bool $preferSource * @param bool $dryRun * @param bool $verbose @@ -45,17 +110,17 @@ class Install * @param bool $update * @param RepositoryInterface $additionalInstalledRepository */ - public function run(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher, $preferSource = false, $dryRun = false, $verbose = false, $noInstallRecommends = false, $installSuggests = false, $update = false, RepositoryInterface $additionalInstalledRepository = null) + public function run($preferSource = false, $dryRun = false, $verbose = false, $noInstallRecommends = false, $installSuggests = false, $update = false, RepositoryInterface $additionalInstalledRepository = null) { if ($dryRun) { $verbose = true; } if ($preferSource) { - $composer->getDownloadManager()->setPreferSource(true); + $this->downloadManager->setPreferSource(true); } - $repoManager = $composer->getRepositoryManager(); + $repoManager = $this->repositoryManager; // create local repo, this contains all packages that are installed in the local project $localRepo = $repoManager->getLocalRepository(); @@ -66,10 +131,10 @@ class Install } // prepare aliased packages - if (!$update && $composer->getLocker()->isLocked()) { - $aliases = $composer->getLocker()->getAliases(); + if (!$update && $this->locker->isLocked()) { + $aliases = $this->locker->getAliases(); } else { - $aliases = $composer->getPackage()->getAliases(); + $aliases = $this->package->getAliases(); } foreach ($aliases as $alias) { foreach ($repoManager->findPackages($alias['package'], $alias['version']) as $package) { @@ -91,31 +156,31 @@ class Install // dispatch pre event if (!$dryRun) { $eventName = $update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; - $eventDispatcher->dispatchCommandEvent($eventName); + $this->eventDispatcher->dispatchCommandEvent($eventName); } // creating requirements request $installFromLock = false; $request = new Request($pool); if ($update) { - $io->write('Updating dependencies'); + $this->io->write('Updating dependencies'); $request->updateAll(); - $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests); + $links = $this->collectLinks($noInstallRecommends, $installSuggests); foreach ($links as $link) { $request->install($link->getTarget(), $link->getConstraint()); } - } elseif ($composer->getLocker()->isLocked()) { + } elseif ($this->locker->isLocked()) { $installFromLock = true; - $io->write('Installing from lock file'); + $this->io->write('Installing from lock file'); - if (!$composer->getLocker()->isFresh()) { - $io->write('Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies'); + if (!$this->locker->isFresh()) { + $this->io->write('Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies'); } - foreach ($composer->getLocker()->getLockedPackages() as $package) { + foreach ($this->locker->getLockedPackages() as $package) { $version = $package->getVersion(); foreach ($aliases as $alias) { if ($alias['package'] === $package->getName() && $alias['version'] === $package->getVersion()) { @@ -127,9 +192,9 @@ class Install $request->install($package->getName(), $constraint); } } else { - $io->write('Installing dependencies'); + $this->io->write('Installing dependencies'); - $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests); + $links = $this->collectLinks($noInstallRecommends, $installSuggests); foreach ($links as $link) { $request->install($link->getTarget(), $link->getConstraint()); @@ -137,9 +202,8 @@ class Install } // prepare solver - $installationManager = $composer->getInstallationManager(); - $policy = new DependencyResolver\DefaultPolicy(); - $solver = new DependencyResolver\Solver($policy, $pool, $installedRepo); + $policy = new DefaultPolicy(); + $solver = new Solver($policy, $pool, $installedRepo); // solve dependencies $operations = $solver->solve($request); @@ -183,15 +247,15 @@ class Install // execute operations if (!$operations) { - $io->write('Nothing to install/update'); + $this->io->write('Nothing to install/update'); } foreach ($operations as $operation) { if ($verbose) { - $io->write((string) $operation); + $this->io->write((string) $operation); } if (!$dryRun) { - $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 ($installFromLock) { @@ -202,7 +266,7 @@ class Install $package = $operation->getPackage(); } if ($package && $package->isDev()) { - $lockData = $composer->getLocker()->getLockData(); + $lockData = $this->locker->getLockData(); foreach ($lockData['packages'] as $lockedPackage) { if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) { $package->setSourceReference($lockedPackage['source-reference']); @@ -211,42 +275,63 @@ class Install } } } - $installationManager->execute($operation); + $this->installationManager->execute($operation); - $eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType())), $operation); + $this->eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType())), $operation); } } if (!$dryRun) { - if ($update || !$composer->getLocker()->isLocked()) { - $composer->getLocker()->setLockData($localRepo->getPackages(), $aliases); - $io->write('Writing lock file'); + if ($update || !$this->locker->isLocked()) { + $this->locker->setLockData($localRepo->getPackages(), $aliases); + $this->io->write('Writing lock file'); } $localRepo->write(); - $io->write('Generating autoload files'); + $this->io->write('Generating autoload files'); $generator = new AutoloadGenerator; - $generator->dump($localRepo, $composer->getPackage(), $installationManager, $installationManager->getVendorPath().'/.composer'); + $generator->dump($localRepo, $this->package, $this->installationManager, $this->installationManager->getVendorPath().'/.composer'); // dispatch post event $eventName = $update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; - $eventDispatcher->dispatchCommandEvent($eventName); + $this->eventDispatcher->dispatchCommandEvent($eventName); } } - private function collectLinks(PackageInterface $package, $noInstallRecommends, $installSuggests) + private function collectLinks($noInstallRecommends, $installSuggests) { - $links = $package->getRequires(); + $links = $this->package->getRequires(); if (!$noInstallRecommends) { - $links = array_merge($links, $package->getRecommends()); + $links = array_merge($links, $this->package->getRecommends()); } if ($installSuggests) { - $links = array_merge($links, $package->getSuggests()); + $links = array_merge($links, $this->package->getSuggests()); } return $links; } + + /** + * Create Install + * + * @param IOInterface $io + * @param Composer $composer + * @param EventDispatcher $eventDispatcher + * @return Install + */ + static public function create(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher) + { + return new static( + $io, + $composer->getPackage(), + $composer->getDownloadManager(), + $composer->getRepositoryManager(), + $composer->getLocker(), + $composer->getInstallationManager(), + $eventDispatcher + ); + } } From b4dd86c2477d0611cf203a88a24afb9abdd56ecc Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Mon, 5 Mar 2012 20:28:37 -0800 Subject: [PATCH 3/6] Remove $repoManager local variable. --- src/Composer/Install.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Composer/Install.php b/src/Composer/Install.php index ff1d658b6..5ca2dec4d 100644 --- a/src/Composer/Install.php +++ b/src/Composer/Install.php @@ -120,10 +120,10 @@ class Install $this->downloadManager->setPreferSource(true); } - $repoManager = $this->repositoryManager; + $this->repositoryManager = $this->repositoryManager; // create local repo, this contains all packages that are installed in the local project - $localRepo = $repoManager->getLocalRepository(); + $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) { @@ -137,19 +137,19 @@ class Install $aliases = $this->package->getAliases(); } foreach ($aliases as $alias) { - foreach ($repoManager->findPackages($alias['package'], $alias['version']) as $package) { + foreach ($this->repositoryManager->findPackages($alias['package'], $alias['version']) as $package) { $package->getRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias'])); } - foreach ($repoManager->getLocalRepository()->findPackages($alias['package'], $alias['version']) as $package) { - $repoManager->getLocalRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias'])); - $repoManager->getLocalRepository()->removePackage($package); + foreach ($this->repositoryManager->getLocalRepository()->findPackages($alias['package'], $alias['version']) as $package) { + $this->repositoryManager->getLocalRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias'])); + $this->repositoryManager->getLocalRepository()->removePackage($package); } } // creating repository pool $pool = new Pool; $pool->addRepository($installedRepo); - foreach ($repoManager->getRepositories() as $repository) { + foreach ($this->repositoryManager->getRepositories() as $repository) { $pool->addRepository($repository); } @@ -230,7 +230,7 @@ class Install } // force update - $newPackage = $repoManager->findPackage($package->getName(), $package->getVersion()); + $newPackage = $this->repositoryManager->findPackage($package->getName(), $package->getVersion()); if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) { $operations[] = new UpdateOperation($package, $newPackage); } @@ -238,10 +238,10 @@ class Install } // anti-alias local repository to allow updates to work fine - foreach ($repoManager->getLocalRepository()->getPackages() as $package) { + foreach ($this->repositoryManager->getLocalRepository()->getPackages() as $package) { if ($package instanceof AliasPackage) { - $repoManager->getLocalRepository()->addPackage(clone $package->getAliasOf()); - $repoManager->getLocalRepository()->removePackage($package); + $this->repositoryManager->getLocalRepository()->addPackage(clone $package->getAliasOf()); + $this->repositoryManager->getLocalRepository()->removePackage($package); } } From 8a7d31706da6626df79ad4f8403de9265f5c8887 Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Tue, 6 Mar 2012 15:30:18 -0800 Subject: [PATCH 4/6] Renamed Composer\Install to Composer\Installer --- src/Composer/Command/InstallCommand.php | 4 ++-- src/Composer/Command/UpdateCommand.php | 4 ++-- src/Composer/{Install.php => Installer.php} | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) rename src/Composer/{Install.php => Installer.php} (99%) diff --git a/src/Composer/Command/InstallCommand.php b/src/Composer/Command/InstallCommand.php index 07b5d480a..e8744af5a 100644 --- a/src/Composer/Command/InstallCommand.php +++ b/src/Composer/Command/InstallCommand.php @@ -12,7 +12,7 @@ namespace Composer\Command; -use Composer\Install; +use Composer\Installer; use Composer\Script\EventDispatcher; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -53,7 +53,7 @@ EOT $composer = $this->getComposer(); $io = $this->getApplication()->getIO(); $eventDispatcher = new EventDispatcher($composer, $io); - $install = Install::create($io, $composer, $eventDispatcher); + $install = Installer::create($io, $composer, $eventDispatcher); return $install->run( (Boolean)$input->getOption('prefer-source'), diff --git a/src/Composer/Command/UpdateCommand.php b/src/Composer/Command/UpdateCommand.php index f07f6df20..69640bd3d 100644 --- a/src/Composer/Command/UpdateCommand.php +++ b/src/Composer/Command/UpdateCommand.php @@ -12,7 +12,7 @@ namespace Composer\Command; -use Composer\Install; +use Composer\Installer; use Composer\Script\EventDispatcher; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -51,7 +51,7 @@ EOT $composer = $this->getComposer(); $io = $this->getApplication()->getIO(); $eventDispatcher = new EventDispatcher($composer, $io); - $install = Install::create($io, $composer, $eventDispatcher); + $install = Installer::create($io, $composer, $eventDispatcher); return $install->run( (Boolean)$input->getOption('prefer-source'), diff --git a/src/Composer/Install.php b/src/Composer/Installer.php similarity index 99% rename from src/Composer/Install.php rename to src/Composer/Installer.php index 5ca2dec4d..6bb13eb86 100644 --- a/src/Composer/Install.php +++ b/src/Composer/Installer.php @@ -33,7 +33,7 @@ use Composer\Repository\RepositoryManager; use Composer\Script\EventDispatcher; use Composer\Script\ScriptEvents; -class Install +class Installer { /** * @@ -315,12 +315,12 @@ class Install } /** - * Create Install + * Create Installer * * @param IOInterface $io * @param Composer $composer * @param EventDispatcher $eventDispatcher - * @return Install + * @return Installer */ static public function create(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher) { From 3352066ecec47b67ae2550a7cb9b75057976e80f Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Thu, 8 Mar 2012 09:58:19 -0800 Subject: [PATCH 5/6] Use Installer for Create Project Command. --- src/Composer/Command/CreateProjectCommand.php | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Composer/Command/CreateProjectCommand.php b/src/Composer/Command/CreateProjectCommand.php index d21fafb64..ea6c4cd42 100644 --- a/src/Composer/Command/CreateProjectCommand.php +++ b/src/Composer/Command/CreateProjectCommand.php @@ -12,16 +12,18 @@ namespace Composer\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Output\OutputInterface; -use Composer\IO\IOInterface; use Composer\Factory; +use Composer\Installer; +use Composer\Installer\ProjectInstaller; +use Composer\IO\IOInterface; use Composer\Repository\ComposerRepository; use Composer\Repository\FilesystemRepository; -use Composer\Installer\ProjectInstaller; +use Composer\Script\EventDispatcher; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; /** * Install a package as new project into new directory. @@ -76,15 +78,6 @@ EOT ); } - protected function getInstallCommand($input, $output) - { - $app = $this->getApplication(); - return function() use ($app, $input, $output) { - $newInput = new ArrayInput(array('command' => 'install')); - $app->doRUn($newInput, $output); - }; - } - public function installProject(IOInterface $io, $installCommand, $packageName, $directory = null, $version = null, $preferSource = false, $repositoryUrl = null) { $dm = $this->createDownloadManager($io); @@ -126,7 +119,12 @@ EOT $io->write('Created project into directory ' . $directory . '', true); chdir($directory); - $installCommand(); + + $composer = Factory::create($io); + $eventDispatcher = new EventDispatcher($composer, $io); + $installer = Installer::create($io, $composer, $eventDispatcher); + + $installer->run($preferSource); } protected function createDownloadManager(IOInterface $io) From b1c93d1f0a5f659ff1099cfa22cdcb7c50f3c7f7 Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Thu, 8 Mar 2012 11:04:56 -0800 Subject: [PATCH 6/6] Fixed unused `use` per @stof and horrible bug that made this class no longer work. --- src/Composer/Command/CreateProjectCommand.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Composer/Command/CreateProjectCommand.php b/src/Composer/Command/CreateProjectCommand.php index ea6c4cd42..04b6266f2 100644 --- a/src/Composer/Command/CreateProjectCommand.php +++ b/src/Composer/Command/CreateProjectCommand.php @@ -19,7 +19,6 @@ use Composer\IO\IOInterface; use Composer\Repository\ComposerRepository; use Composer\Repository\FilesystemRepository; use Composer\Script\EventDispatcher; -use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -69,7 +68,6 @@ EOT return $this->installProject( $io, - $this->getInstallCommand($input, $output), $input->getArgument('package'), $input->getArgument('directory'), $input->getArgument('version'), @@ -78,7 +76,7 @@ EOT ); } - public function installProject(IOInterface $io, $installCommand, $packageName, $directory = null, $version = null, $preferSource = false, $repositoryUrl = null) + public function installProject(IOInterface $io, $packageName, $directory = null, $version = null, $preferSource = false, $repositoryUrl = null) { $dm = $this->createDownloadManager($io); if ($preferSource) {