瀏覽代碼

Composer\Command dependencies (application and io) should can now be injected. This way, Composer\Command objects can be used in a regular symfony2 Console\Application.

Paul Seiffert 13 年之前
父節點
當前提交
51e165c6c5
共有 1 個文件被更改,包括 47 次插入5 次删除
  1. 47 5
      src/Composer/Command/Command.php

+ 47 - 5
src/Composer/Command/Command.php

@@ -13,6 +13,7 @@
 namespace Composer\Command;
 namespace Composer\Command;
 
 
 use Symfony\Component\Console\Command\Command as BaseCommand;
 use Symfony\Component\Console\Command\Command as BaseCommand;
+use Composer\Console\Application as ComposerApplication;
 
 
 /**
 /**
  * Base class for Composer commands
  * Base class for Composer commands
@@ -23,18 +24,59 @@ use Symfony\Component\Console\Command\Command as BaseCommand;
 abstract class Command extends BaseCommand
 abstract class Command extends BaseCommand
 {
 {
     /**
     /**
-     * @return \Composer\Composer
+     * @var \Composer\Composer
      */
      */
-    protected function getComposer($required = true)
+    private $composer;
+
+    /**
+     * @var \Composer\IO\IOInterface
+     */
+    private $io;
+
+    /**
+     * @param   bool                $required
+     * @return  \Composer\Composer
+     */
+    public function getComposer($required = true)
+    {
+        if (null === $this->composer) {
+            $application = $this->getApplication();
+            if ($application instanceof ComposerApplication) {
+                /* @var $application    ComposerApplication */
+                $this->composer = $application->getComposer();
+            }
+        }
+        return $this->composer;
+    }
+
+    /**
+     * @param   \Composer\Composer  $composer
+     */
+    public function setComposer(\Composer\Composer $composer)
     {
     {
-        return $this->getApplication()->getComposer($required);
+        $this->composer = $composer;
     }
     }
 
 
     /**
     /**
      * @return \Composer\IO\ConsoleIO
      * @return \Composer\IO\ConsoleIO
      */
      */
-    protected function getIO()
+    public function getIO()
+    {
+        if (null === $this->io) {
+            $application = $this->getApplication();
+            if ($application instanceof ComposerApplication) {
+                /* @var $application    ComposerApplication */
+                $this->io = $application->getIO();
+            }
+        }
+        return $this->io;
+    }
+
+    /**
+     * @param   \Composer\IO\IOInterface    $io
+     */
+    public function setIO(\Composer\IO\IOInterface $io)
     {
     {
-        return $this->getApplication()->getIO();
+        $this->io = $io;
     }
     }
 }
 }