Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. CreateProjectCommand.php

function CreateProjectCommand::installProject

Parameters

string|array<string>|null $repositories:

Throws

\Exception

1 call to CreateProjectCommand::installProject()
CreateProjectCommand::execute in vendor/composer/composer/src/Composer/Command/CreateProjectCommand.php
Executes the current command.

File

vendor/composer/composer/src/Composer/Command/CreateProjectCommand.php, line 180

Class

CreateProjectCommand
Install a package as new project into new directory.

Namespace

Composer\Command

Code

public function installProject(IOInterface $io, Config $config, InputInterface $input, ?string $packageName = null, ?string $directory = null, ?string $packageVersion = null, ?string $stability = 'stable', bool $preferSource = false, bool $preferDist = false, bool $installDevPackages = false, $repositories = null, bool $disablePlugins = false, bool $disableScripts = false, bool $noProgress = false, bool $noInstall = false, ?PlatformRequirementFilterInterface $platformRequirementFilter = null, bool $secureHttp = true, bool $addRepository = false) : int {
    $oldCwd = Platform::getCwd();
    if ($repositories !== null && !is_array($repositories)) {
        $repositories = (array) $repositories;
    }
    $platformRequirementFilter = $platformRequirementFilter ?? PlatformRequirementFilterFactory::ignoreNothing();
    // we need to manually load the configuration to pass the auth credentials to the io interface!
    $io->loadConfiguration($config);
    $this->suggestedPackagesReporter = new SuggestedPackagesReporter($io);
    if ($packageName !== null) {
        $installedFromVcs = $this->installRootPackage($input, $io, $config, $packageName, $platformRequirementFilter, $directory, $packageVersion, $stability, $preferSource, $preferDist, $installDevPackages, $repositories, $disablePlugins, $disableScripts, $noProgress, $secureHttp);
    }
    else {
        $installedFromVcs = false;
    }
    if ($repositories !== null && $addRepository && is_file('composer.lock')) {
        unlink('composer.lock');
    }
    $composer = $this->createComposerInstance($input, $io, null, $disablePlugins, $disableScripts);
    // add the repository to the composer.json and use it for the install run later
    if ($repositories !== null && $addRepository) {
        foreach ($repositories as $index => $repo) {
            $repoConfig = RepositoryFactory::configFromString($io, $composer->getConfig(), $repo, true);
            $composerJsonRepositoriesConfig = $composer->getConfig()
                ->getRepositories();
            $name = RepositoryFactory::generateRepositoryName($index, $repoConfig, $composerJsonRepositoriesConfig);
            $configSource = new JsonConfigSource(new JsonFile('composer.json'));
            if (isset($repoConfig['packagist']) && $repoConfig === [
                'packagist' => false,
            ] || isset($repoConfig['packagist.org']) && $repoConfig === [
                'packagist.org' => false,
            ]) {
                $configSource->addRepository('packagist.org', false);
            }
            else {
                $configSource->addRepository($name, $repoConfig, false);
            }
            $composer = $this->createComposerInstance($input, $io, null, $disablePlugins);
        }
    }
    $process = $composer->getLoop()
        ->getProcessExecutor();
    $fs = new Filesystem($process);
    // dispatch event
    $composer->getEventDispatcher()
        ->dispatchScript(ScriptEvents::POST_ROOT_PACKAGE_INSTALL, $installDevPackages);
    // use the new config including the newly installed project
    $config = $composer->getConfig();
    [
        $preferSource,
        $preferDist,
    ] = $this->getPreferredInstallOptions($config, $input);
    // install dependencies of the created project
    if ($noInstall === false) {
        $composer->getInstallationManager()
            ->setOutputProgress(!$noProgress);
        $installer = Installer::create($io, $composer);
        $installer->setPreferSource($preferSource)
            ->setPreferDist($preferDist)
            ->setDevMode($installDevPackages)
            ->setPlatformRequirementFilter($platformRequirementFilter)
            ->setSuggestedPackagesReporter($this->suggestedPackagesReporter)
            ->setOptimizeAutoloader($config->get('optimize-autoloader'))
            ->setClassMapAuthoritative($config->get('classmap-authoritative'))
            ->setApcuAutoloader($config->get('apcu-autoloader'))
            ->setAudit(!$input->getOption('no-audit'))
            ->setAuditFormat($this->getAuditFormat($input));
        if (!$composer->getLocker()
            ->isLocked()) {
            $installer->setUpdate(true);
        }
        if ($disablePlugins) {
            $installer->disablePlugins();
        }
        try {
            $status = $installer->run();
            if (0 !== $status) {
                return $status;
            }
        } catch (PluginBlockedException $e) {
            $io->writeError('<error>Hint: To allow running the config command recommended below before dependencies are installed, run create-project with --no-install.</error>');
            $io->writeError('<error>You can then cd into ' . getcwd() . ', configure allow-plugins, and finally run a composer install to complete the process.</error>');
            throw $e;
        }
    }
    $hasVcs = $installedFromVcs;
    if (!$input->getOption('keep-vcs') && $installedFromVcs && ($input->getOption('remove-vcs') || !$io->isInteractive() || $io->askConfirmation('<info>Do you want to remove the existing VCS (.git, .svn..) history?</info> [<comment>Y,n</comment>]? '))) {
        $finder = new Finder();
        $finder->depth(0)
            ->directories()
            ->in(Platform::getCwd())
            ->ignoreVCS(false)
            ->ignoreDotFiles(false);
        foreach ([
            '.svn',
            '_svn',
            'CVS',
            '_darcs',
            '.arch-params',
            '.monotone',
            '.bzr',
            '.git',
            '.hg',
            '.fslckout',
            '_FOSSIL_',
        ] as $vcsName) {
            $finder->name($vcsName);
        }
        try {
            $dirs = iterator_to_array($finder);
            unset($finder);
            foreach ($dirs as $dir) {
                if (!$fs->removeDirectory((string) $dir)) {
                    throw new \RuntimeException('Could not remove ' . $dir);
                }
            }
        } catch (\Exception $e) {
            $io->writeError('<error>An error occurred while removing the VCS metadata: ' . $e->getMessage() . '</error>');
        }
        $hasVcs = false;
    }
    // rewriting self.version dependencies with explicit version numbers if the package's vcs metadata is gone
    if (!$hasVcs) {
        $package = $composer->getPackage();
        $configSource = new JsonConfigSource(new JsonFile('composer.json'));
        foreach (BasePackage::$supportedLinkTypes as $type => $meta) {
            foreach ($package->{'get' . $meta['method']}() as $link) {
                if ($link->getPrettyConstraint() === 'self.version') {
                    $configSource->addLink($type, $link->getTarget(), $package->getPrettyVersion());
                }
            }
        }
    }
    // dispatch event
    $composer->getEventDispatcher()
        ->dispatchScript(ScriptEvents::POST_CREATE_PROJECT_CMD, $installDevPackages);
    chdir($oldCwd);
    return 0;
}
RSS feed
Powered by Drupal