protected function installRootPackage(IOInterface $io, Config $config, $packageName, $directory = null, $packageVersion = null, $stability = 'stable', $preferSource = false, $preferDist = false, $installDevPackages = false, $repository = null, $disablePlugins = false, $noScripts = false, $keepVcs = false, $noProgress = false, $ignorePlatformReqs = false, $secureHttp = true)
 {
     if (!$secureHttp) {
         $config->merge(array('config' => array('secure-http' => false)));
     }
     if (null === $repository) {
         $sourceRepo = new CompositeRepository(RepositoryFactory::defaultRepos($io, $config));
     } else {
         $sourceRepo = RepositoryFactory::fromString($io, $config, $repository, true);
     }
     $parser = new VersionParser();
     $requirements = $parser->parseNameVersionPairs(array($packageName));
     $name = strtolower($requirements[0]['name']);
     if (!$packageVersion && isset($requirements[0]['version'])) {
         $packageVersion = $requirements[0]['version'];
     }
     if (null === $stability) {
         if (preg_match('{^[^,\\s]*?@(' . implode('|', array_keys(BasePackage::$stabilities)) . ')$}i', $packageVersion, $match)) {
             $stability = $match[1];
         } else {
             $stability = VersionParser::parseStability($packageVersion);
         }
     }
     $stability = VersionParser::normalizeStability($stability);
     if (!isset(BasePackage::$stabilities[$stability])) {
         throw new \InvalidArgumentException('Invalid stability provided (' . $stability . '), must be one of: ' . implode(', ', array_keys(BasePackage::$stabilities)));
     }
     $pool = new Pool($stability);
     $pool->addRepository($sourceRepo);
     $phpVersion = null;
     $prettyPhpVersion = null;
     if (!$ignorePlatformReqs) {
         $platformOverrides = $config->get('platform') ?: array();
         // initialize $this->repos as it is used by the parent InitCommand
         $platform = new PlatformRepository(array(), $platformOverrides);
         $phpPackage = $platform->findPackage('php', '*');
         $phpVersion = $phpPackage->getVersion();
         $prettyPhpVersion = $phpPackage->getPrettyVersion();
     }
     // find the latest version if there are multiple
     $versionSelector = new VersionSelector($pool);
     $package = $versionSelector->findBestCandidate($name, $packageVersion, $phpVersion, $stability);
     if (!$package) {
         $errorMessage = "Could not find package {$name} with " . ($packageVersion ? "version {$packageVersion}" : "stability {$stability}");
         if ($phpVersion && $versionSelector->findBestCandidate($name, $packageVersion, null, $stability)) {
             throw new \InvalidArgumentException($errorMessage . ' in a version installable using your PHP version ' . $prettyPhpVersion . '.');
         }
         throw new \InvalidArgumentException($errorMessage . '.');
     }
     if (null === $directory) {
         $parts = explode("/", $name, 2);
         $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts);
     }
     // handler Ctrl+C for unix-like systems
     if (function_exists('pcntl_signal')) {
         declare (ticks=100);
         pcntl_signal(SIGINT, function () use($directory) {
             $fs = new Filesystem();
             $fs->removeDirectory($directory);
             exit(130);
         });
     }
     $io->writeError('<info>Installing ' . $package->getName() . ' (' . $package->getFullPrettyVersion(false) . ')</info>');
     if ($disablePlugins) {
         $io->writeError('<info>Plugins have been disabled.</info>');
     }
     if (0 === strpos($package->getPrettyVersion(), 'dev-') && in_array($package->getSourceType(), array('git', 'hg'))) {
         $package->setSourceReference(substr($package->getPrettyVersion(), 4));
     }
     $dm = $this->createDownloadManager($io, $config);
     $dm->setPreferSource($preferSource)->setPreferDist($preferDist)->setOutputProgress(!$noProgress);
     $projectInstaller = new ProjectInstaller($directory, $dm);
     $im = $this->createInstallationManager();
     $im->addInstaller($projectInstaller);
     $im->install(new InstalledFilesystemRepository(new JsonFile('php://memory')), new InstallOperation($package));
     $im->notifyInstalls($io);
     // collect suggestions
     $this->suggestedPackagesReporter->addSuggestionsFromPackage($package);
     $installedFromVcs = 'source' === $package->getInstallationSource();
     $io->writeError('<info>Created project in ' . $directory . '</info>');
     chdir($directory);
     $_SERVER['COMPOSER_ROOT_VERSION'] = $package->getPrettyVersion();
     putenv('COMPOSER_ROOT_VERSION=' . $_SERVER['COMPOSER_ROOT_VERSION']);
     return $installedFromVcs;
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 protected function interact(InputInterface $input, OutputInterface $output)
 {
     $git = $this->getGitConfig();
     $io = $this->getIO();
     $formatter = $this->getHelperSet()->get('formatter');
     // initialize repos if configured
     $repositories = $input->getOption('repository');
     if ($repositories) {
         $config = Factory::createConfig($io);
         $repos = array(new PlatformRepository());
         foreach ($repositories as $repo) {
             $repos[] = RepositoryFactory::fromString($io, $config, $repo);
         }
         $repos[] = RepositoryFactory::createRepo($io, $config, array('type' => 'composer', 'url' => 'https://packagist.org'));
         $this->repos = new CompositeRepository($repos);
         unset($repos, $config, $repositories);
     }
     $io->writeError(array('', $formatter->formatBlock('Welcome to the Composer config generator', 'bg=blue;fg=white', true), ''));
     // namespace
     $io->writeError(array('', 'This command will guide you through creating your composer.json config.', ''));
     $cwd = realpath(".");
     if (!($name = $input->getOption('name'))) {
         $name = basename($cwd);
         $name = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $name);
         $name = strtolower($name);
         if (isset($git['github.user'])) {
             $name = $git['github.user'] . '/' . $name;
         } elseif (!empty($_SERVER['USERNAME'])) {
             $name = $_SERVER['USERNAME'] . '/' . $name;
         } elseif (get_current_user()) {
             $name = get_current_user() . '/' . $name;
         } else {
             // package names must be in the format foo/bar
             $name = $name . '/' . $name;
         }
         $name = strtolower($name);
     } else {
         if (!preg_match('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}', $name)) {
             throw new \InvalidArgumentException('The package name ' . $name . ' is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+');
         }
     }
     $name = $io->askAndValidate('Package name (<vendor>/<name>) [<comment>' . $name . '</comment>]: ', function ($value) use($name) {
         if (null === $value) {
             return $name;
         }
         if (!preg_match('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}', $value)) {
             throw new \InvalidArgumentException('The package name ' . $value . ' is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+');
         }
         return $value;
     }, null, $name);
     $input->setOption('name', $name);
     $description = $input->getOption('description') ?: false;
     $description = $io->ask('Description [<comment>' . $description . '</comment>]: ', $description);
     $input->setOption('description', $description);
     if (null === ($author = $input->getOption('author'))) {
         if (isset($git['user.name']) && isset($git['user.email'])) {
             $author = sprintf('%s <%s>', $git['user.name'], $git['user.email']);
         }
     }
     $self = $this;
     $author = $io->askAndValidate('Author [<comment>' . $author . '</comment>, n to skip]: ', function ($value) use($self, $author) {
         if ($value === 'n' || $value === 'no') {
             return;
         }
         $value = $value ?: $author;
         $author = $self->parseAuthorString($value);
         return sprintf('%s <%s>', $author['name'], $author['email']);
     }, null, $author);
     $input->setOption('author', $author);
     $minimumStability = $input->getOption('stability') ?: null;
     $minimumStability = $io->askAndValidate('Minimum Stability [<comment>' . $minimumStability . '</comment>]: ', function ($value) use($self, $minimumStability) {
         if (null === $value) {
             return $minimumStability;
         }
         if (!isset(BasePackage::$stabilities[$value])) {
             throw new \InvalidArgumentException('Invalid minimum stability "' . $value . '". Must be empty or one of: ' . implode(', ', array_keys(BasePackage::$stabilities)));
         }
         return $value;
     }, null, $minimumStability);
     $input->setOption('stability', $minimumStability);
     $type = $input->getOption('type') ?: false;
     $type = $io->ask('Package Type (e.g. library, project, metapackage, composer-plugin) [<comment>' . $type . '</comment>]: ', $type);
     $input->setOption('type', $type);
     $license = $input->getOption('license') ?: false;
     $license = $io->ask('License [<comment>' . $license . '</comment>]: ', $license);
     $input->setOption('license', $license);
     $io->writeError(array('', 'Define your dependencies.', ''));
     $question = 'Would you like to define your dependencies (require) interactively [<comment>yes</comment>]? ';
     $requirements = array();
     if ($io->askConfirmation($question, true)) {
         $requirements = $this->determineRequirements($input, $output, $input->getOption('require'));
     }
     $input->setOption('require', $requirements);
     $question = 'Would you like to define your dev dependencies (require-dev) interactively [<comment>yes</comment>]? ';
     $devRequirements = array();
     if ($io->askConfirmation($question, true)) {
         $devRequirements = $this->determineRequirements($input, $output, $input->getOption('require-dev'));
     }
     $input->setOption('require-dev', $devRequirements);
 }
 protected function installRootPackage(IOInterface $io, Config $config, $packageName, $directory = null, $packageVersion = null, $stability = 'stable', $preferSource = false, $preferDist = false, $installDevPackages = false, $repository = null, $disablePlugins = false, $noScripts = false, $keepVcs = false, $noProgress = false)
 {
     if (null === $repository) {
         $sourceRepo = new CompositeRepository(RepositoryFactory::defaultRepos($io, $config));
     } else {
         $sourceRepo = RepositoryFactory::fromString($io, $config, $repository, true);
     }
     $parser = new VersionParser();
     $requirements = $parser->parseNameVersionPairs(array($packageName));
     $name = strtolower($requirements[0]['name']);
     if (!$packageVersion && isset($requirements[0]['version'])) {
         $packageVersion = $requirements[0]['version'];
     }
     if (null === $stability) {
         if (preg_match('{^[^,\\s]*?@(' . implode('|', array_keys(BasePackage::$stabilities)) . ')$}i', $packageVersion, $match)) {
             $stability = $match[1];
         } else {
             $stability = VersionParser::parseStability($packageVersion);
         }
     }
     $stability = VersionParser::normalizeStability($stability);
     if (!isset(BasePackage::$stabilities[$stability])) {
         throw new \InvalidArgumentException('Invalid stability provided (' . $stability . '), must be one of: ' . implode(', ', array_keys(BasePackage::$stabilities)));
     }
     $pool = new Pool($stability);
     $pool->addRepository($sourceRepo);
     // using those 3 constants to build a version without the 'extra' bit that can contain garbage
     $phpVersion = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;
     // find the latest version if there are multiple
     $versionSelector = new VersionSelector($pool);
     $package = $versionSelector->findBestCandidate($name, $packageVersion, $phpVersion, $stability);
     if (!$package) {
         throw new \InvalidArgumentException("Could not find package {$name}" . ($packageVersion ? " with version {$packageVersion}." : " with stability {$stability}."));
     }
     if (null === $directory) {
         $parts = explode("/", $name, 2);
         $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts);
     }
     // handler Ctrl+C for unix-like systems
     if (function_exists('pcntl_signal')) {
         declare (ticks=100);
         pcntl_signal(SIGINT, function () use($directory) {
             $fs = new Filesystem();
             $fs->removeDirectory($directory);
             exit(130);
         });
     }
     $io->writeError('<info>Installing ' . $package->getName() . ' (' . $package->getFullPrettyVersion(false) . ')</info>');
     if ($disablePlugins) {
         $io->writeError('<info>Plugins have been disabled.</info>');
     }
     if (0 === strpos($package->getPrettyVersion(), 'dev-') && in_array($package->getSourceType(), array('git', 'hg'))) {
         $package->setSourceReference(substr($package->getPrettyVersion(), 4));
     }
     $dm = $this->createDownloadManager($io, $config);
     $dm->setPreferSource($preferSource)->setPreferDist($preferDist)->setOutputProgress(!$noProgress);
     $projectInstaller = new ProjectInstaller($directory, $dm);
     $im = $this->createInstallationManager();
     $im->addInstaller($projectInstaller);
     $im->install(new InstalledFilesystemRepository(new JsonFile('php://memory')), new InstallOperation($package));
     $im->notifyInstalls($io);
     $installedFromVcs = 'source' === $package->getInstallationSource();
     $io->writeError('<info>Created project in ' . $directory . '</info>');
     chdir($directory);
     $_SERVER['COMPOSER_ROOT_VERSION'] = $package->getPrettyVersion();
     putenv('COMPOSER_ROOT_VERSION=' . $_SERVER['COMPOSER_ROOT_VERSION']);
     return $installedFromVcs;
 }