123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
- use Composer\Factory;
- use Composer\Installer;
- use Composer\Json\JsonFile;
- use Composer\Json\JsonManipulator;
- use Composer\Package\Version\VersionParser;
- use Composer\Plugin\CommandEvent;
- use Composer\Plugin\PluginEvents;
- /**
- * @author Jérémy Romey <jeremy@free-agent.fr>
- * @author Jordi Boggiano <j.boggiano@seld.be>
- */
- class RequireCommand extends InitCommand
- {
- protected function configure()
- {
- $this
- ->setName('require')
- ->setDescription('Adds required packages to your composer.json and installs them')
- ->setDefinition(array(
- new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Required package with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"'),
- new InputOption('dev', null, InputOption::VALUE_NONE, 'Add requirement to require-dev.'),
- new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'),
- new InputOption('prefer-dist', null, InputOption::VALUE_NONE, 'Forces installation from package dist even for dev versions.'),
- new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'),
- new InputOption('no-update', null, InputOption::VALUE_NONE, 'Disables the automatic update of the dependencies.'),
- ))
- ->setHelp(<<<EOT
- The require command adds required packages to your composer.json and installs them
- If you do not want to install the new dependencies immediately you can call it with --no-update
- EOT
- )
- ;
- }
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $file = Factory::getComposerFile();
- if (!file_exists($file) && !file_put_contents($file, "{\n}\n")) {
- $output->writeln('<error>'.$file.' could not be created.</error>');
- return 1;
- }
- if (!is_readable($file)) {
- $output->writeln('<error>'.$file.' is not readable.</error>');
- return 1;
- }
- if (!is_writable($file)) {
- $output->writeln('<error>'.$file.' is not writable.</error>');
- return 1;
- }
- $json = new JsonFile($file);
- $composer = $json->read();
- $composerBackup = file_get_contents($json->getPath());
- $requirements = $this->determineRequirements($input, $output, $input->getArgument('packages'));
- $requireKey = $input->getOption('dev') ? 'require-dev' : 'require';
- $baseRequirements = array_key_exists($requireKey, $composer) ? $composer[$requireKey] : array();
- $requirements = $this->formatRequirements($requirements);
- // validate requirements format
- $versionParser = new VersionParser();
- foreach ($requirements as $constraint) {
- $versionParser->parseConstraints($constraint);
- }
- if (!$this->updateFileCleanly($json, $baseRequirements, $requirements, $requireKey)) {
- foreach ($requirements as $package => $version) {
- $baseRequirements[$package] = $version;
- }
- $composer[$requireKey] = $baseRequirements;
- $json->write($composer);
- }
- $output->writeln('<info>'.$file.' has been updated</info>');
- if ($input->getOption('no-update')) {
- return 0;
- }
- // Update packages
- $composer = $this->getComposer();
- $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
- $io = $this->getIO();
- $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'require', $input, $output);
- $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
- $install = Installer::create($io, $composer);
- $install
- ->setVerbose($input->getOption('verbose'))
- ->setPreferSource($input->getOption('prefer-source'))
- ->setPreferDist($input->getOption('prefer-dist'))
- ->setDevMode(true)
- ->setUpdate(true)
- ->setUpdateWhitelist(array_keys($requirements));
- ;
- if (!$install->run()) {
- $output->writeln("\n".'<error>Installation failed, reverting '.$file.' to its original content.</error>');
- file_put_contents($json->getPath(), $composerBackup);
- return 1;
- }
- return 0;
- }
- private function updateFileCleanly($json, array $base, array $new, $requireKey)
- {
- $contents = file_get_contents($json->getPath());
- $manipulator = new JsonManipulator($contents);
- foreach ($new as $package => $constraint) {
- if (!$manipulator->addLink($requireKey, $package, $constraint)) {
- return false;
- }
- }
- file_put_contents($json->getPath(), $manipulator->getContents());
- return true;
- }
- protected function interact(InputInterface $input, OutputInterface $output)
- {
- return;
- }
- }
|