Browse Source

Merge pull request #5830 from fabpot/logic-refactoring

Refactor some common logic in Command classes
Jordi Boggiano 8 years ago
parent
commit
f7475c6ff6

+ 36 - 0
src/Composer/Command/BaseCommand.php

@@ -13,6 +13,7 @@
 namespace Composer\Command;
 
 use Composer\Composer;
+use Composer\Config;
 use Composer\Console\Application;
 use Composer\IO\IOInterface;
 use Composer\IO\NullIO;
@@ -128,4 +129,39 @@ abstract class BaseCommand extends Command
 
         parent::initialize($input, $output);
     }
+
+    /**
+     * Returns preferSource and preferDist values based on the configuration.
+     *
+     * @param Config         $config
+     * @param InputInterface $input
+     * @param bool           $keepVcsRequiresPreferSource
+     *
+     * @return bool[] An array composed of the preferSource and preferDist values
+     */
+    protected function getPreferredInstallOptions(Config $config, InputInterface $input, $keepVcsRequiresPreferSource = false)
+    {
+        $preferSource = false;
+        $preferDist = false;
+
+        switch ($config->get('preferred-install')) {
+            case 'source':
+                $preferSource = true;
+                break;
+            case 'dist':
+                $preferDist = true;
+                break;
+            case 'auto':
+            default:
+                // noop
+                break;
+        }
+
+        if ($input->getOption('prefer-source') || $input->getOption('prefer-dist') || ($keepVcsRequiresPreferSource && $input->hasOption('keep-vcs') && $input->getOption('keep-vcs'))) {
+            $preferSource = $input->getOption('prefer-source') || ($keepVcsRequiresPreferSource && $input->hasOption('keep-vcs') && $input->getOption('keep-vcs'));
+            $preferDist = $input->getOption('prefer-dist');
+        }
+
+        return array($preferSource, $preferDist);
+    }
 }

+ 2 - 35
src/Composer/Command/CreateProjectCommand.php

@@ -111,7 +111,7 @@ EOT
         $config = Factory::createConfig();
         $io = $this->getIO();
 
-        $this->updatePreferredOptions($config, $input, $preferSource, $preferDist, true);
+        list($preferSource, $preferDist) = $this->getPreferredInstallOptions($config, $input, true);
 
         if ($input->getOption('dev')) {
             $io->writeError('<warning>You are using the deprecated option "dev". Dev packages are installed by default now.</warning>');
@@ -168,8 +168,7 @@ EOT
             $composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_ROOT_PACKAGE_INSTALL, $installDevPackages);
         }
 
-        $rootPackageConfig = $composer->getConfig();
-        $this->updatePreferredOptions($rootPackageConfig, $input, $preferSource, $preferDist);
+        list($preferSource, $preferDist) = $this->getPreferredInstallOptions($composer->getConfig(), $input);
 
         // install dependencies of the created project
         if ($noInstall === false) {
@@ -371,36 +370,4 @@ EOT
     {
         return new InstallationManager();
     }
-
-    /**
-     * Updated preferSource or preferDist based on the preferredInstall config option
-     * @param Config         $config
-     * @param InputInterface $input
-     * @param bool           $preferSource
-     * @param bool           $preferDist
-     * @param bool           $keepVcsRequiresPreferSource
-     */
-    protected function updatePreferredOptions(Config $config, InputInterface $input, &$preferSource, &$preferDist, $keepVcsRequiresPreferSource = false)
-    {
-        $preferSource = false;
-        $preferDist = false;
-
-        switch ($config->get('preferred-install')) {
-            case 'source':
-                $preferSource = true;
-                break;
-            case 'dist':
-                $preferDist = true;
-                break;
-            case 'auto':
-            default:
-                // noop
-                break;
-        }
-
-        if ($input->getOption('prefer-source') || $input->getOption('prefer-dist') || ($keepVcsRequiresPreferSource && $input->getOption('keep-vcs'))) {
-            $preferSource = $input->getOption('prefer-source') || ($keepVcsRequiresPreferSource && $input->getOption('keep-vcs'));
-            $preferDist = $input->getOption('prefer-dist');
-        }
-    }
 }

+ 1 - 20
src/Composer/Command/InstallCommand.php

@@ -89,27 +89,8 @@ EOT
 
         $install = Installer::create($io, $composer);
 
-        $preferSource = false;
-        $preferDist = false;
-
         $config = $composer->getConfig();
-
-        switch ($config->get('preferred-install')) {
-            case 'source':
-                $preferSource = true;
-                break;
-            case 'dist':
-                $preferDist = true;
-                break;
-            case 'auto':
-            default:
-                // noop
-                break;
-        }
-        if ($input->getOption('prefer-source') || $input->getOption('prefer-dist')) {
-            $preferSource = $input->getOption('prefer-source');
-            $preferDist = $input->getOption('prefer-dist');
-        }
+        list($preferSource, $preferDist) = $this->getPreferredInstallOptions($config, $input);
 
         $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader');
         $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative');

+ 1 - 20
src/Composer/Command/UpdateCommand.php

@@ -123,27 +123,8 @@ EOT
 
         $install = Installer::create($io, $composer);
 
-        $preferSource = false;
-        $preferDist = false;
-
         $config = $composer->getConfig();
-
-        switch ($config->get('preferred-install')) {
-            case 'source':
-                $preferSource = true;
-                break;
-            case 'dist':
-                $preferDist = true;
-                break;
-            case 'auto':
-            default:
-                // noop
-                break;
-        }
-        if ($input->getOption('prefer-source') || $input->getOption('prefer-dist')) {
-            $preferSource = $input->getOption('prefer-source');
-            $preferDist = $input->getOption('prefer-dist');
-        }
+        list($preferSource, $preferDist) = $this->getPreferredInstallOptions($config, $input);
 
         $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader');
         $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative');