From f06c0cb58016ac316ee3467ae017304692c52a3b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 1 Mar 2013 23:47:24 +0100 Subject: [PATCH] Code reorgs and make bool values real booleans, refs #1637 --- doc/04-schema.md | 6 ++++-- res/composer-schema.json | 4 ++-- src/Composer/Command/ConfigCommand.php | 13 ++++++++---- src/Composer/Config.php | 8 ++++---- src/Composer/Downloader/GitDownloader.php | 25 ++++++++++------------- src/Composer/Downloader/SvnDownloader.php | 13 ++++-------- 6 files changed, 34 insertions(+), 35 deletions(-) diff --git a/doc/04-schema.md b/doc/04-schema.md index 4a3927db6..ed585b76d 100644 --- a/doc/04-schema.md +++ b/doc/04-schema.md @@ -623,8 +623,10 @@ The following options are supported: define a notification URL, so that they get notified whenever a package from that repository is installed. This option allows you to disable that behaviour. * **discard-changes:** Defaults to `false` and can be any of `true`, `false` or - `stash`. This option allows you to set the default style of handling dirty - updates, specially useful for non-interactive mode. + `"stash"`. This option allows you to set the default style of handling dirty + updates when in non-interactive mode. `true` will always discard changes in + vendors, while `"stash"` will try to stash and reapply. Use this for CI + servers or deploy scripts if you tend to have modified vendors. Example: diff --git a/res/composer-schema.json b/res/composer-schema.json index f11ff824c..e8c0474e7 100644 --- a/res/composer-schema.json +++ b/res/composer-schema.json @@ -169,8 +169,8 @@ "description": "The cache max size for the files cache, defaults to \"300MiB\"." }, "discard-changes": { - "type": ["string"], - "description": "The default style of handling dirty updates, defaults to \"false\" and can be any of \"true\", \"false\" or \"stash\"." + "type": ["string", "boolean"], + "description": "The default style of handling dirty updates, defaults to false and can be any of true, false or \"stash\"." } } }, diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 1a679fcae..4c2e9e5d8 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -252,11 +252,11 @@ EOT $uniqueConfigValues = array( 'process-timeout' => array('is_numeric', 'intval'), 'use-include-path' => array( - function ($val) { return true; }, + function ($val) { return in_array($val, array('true', 'false', '1', '0'), true); }, function ($val) { return $val !== 'false' && (bool) $val; } ), 'notify-on-install' => array( - function ($val) { return true; }, + function ($val) { return in_array($val, array('true', 'false', '1', '0'), true); }, function ($val) { return $val !== 'false' && (bool) $val; } ), 'vendor-dir' => array('is_string', function ($val) { return $val; }), @@ -272,8 +272,13 @@ EOT function ($val) { return $val; } ), 'discard-changes' => array( - function ($val) { return in_array($val, array('true', 'false', 'stash')); }, - function ($val) { return $val; } + function ($val) { return in_array($val, array('stash', 'true', 'false', '1', '0'), true); }, + function ($val) { + if ('stash' === $val) { + return 'stash'; + } + return $val !== 'false' && (bool) $val; + } ), ); $multiConfigValues = array( diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 5b6fc72e8..0d9aa22b7 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -33,7 +33,7 @@ class Config 'cache-ttl' => 15552000, // 6 months 'cache-files-ttl' => null, // fallback to cache-ttl 'cache-files-maxsize' => '300MiB', - 'discard-changes' => 'false', + 'discard-changes' => false, ); public static $defaultRepositories = array( @@ -145,7 +145,7 @@ class Config case 'cache-files-maxsize': if (!preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $this->config[$key], $matches)) { throw new \RuntimeException( - "Could not parse the value of 'cache-files-maxsize' from your config: {$this->config[$key]}" + "Could not parse the value of 'cache-files-maxsize': {$this->config[$key]}" ); } $size = $matches[1]; @@ -176,9 +176,9 @@ class Config return rtrim($this->process($this->config[$key]), '/\\'); case 'discard-changes': - if (!in_array($this->config[$key], array('true', 'false', 'stash'))) { + if (!in_array($this->config[$key], array(true, false, 'stash'), true)) { throw new \RuntimeException( - "Invalid value of 'discard-changes' from your config: {$this->config[$key]}" + "Invalid value for 'discard-changes': {$this->config[$key]}" ); } diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index ed9f2ca44..7033708a9 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -94,23 +94,20 @@ class GitDownloader extends VcsDownloader return; } - $discardChanges = $this->config->get('discard-changes'); if (!$this->io->isInteractive()) { - switch ($discardChanges) { - case 'true': - return $this->discardChanges($path); - - case 'stash': - if (!$update) { - return parent::cleanChanges($path, $update); - } - - return $this->stashChanges($path); - - case 'false': - default: + $discardChanges = $this->config->get('discard-changes'); + if (true === $discardChanges) { + return $this->discardChanges($path); + } + if ('stash' === $discardChanges) { + if (!$update) { return parent::cleanChanges($path, $update); + } + + return $this->stashChanges($path); } + + return parent::cleanChanges($path, $update); } $changes = array_map(function ($elem) { diff --git a/src/Composer/Downloader/SvnDownloader.php b/src/Composer/Downloader/SvnDownloader.php index 9547dfae0..8dde35968 100644 --- a/src/Composer/Downloader/SvnDownloader.php +++ b/src/Composer/Downloader/SvnDownloader.php @@ -92,17 +92,12 @@ class SvnDownloader extends VcsDownloader return; } - $discardChanges = $this->config->get('discard-changes'); if (!$this->io->isInteractive()) { - switch ($discardChanges) { - case 'true': - return $this->discardChanges($path); - - case 'false': - case 'stash': - default: - return parent::cleanChanges($path, $update); + if (true === $this->config->get('discard-changes')) { + return $this->discardChanges($path); } + + return parent::cleanChanges($path, $update); } $changes = array_map(function ($elem) {