function Factory::createComposer
Creates a Composer instance
@phpstan-return ($fullLoad is true ? Composer : PartialComposer)
Parameters
IOInterface $io IO instance:
array<string, mixed>|string|null $localConfig either a configuration array or a filename to read from, if null it will: read from the default filename
bool|'local'|'global' $disablePlugins Whether plugins should not be loaded, can be set to local or global to only disable local/global plugins:
bool $disableScripts Whether scripts should not be run:
bool $fullLoad Whether to initialize everything or only main project stuff (used when loading the global composer):
Return value
Composer|PartialComposer Composer if $fullLoad is true, otherwise PartialComposer
Throws
\InvalidArgumentException
\UnexpectedValueException
1 call to Factory::createComposer()
- Factory::createGlobalComposer in vendor/
composer/ composer/ src/ Composer/ Factory.php - @phpstan-return ($fullLoad is true ? Composer|null : PartialComposer|null)
File
-
vendor/
composer/ composer/ src/ Composer/ Factory.php, line 281
Class
- Factory
- Creates a configured instance of composer.
Namespace
ComposerCode
public function createComposer(IOInterface $io, $localConfig = null, $disablePlugins = false, ?string $cwd = null, bool $fullLoad = true, bool $disableScripts = false) {
// if a custom composer.json path is given, we change the default cwd to be that file's directory
if (is_string($localConfig) && is_file($localConfig) && null === $cwd) {
$cwd = dirname($localConfig);
}
$cwd = $cwd ?? Platform::getCwd(true);
// load Composer configuration
if (null === $localConfig) {
$localConfig = static::getComposerFile();
}
$localConfigSource = Config::SOURCE_UNKNOWN;
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 = $fullLoad ? 'To initialize a project, please create a composer.json file. See https://getcomposer.org/basic-usage' : '';
throw new \InvalidArgumentException($message . PHP_EOL . $instructions);
}
if (!Platform::isInputCompletionProcess()) {
try {
$file->validateSchema(JsonFile::LAX_SCHEMA);
} catch (JsonValidationException $e) {
$errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors());
$message = $e->getMessage() . ':' . PHP_EOL . $errors;
throw new JsonValidationException($message);
}
}
$localConfig = $file->read();
$localConfigSource = $file->getPath();
}
// Load config and override with local config/auth config
$config = static::createConfig($io, $cwd);
$config->merge($localConfig, $localConfigSource);
if (isset($composerFile)) {
$io->writeError('Loading config file ' . $composerFile . ' (' . realpath($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);
self::validateJsonSchema($io, $localAuthFile, JsonFile::AUTH_SCHEMA);
$config->merge([
'config' => $localAuthFile->read(),
], $localAuthFile->getPath());
$config->setLocalAuthConfigSource(new JsonConfigSource($localAuthFile, true));
}
}
// make sure we load the auth env again over the local auth.json + composer.json config
self::loadComposerAuthEnv($config, $io);
$vendorDir = $config->get('vendor-dir');
// initialize composer
$composer = $fullLoad ? new Composer() : new PartialComposer();
$composer->setConfig($config);
if ($fullLoad) {
// load auth configs into the IO instance
$io->loadConfiguration($config);
// load existing Composer\InstalledVersions instance if available and scripts/plugins are allowed, as they might need it
// we only load if the InstalledVersions class wasn't defined yet so that this is only loaded once
if (false === $disablePlugins && false === $disableScripts && !class_exists('Composer\\InstalledVersions', false) && file_exists($installedVersionsPath = $config->get('vendor-dir') . '/composer/installed.php')) {
// force loading the class at this point so it is loaded from the composer phar and not from the vendor dir
// as we cannot guarantee integrity of that file
if (class_exists('Composer\\InstalledVersions')) {
FilesystemRepository::safelyLoadInstalledVersions($installedVersionsPath);
}
}
}
$httpDownloader = self::createHttpDownloader($io, $config);
$process = new ProcessExecutor($io);
$loop = new Loop($httpDownloader, $process);
$composer->setLoop($loop);
// initialize event dispatcher
$dispatcher = new EventDispatcher($composer, $io, $process);
$dispatcher->setRunScripts(!$disableScripts);
$composer->setEventDispatcher($dispatcher);
// initialize repository manager
$rm = RepositoryFactory::manager($io, $config, $httpDownloader, $dispatcher, $process);
$composer->setRepositoryManager($rm);
// 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, $process, $parser, $io);
$loader = $this->loadRootPackage($rm, $config, $parser, $guesser, $io);
$package = $loader->load($localConfig, 'Composer\\Package\\RootPackage', $cwd);
$composer->setPackage($package);
// load local repository
$this->addLocalRepository($io, $rm, $vendorDir, $package, $process);
// initialize installation manager
$im = $this->createInstallationManager($loop, $io, $dispatcher);
$composer->setInstallationManager($im);
if ($composer instanceof Composer) {
// initialize download manager
$dm = $this->createDownloadManager($io, $config, $httpDownloader, $process, $dispatcher);
$composer->setDownloadManager($dm);
// initialize autoload generator
$generator = new AutoloadGenerator($dispatcher, $io);
$composer->setAutoloadGenerator($generator);
// initialize archive manager
$am = $this->createArchiveManager($config, $dm, $loop);
$composer->setArchiveManager($am);
}
// add installers to the manager (must happen after download manager is created since they read it out of $composer)
$this->createDefaultInstallers($im, $composer, $io, $process);
// init locker if possible
if ($composer instanceof Composer && isset($composerFile)) {
$lockFile = self::getLockFile($composerFile);
if (!$config->get('lock') && file_exists($lockFile)) {
$io->writeError('<warning>' . $lockFile . ' is present but ignored as the "lock" config option is disabled.</warning>');
}
$locker = new Package\Locker($io, new JsonFile($config->get('lock') ? $lockFile : Platform::getDevNull(), null, $io), $im, file_get_contents($composerFile), $process);
$composer->setLocker($locker);
}
elseif ($composer instanceof Composer) {
$locker = new Package\Locker($io, new JsonFile(Platform::getDevNull(), null, $io), $im, JsonFile::encode($localConfig), $process);
$composer->setLocker($locker);
}
if ($composer instanceof Composer) {
$globalComposer = null;
if (realpath($config->get('home')) !== $cwd) {
$globalComposer = $this->createGlobalComposer($io, $config, $disablePlugins, $disableScripts);
}
$pm = $this->createPluginManager($io, $composer, $globalComposer, $disablePlugins);
$composer->setPluginManager($pm);
if (realpath($config->get('home')) === $cwd) {
$pm->setRunningInGlobalDir(true);
}
$pm->loadInstalledPlugins();
}
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
$this->purgePackages($rm->getLocalRepository(), $im);
}
return $composer;
}