Ejemplo n.º 1
0
 /**
  * Constructor
  *
  * @param Filesystem $filesystem
  */
 public function __construct(Filesystem $filesystem)
 {
     $vendor = $filesystem->getDirectoryRead(DirectoryList::CONFIG)->getAbsolutePath('vendor_path.php');
     $vendorPath = $filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath() . (include $vendor);
     // Create Composer
     $io = new \Composer\IO\BufferIO();
     $this->composer = ComposerFactory::create($io, $vendorPath . '/../composer.json');
     $this->locker = $this->composer->getLocker();
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function init()
 {
     // Retrieve the configuration variables.
     $this->config = $this->composer->getConfig();
     if (isset($this->config)) {
         if ($this->config->has('component-dir')) {
             $this->componentDir = $this->config->get('component-dir');
         }
     }
     // Get the available packages.
     $allPackages = array();
     /** @var \Composer\Package\Locker $locker */
     $locker = $this->composer->getLocker();
     if ($locker !== null && $locker->isLocked()) {
         $lockData = $locker->getLockData();
         $allPackages = $lockData['packages'];
         // Also merge in any of the development packages.
         $dev = isset($lockData['packages-dev']) ? $lockData['packages-dev'] : array();
         foreach ($dev as $package) {
             $allPackages[] = $package;
         }
     }
     // Only add those packages that we can reasonably
     // assume are components into our packages list
     /** @var \Composer\Package\RootPackageInterface $rootPackage */
     $rootPackage = $this->composer->getPackage();
     $rootExtras = $rootPackage ? $rootPackage->getExtra() : array();
     $customComponents = isset($rootExtras['component']) ? $rootExtras['component'] : array();
     foreach ($allPackages as $package) {
         $name = $package['name'];
         if (isset($customComponents[$name]) && is_array($customComponents[$name])) {
             $package['extra'] = array('component' => $customComponents[$name]);
             $this->packages[] = $package;
         } else {
             $extra = isset($package['extra']) ? $package['extra'] : array();
             if (isset($extra['component']) && is_array($extra['component'])) {
                 $this->packages[] = $package;
             }
         }
     }
     // Add the root package to the packages list.
     $root = $this->composer->getPackage();
     if ($root) {
         $dumper = new ArrayDumper();
         $package = $dumper->dump($root);
         $package['is-root'] = true;
         $this->packages[] = $package;
     }
     return true;
 }
Ejemplo n.º 3
0
 /**
  * Constructor
  *
  * @param Filesystem $filesystem
  * @param BufferIoFactory $bufferIoFactory
  * @throws \Exception
  */
 public function __construct(Filesystem $filesystem, BufferIoFactory $bufferIoFactory)
 {
     // composer.json is in same directory as vendor
     $vendorPath = $filesystem->getDirectoryRead(DirectoryList::CONFIG)->getAbsolutePath('vendor_path.php');
     $vendorDir = (require "{$vendorPath}");
     $composerJson = $filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath() . "/{$vendorDir}/../composer.json";
     $composerJsonRealPath = realpath($composerJson);
     if ($composerJsonRealPath === false) {
         throw new \Exception('Composer file not found: ' . $composerJson);
     }
     putenv('COMPOSER_HOME=' . $filesystem->getDirectoryRead(DirectoryList::COMPOSER_HOME)->getAbsolutePath());
     // Create Composer
     $this->composer = ComposerFactory::create($bufferIoFactory->create(), $composerJson);
     $this->locker = $this->composer->getLocker();
 }
Ejemplo n.º 4
0
 public function testSetGetLocker()
 {
     $composer = new Composer();
     $locker = $this->getMockBuilder('Composer\\Package\\Locker')->disableOriginalConstructor()->getMock();
     $composer->setLocker($locker);
     $this->assertSame($locker, $composer->getLocker());
 }
Ejemplo n.º 5
0
 /**
  * Constructor
  *
  * @param MagentoComposerApplicationFactory $applicationFactory
  * @throws \Exception
  */
 public function __construct(
     MagentoComposerApplicationFactory $applicationFactory
 ) {
     $this->application = $applicationFactory->create();
     $this->composer = $this->application->createComposer();
     $this->locker = $this->composer->getLocker();
 }
Ejemplo n.º 6
0
 /**
  * Returns locker data for given package
  *
  * @param BasePackage
  * @return string
  */
 private function getLockerData(BasePackage $package)
 {
     $data = $this->composer->getLocker()->getLockData();
     foreach ((array) $data['packages'] as $pkgData) {
         if ($pkgData['name'] == $package->getName()) {
             return $pkgData;
         }
     }
     return NULL;
 }
Ejemplo n.º 7
0
 /**
  * Create Installer
  *
  * @param  IOInterface       $io
  * @param  Composer          $composer
  * @param  EventDispatcher   $eventDispatcher
  * @param  AutoloadGenerator $autoloadGenerator
  * @return Installer
  */
 public static function create(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher = null, AutoloadGenerator $autoloadGenerator = null)
 {
     $eventDispatcher = $eventDispatcher ?: new EventDispatcher($composer, $io);
     $autoloadGenerator = $autoloadGenerator ?: new AutoloadGenerator();
     return new static($io, $composer->getConfig(), $composer->getPackage(), $composer->getDownloadManager(), $composer->getRepositoryManager(), $composer->getLocker(), $composer->getInstallationManager(), $eventDispatcher, $autoloadGenerator);
 }
Ejemplo n.º 8
0
 /**
  * Create Installer
  *
  * @param  IOInterface $io
  * @param  Composer    $composer
  * @return Installer
  */
 public static function create(IOInterface $io, Composer $composer)
 {
     return new static($io, $composer->getConfig(), $composer->getPackage(), $composer->getDownloadManager(), $composer->getRepositoryManager(), $composer->getLocker(), $composer->getInstallationManager(), $composer->getEventDispatcher(), $composer->getAutoloadGenerator());
 }
 /**
  * Create an Installer instance.
  *
  * Private static factory, to allow slip-streaming in a mock as needed for
  * testing.
  */
 private static function createInstaller(Composer $composer, IOInterface $io, PackageInterface $package)
 {
     $eventDispatcher = new EventDispatcher($composer, $io);
     return new ComposerInstaller($io, $composer->getConfig(), $package, $composer->getDownloadManager(), $composer->getRepositoryManager(), $composer->getLocker(), $composer->getInstallationManager(), $eventDispatcher, $composer->getAutoloadGenerator());
 }
Ejemplo n.º 10
0
 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);
     }
     // create local repo, this contains all packages that are installed in the local project
     $localRepo = $composer->getRepositoryManager()->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);
     }
     // creating repository pool
     $pool = new Pool();
     $pool->addRepository($installedRepo);
     foreach ($composer->getRepositoryManager()->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('<info>Updating dependencies</info>');
         $installedPackages = $installedRepo->getPackages();
         $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests);
         $request->updateAll();
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     } elseif ($composer->getLocker()->isLocked()) {
         $installFromLock = true;
         $io->write('<info>Installing from lock file</info>');
         if (!$composer->getLocker()->isFresh()) {
             $io->write('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
         }
         foreach ($composer->getLocker()->getLockedPackages() as $package) {
             $constraint = new VersionConstraint('=', $package->getVersion());
             $request->install($package->getName(), $constraint);
         }
     } else {
         $io->write('<info>Installing dependencies</info>');
         $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);
     // execute operations
     if (!$operations) {
         $io->write('<info>Nothing to install/update</info>');
     }
     // force dev packages to be updated to latest reference on update
     if ($update) {
         foreach ($localRepo->getPackages() as $package) {
             // 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 = $composer->getRepositoryManager()->findPackage($package->getName(), $package->getVersion());
             if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) {
                 $operations[] = new UpdateOperation($package, $newPackage);
             }
         }
     }
     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()->lockPackages($localRepo->getPackages());
             $io->write('<info>Writing lock file</info>');
         }
         $localRepo->write();
         $io->write('<info>Generating autoload files</info>');
         $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);
     }
 }
