From 260d7d434f994abcb144d5b2f9a479826eb52a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81re=CC=81my=20Romey?= Date: Sun, 1 Apr 2012 00:39:43 +0200 Subject: [PATCH] Add a command for adding package to composer.json Updated doc for require command --- doc/03-cli.md | 14 ++- src/Composer/Command/RequireCommand.php | 110 ++++++++++++++++++++++++ src/Composer/Console/Application.php | 1 + src/Composer/Json/JsonFile.php | 10 +++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 src/Composer/Command/RequireCommand.php diff --git a/doc/03-cli.md b/doc/03-cli.md index 45344adea..cff1be360 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -62,6 +62,18 @@ into `composer.lock`. * **--no-install-recommends:** Do not install packages referenced by `recommend`. * **--install-suggests:** Install packages referenced by `suggest`. +## require + +The `require` command adds new packages to the `composer.json` file from the current +directory. + + $ php composer.phar require + +This will update your `composer.json` file keeping previous required packages. +If a package is already in the `composer.json` file you will be asked to choose which version you want. + +Like the `init` command, `require` allows you to search for packages. + ## search The search command allows you to search through the current project's package @@ -190,4 +202,4 @@ some tools like git or curl will only use the lower-cased `http_proxy` version. Alternatively you can also define the git proxy using `git config --global http.proxy `. -← [Libraries](02-libraries.md) | [Schema](04-schema.md) → \ No newline at end of file +← [Libraries](02-libraries.md) | [Schema](04-schema.md) → diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php new file mode 100644 index 000000000..80cc68e02 --- /dev/null +++ b/src/Composer/Command/RequireCommand.php @@ -0,0 +1,110 @@ + + * Jordi Boggiano + * + * 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\Json\JsonFile; +use Composer\Json\JsonValidationException; +use Composer\Util\RemoteFilesystem; + +/** + * @author Jérémy Romey + */ +class RequireCommand extends InitCommand +{ + protected function configure() + { + $this + ->setName('require') + ->setDescription('Adds a required package to a composer.json') + ->setDefinition(array( + new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json'), + new InputOption('require', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'An array of required packages'), + )) + ->setHelp(<<getArgument('file'); + + if (!file_exists($file)) { + $output->writeln(''.$file.' not found.'); + return 1; + } + if (!is_readable($file)) { + $output->writeln(''.$file.' is not readable.'); + return 1; + } + + $laxValid = false; + try { + $json = new JsonFile($file, new RemoteFilesystem($this->getIO())); + $json->read(); + + $json->validateSchema(JsonFile::LAX_SCHEMA); + $laxValid = true; + $json->validateSchema(); + + } catch (\Exception $e) { + $output->writeln(''.$file.' has an error. Run the validate command for more info'); + return 1; + } + + $output->writeln(array( + '', + 'Updating your dependencies.', + '' + )); + + $dialog = $this->getHelperSet()->get('dialog'); + + $options = json_decode($json->getResult(), true); + + $requirements = array(); + $requirements = $this->determineRequirements($input, $output); + + $baseRequirements = array_key_exists('require', $options) ? $options['require'] : array(); + $requirements = $this->formatRequirements($requirements); + + foreach ($requirements as $package => $version) { + if (array_key_exists($package, $baseRequirements)) { + if ($dialog->askConfirmation($output, $dialog->getQuestion('The package '.$package.' is already in requirements. Would you like to update the version required from '.$baseRequirements[$package].' to '.$version, 'yes', '?'), true)) { + $baseRequirements[$package] = $version; + } + } else { + $baseRequirements[$package] = $version; + } + } + + $options['require'] = $baseRequirements; + + $json->encode($options); + $json->write($options); + + $output->writeln(''.$file.' has been updated'); + } + + protected function interact(InputInterface $input, OutputInterface $output) + { + return; + } +} diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index bcdd1817d..7aae3869d 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -113,6 +113,7 @@ class Application extends BaseApplication $this->add(new Command\SearchCommand()); $this->add(new Command\ValidateCommand()); $this->add(new Command\ShowCommand()); + $this->add(new Command\RequireCommand()); if ('phar:' === substr(__FILE__, 0, 5)) { $this->add(new Command\SelfUpdateCommand()); diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 5c891814e..e90479d1c 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -293,4 +293,14 @@ class JsonFile throw $result; } + + /** + * Returns the content of the file + * + * @return string result + */ + public function getResult() + { + return $this->rfs->getContents($this->path, $this->path, false); + } }