Ejemplo n.º 1
0
 public function testIsLocked()
 {
     $json = $this->createJsonFileMock();
     $locker = new Locker($json, $this->createRepositoryManagerMock(), 'md5');
     $json->expects($this->once())->method('exists')->will($this->returnValue(true));
     $this->assertTrue($locker->isLocked());
 }
Ejemplo n.º 2
0
 public function testIsLocked()
 {
     $json = $this->createJsonFileMock();
     $locker = new Locker(new NullIO(), $json, $this->createRepositoryManagerMock(), $this->createInstallationManagerMock(), 'md5');
     $json->expects($this->any())->method('exists')->will($this->returnValue(true));
     $json->expects($this->any())->method('read')->will($this->returnValue(array('packages' => array())));
     $this->assertTrue($locker->isLocked());
 }
Ejemplo n.º 3
0
 /**
  * Loads the most "current" list of packages that are installed meaning from lock ideally or from installed repo as fallback
  * @param  RepositoryInterface $installedRepo
  * @return array
  */
 private function getCurrentPackages($installedRepo)
 {
     if ($this->locker->isLocked()) {
         try {
             return $this->locker->getLockedRepository(true)->getPackages();
         } catch (\RuntimeException $e) {
             // fetch only non-dev packages from lock if doing a dev update fails due to a previously incomplete lock file
             return $this->locker->getLockedRepository()->getPackages();
         }
     }
     return $installedRepo->getPackages();
 }
Ejemplo n.º 4
0
 private function getRootAliases()
 {
     if (!$this->update && $this->locker->isLocked()) {
         $aliases = $this->locker->getAliases();
     } else {
         $aliases = $this->package->getAliases();
     }
     $normalizedAliases = array();
     foreach ($aliases as $alias) {
         $normalizedAliases[$alias['package']][$alias['version']] = array('alias' => $alias['alias'], 'alias_normalized' => $alias['alias_normalized']);
     }
     return $normalizedAliases;
 }
Ejemplo n.º 5
0
 private function aliasPackages()
 {
     if (!$this->update && $this->locker->isLocked()) {
         $aliases = $this->locker->getAliases();
     } else {
         $aliases = $this->package->getAliases();
     }
     foreach ($aliases as $alias) {
         foreach ($this->repositoryManager->findPackages($alias['package'], $alias['version']) as $package) {
             $package->setAlias($alias['alias_normalized']);
             $package->setPrettyAlias($alias['alias']);
             $package->getRepository()->addPackage($aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
             $aliasPackage->setRootPackageAlias(true);
         }
     }
     return $aliases;
 }
Ejemplo n.º 6
0
 /**
  * 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)
 {
     if ($dryRun) {
         $verbose = true;
     }
     if ($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);
     }
     // prepare aliased packages
     if (!$update && $this->locker->isLocked()) {
         $aliases = $this->locker->getAliases();
     } else {
         $aliases = $this->package->getAliases();
     }
     foreach ($aliases as $alias) {
         foreach ($this->repositoryManager->findPackages($alias['package'], $alias['version']) as $package) {
             $package->getRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
         }
         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 ($this->repositoryManager->getRepositories() as $repository) {
         $pool->addRepository($repository);
     }
     // dispatch pre event
     if (!$dryRun) {
         $eventName = $update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchCommandEvent($eventName);
     }
     // creating requirements request
     $installFromLock = false;
     $request = new Request($pool);
     if ($update) {
         $this->io->write('<info>Updating dependencies</info>');
         $request->updateAll();
         $links = $this->collectLinks($noInstallRecommends, $installSuggests);
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     } elseif ($this->locker->isLocked()) {
         $installFromLock = true;
         $this->io->write('<info>Installing from lock file</info>');
         if (!$this->locker->isFresh()) {
             $this->io->write('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
         }
         foreach ($this->locker->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 {
         $this->io->write('<info>Installing dependencies</info>');
         $links = $this->collectLinks($noInstallRecommends, $installSuggests);
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     }
     // prepare solver
     $policy = new DefaultPolicy();
     $solver = new 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 = $this->repositoryManager->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 ($this->repositoryManager->getLocalRepository()->getPackages() as $package) {
         if ($package instanceof AliasPackage) {
             $this->repositoryManager->getLocalRepository()->addPackage(clone $package->getAliasOf());
             $this->repositoryManager->getLocalRepository()->removePackage($package);
         }
     }
     // execute operations
     if (!$operations) {
         $this->io->write('<info>Nothing to install/update</info>');
     }
     foreach ($operations as $operation) {
         if ($verbose) {
             $this->io->write((string) $operation);
         }
         if (!$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
             if ($installFromLock) {
                 $package = null;
                 if ('update' === $operation->getJobType()) {
                     $package = $operation->getTargetPackage();
                 } elseif ('install' === $operation->getJobType()) {
                     $package = $operation->getPackage();
                 }
                 if ($package && $package->isDev()) {
                     $lockData = $this->locker->getLockData();
                     foreach ($lockData['packages'] as $lockedPackage) {
                         if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) {
                             $package->setSourceReference($lockedPackage['source-reference']);
                             break;
                         }
                     }
                 }
             }
             $this->installationManager->execute($operation);
             $this->eventDispatcher->dispatchPackageEvent(constant('Composer\\Script\\ScriptEvents::POST_PACKAGE_' . strtoupper($operation->getJobType())), $operation);
         }
     }
     if (!$dryRun) {
         if ($update || !$this->locker->isLocked()) {
             $this->locker->setLockData($localRepo->getPackages(), $aliases);
             $this->io->write('<info>Writing lock file</info>');
         }
         $localRepo->write();
         $this->io->write('<info>Generating autoload files</info>');
         $generator = new AutoloadGenerator();
         $generator->dump($localRepo, $this->package, $this->installationManager, $this->installationManager->getVendorPath() . '/.composer');
         // dispatch post event
         $eventName = $update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
         $this->eventDispatcher->dispatchCommandEvent($eventName);
     }
 }