Ejemplo n.º 11
0
 private static function generateVersionsClass(Composer $composer) : string
 {
     return sprintf(self::$generatedClassTemplate, var_export(iterator_to_array(self::getVersions($composer->getLocker(), $composer->getPackage())), true));
 }
Ejemplo n.º 12
0
 /**
  * Returns the PhantomJS version number.
  *
  * Firstly, we search for a version number in the local repository,
  * secondly, in the root package.
  * A version specification of "dev-master#<commit-reference>" is disallowed.
  *
  * @param Composer $composer
  * @return string $version Version
  */
 public static function getVersion($composer)
 {
     // try getting the version from the local repository
     $packages = $composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages();
     foreach ($packages as $package) {
         if ($package->getName() === self::PACKAGE_NAME) {
             $version = $package->getPrettyVersion();
             break;
         }
     }
     // let's take a look at the aliases
     if ($package->getVersion() === '9999999-dev') {
         // this indicates the version alias???
         $aliases = $composer->getLocker()->getAliases();
         foreach ($aliases as $idx => $alias) {
             if ($alias['package'] === self::PACKAGE_NAME) {
                 return $alias['alias'];
             }
         }
     }
     // let's take a look at the root package
     if (!empty($version)) {
         $version = self::getRequiredVersion($composer->getPackage());
     }
     // fallback to the hardcoded latest version, if "dev-master" was set
     if ($version === 'dev-master') {
         return self::getLatestPhantomJsVersion();
     }
     // grab version from commit-reference, e.g. "dev-master#<commit-ref> as version"
     if (preg_match('/dev-master#(?:.*)(\\d.\\d.\\d)/i', $version, $matches)) {
         return $matches[1];
     }
     // grab version from a Composer patch version tag with a patch level, like "1.9.8-p02"
     if (preg_match('/(\\d.\\d.\\d)(?:(?:-p\\d{2})?)/i', $version, $matches)) {
         return $matches[1];
     }
     return $version;
 }