소스 검색

Allow using new code in the init command and avoid initializing the composer instance too early, refs #3096

Jordi Boggiano 10 년 전
부모
커밋
b7b0901f87
1개의 변경된 파일33개의 추가작업 그리고 16개의 파일을 삭제
  1. 33 16
      src/Composer/Command/InitCommand.php

+ 33 - 16
src/Composer/Command/InitCommand.php

@@ -302,16 +302,6 @@ EOT
         return $this->repos;
         return $this->repos;
     }
     }
 
 
-    protected function getPool()
-    {
-        if (!$this->pool) {
-            $this->pool = new Pool($this->getComposer()->getPackage()->getMinimumStability());
-            $this->pool->addRepository($this->getRepos());
-        }
-
-        return $this->pool;
-    }
-
     protected function determineRequirements(InputInterface $input, OutputInterface $output, $requires = array())
     protected function determineRequirements(InputInterface $input, OutputInterface $output, $requires = array())
     {
     {
         $dialog = $this->getHelperSet()->get('dialog');
         $dialog = $this->getHelperSet()->get('dialog');
@@ -325,7 +315,7 @@ EOT
                 if (!isset($requirement['version'])) {
                 if (!isset($requirement['version'])) {
 
 
                     // determine the best version automatically
                     // determine the best version automatically
-                    $version = $this->findBestVersionForPackage($requirement['name']);
+                    $version = $this->findBestVersionForPackage($input, $requirement['name']);
                     $requirement['version'] = $version;
                     $requirement['version'] = $version;
 
 
                     $output->writeln(sprintf(
                     $output->writeln(sprintf(
@@ -402,7 +392,7 @@ EOT
                         3)
                         3)
                     ;
                     ;
                     if (false === $constraint) {
                     if (false === $constraint) {
-                        $constraint = $this->findBestVersionForPackage($package);
+                        $constraint = $this->findBestVersionForPackage($input, $package);
 
 
                         $output->writeln(sprintf(
                         $output->writeln(sprintf(
                             'Using version <info>%s</info> for <info>%s</info>',
                             'Using version <info>%s</info> for <info>%s</info>',
@@ -534,26 +524,53 @@ EOT
         return false !== filter_var($email, FILTER_VALIDATE_EMAIL);
         return false !== filter_var($email, FILTER_VALIDATE_EMAIL);
     }
     }
 
 
+    private function getPool(InputInterface $input)
+    {
+        if (!$this->pool) {
+            $this->pool = new Pool($this->getMinimumStability($input));
+            $this->pool->addRepository($this->getRepos());
+        }
+
+        return $this->pool;
+    }
+
+    private function getMinimumStability(InputInterface $input)
+    {
+        if ($input->hasOption('stability')) {
+            return $input->getOption('stability') ?: 'stable';
+        }
+
+        $file = Factory::getComposerFile();
+        if (is_file($file) && is_readable($file) && is_array($composer = json_decode(file_get_contents($file), true))) {
+            if (!empty($composer['minimum-stability'])) {
+                return $composer['minimum-stability'];
+            }
+        }
+
+        return 'stable';
+    }
+
     /**
     /**
      * Given a package name, this determines the best version to use in the require key.
      * Given a package name, this determines the best version to use in the require key.
      *
      *
      * This returns a version with the ~ operator prefixed when possible.
      * This returns a version with the ~ operator prefixed when possible.
      *
      *
-     * @param string $name
+     * @param  InputInterface $input
+     * @param  string         $name
      * @return string
      * @return string
      * @throws \InvalidArgumentException
      * @throws \InvalidArgumentException
      */
      */
-    protected function findBestVersionForPackage($name)
+    private function findBestVersionForPackage(InputInterface $input, $name)
     {
     {
         // find the latest version allowed in this pool
         // find the latest version allowed in this pool
-        $versionSelector = new VersionSelector($this->getPool());
+        $versionSelector = new VersionSelector($this->getPool($input));
         $package = $versionSelector->findBestCandidate($name);
         $package = $versionSelector->findBestCandidate($name);
 
 
         if (!$package) {
         if (!$package) {
             throw new \InvalidArgumentException(sprintf(
             throw new \InvalidArgumentException(sprintf(
                 'Could not find package %s at any version for your minimum-stability (%s). Check the package spelling or your minimum-stability',
                 'Could not find package %s at any version for your minimum-stability (%s). Check the package spelling or your minimum-stability',
                 $name,
                 $name,
-                $this->getComposer()->getPackage()->getMinimumStability()
+                $this->getMinimumStability($input)
             ));
             ));
         }
         }