Пример #1
0
 /**
  * @param string|JsonFile $json
  * @param Config $config
  *
  * @return \Composer\Package\RootPackageInterface
  */
 public static function loadRootPackageFromJson($json, Config $config = null)
 {
     try {
         $config = null !== $config ? $config : self::loadConfiguration();
         $loader = new JsonLoader(new RootPackageLoader(RepositoryFactory::manager(new NullIO(), $config), $config));
         $package = $loader->load($json);
     } catch (Exception $e) {
         throw RuntimeException::fromAnyException($e);
     }
     return $package;
 }
Пример #2
0
 /**
  * Creates a Composer instance
  *
  * @param  IOInterface               $io             IO instance
  * @param  array|string|null         $localConfig    either a configuration array or a filename to read from, if null it will
  *                                                   read from the default filename
  * @param  bool                      $disablePlugins Whether plugins should not be loaded
  * @param  bool                      $fullLoad       Whether to initialize everything or only main project stuff (used when loading the global composer)
  * @throws \InvalidArgumentException
  * @throws \UnexpectedValueException
  * @return Composer
  */
 public function createComposer(IOInterface $io, $localConfig = null, $disablePlugins = false, $cwd = null, $fullLoad = true)
 {
     $cwd = $cwd ?: getcwd();
     // load Composer configuration
     if (null === $localConfig) {
         $localConfig = static::getComposerFile();
     }
     if (is_string($localConfig)) {
         $composerFile = $localConfig;
         $file = new JsonFile($localConfig, null, $io);
         if (!$file->exists()) {
             if ($localConfig === './composer.json' || $localConfig === 'composer.json') {
                 $message = 'Composer could not find a composer.json file in ' . $cwd;
             } else {
                 $message = 'Composer could not find the config file: ' . $localConfig;
             }
             $instructions = 'To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section';
             throw new \InvalidArgumentException($message . PHP_EOL . $instructions);
         }
         $file->validateSchema(JsonFile::LAX_SCHEMA);
         $jsonParser = new JsonParser();
         try {
             $jsonParser->parse(file_get_contents($localConfig), JsonParser::DETECT_KEY_CONFLICTS);
         } catch (DuplicateKeyException $e) {
             $details = $e->getDetails();
             $io->writeError('<warning>Key ' . $details['key'] . ' is a duplicate in ' . $localConfig . ' at line ' . $details['line'] . '</warning>');
         }
         $localConfig = $file->read();
     }
     // Load config and override with local config/auth config
     $config = static::createConfig($io, $cwd);
     $config->merge($localConfig);
     if (isset($composerFile)) {
         $io->writeError('Loading config file ' . $composerFile, true, IOInterface::DEBUG);
         $config->setConfigSource(new JsonConfigSource(new JsonFile(realpath($composerFile), null, $io)));
         $localAuthFile = new JsonFile(dirname(realpath($composerFile)) . '/auth.json', null, $io);
         if ($localAuthFile->exists()) {
             $io->writeError('Loading config file ' . $localAuthFile->getPath(), true, IOInterface::DEBUG);
             $config->merge(array('config' => $localAuthFile->read()));
             $config->setAuthConfigSource(new JsonConfigSource($localAuthFile, true));
         }
     }
     $vendorDir = $config->get('vendor-dir');
     // initialize composer
     $composer = new Composer();
     $composer->setConfig($config);
     if ($fullLoad) {
         // load auth configs into the IO instance
         $io->loadConfiguration($config);
     }
     $rfs = self::createRemoteFilesystem($io, $config);
     // initialize event dispatcher
     $dispatcher = new EventDispatcher($composer, $io);
     $composer->setEventDispatcher($dispatcher);
     // initialize repository manager
     $rm = RepositoryFactory::manager($io, $config, $dispatcher, $rfs);
     $composer->setRepositoryManager($rm);
     // load local repository
     $this->addLocalRepository($io, $rm, $vendorDir);
     // force-set the version of the global package if not defined as
     // guessing it adds no value and only takes time
     if (!$fullLoad && !isset($localConfig['version'])) {
         $localConfig['version'] = '1.0.0';
     }
     // load package
     $parser = new VersionParser();
     $guesser = new VersionGuesser($config, new ProcessExecutor($io), $parser);
     $loader = new Package\Loader\RootPackageLoader($rm, $config, $parser, $guesser);
     $package = $loader->load($localConfig, 'Composer\\Package\\RootPackage', $cwd);
     $composer->setPackage($package);
     // initialize installation manager
     $im = $this->createInstallationManager();
     $composer->setInstallationManager($im);
     if ($fullLoad) {
         // initialize download manager
         $dm = $this->createDownloadManager($io, $config, $dispatcher, $rfs);
         $composer->setDownloadManager($dm);
         // initialize autoload generator
         $generator = new AutoloadGenerator($dispatcher, $io);
         $composer->setAutoloadGenerator($generator);
     }
     // add installers to the manager (must happen after download manager is created since they read it out of $composer)
     $this->createDefaultInstallers($im, $composer, $io);
     if ($fullLoad) {
         $globalComposer = null;
         if (realpath($config->get('home')) !== $cwd) {
             $globalComposer = $this->createGlobalComposer($io, $config, $disablePlugins);
         }
         $pm = $this->createPluginManager($io, $composer, $globalComposer, $disablePlugins);
         $composer->setPluginManager($pm);
         $pm->loadInstalledPlugins();
     }
     // init locker if possible
     if ($fullLoad && isset($composerFile)) {
         $lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION) ? substr($composerFile, 0, -4) . 'lock' : $composerFile . '.lock';
         $locker = new Package\Locker($io, new JsonFile($lockFile, null, $io), $rm, $im, file_get_contents($composerFile));
         $composer->setLocker($locker);
     }
     if ($fullLoad) {
         $initEvent = new Event(PluginEvents::INIT);
         $composer->getEventDispatcher()->dispatch($initEvent->getName(), $initEvent);
         // once everything is initialized we can
         // purge packages from local repos if they have been deleted on the filesystem
         if ($rm->getLocalRepository()) {
             $this->purgePackages($rm->getLocalRepository(), $im);
         }
     }
     return $composer;
 }