Browse Source

Remove the `$io` parameter from a factory method.

Robert Pustułka 8 years ago
parent
commit
40a4bdcd56

+ 2 - 7
src/Console/CommandFactory.php

@@ -26,15 +26,10 @@ class CommandFactory implements CommandFactoryInterface
     /**
      * {@inheritDoc}
      */
-    public function create($className, ConsoleIo $io)
+    public function create($className)
     {
-        if (is_subclass_of($className, Shell::class)) {
-            return new $className($io);
-        }
-
-        // Command class
         $command = new $className();
-        if (!$command instanceof Command) {
+        if (!($command instanceof Command) && !($command instanceof Shell)) {
             $valid = implode('` or `', [Shell::class, Command::class]);
             $message = sprintf('Class `%s` must be an instance of `%s`.', $className, $valid);
             throw new InvalidArgumentException($message);

+ 1 - 2
src/Console/CommandFactoryInterface.php

@@ -21,8 +21,7 @@ interface CommandFactoryInterface
      * The factory method for creating Command and Shell instances.
      *
      * @param string $className Command/Shell class name.
-     * @param \Cake\Console\ConsoleIo $io The IO wrapper for the created shell class.
      * @return \Cake\Console\Shell|\Cake\Console\Command
      */
-    public function create($className, ConsoleIo $io);
+    public function create($className);
 }

+ 6 - 1
src/Console/CommandRunner.php

@@ -285,6 +285,11 @@ class CommandRunner implements EventDispatcherInterface
      */
     protected function createShell($className, ConsoleIo $io)
     {
-        return $this->factory->create($className, $io);
+        $shell = $this->factory->create($className);
+        if ($shell instanceof Shell) {
+            $shell->setIo($io);
+        }
+
+        return $shell;
     }
 }

+ 3 - 6
tests/TestCase/Console/CommandFactoryTest.php

@@ -25,7 +25,7 @@ class CommandFactoryTest extends TestCase
     {
         $factory = new CommandFactory();
 
-        $command = $factory->create(DemoCommand::class, new ConsoleIo());
+        $command = $factory->create(DemoCommand::class);
         $this->assertInstanceOf(DemoCommand::class, $command);
     }
 
@@ -33,11 +33,8 @@ class CommandFactoryTest extends TestCase
     {
         $factory = new CommandFactory();
 
-        $io = new ConsoleIo();
-        $shell = $factory->create(SampleShell::class, $io);
-
+        $shell = $factory->create(SampleShell::class);
         $this->assertInstanceOf(SampleShell::class, $shell);
-        $this->assertSame($io, $shell->getIo());
     }
 
     public function testInvalid()
@@ -47,6 +44,6 @@ class CommandFactoryTest extends TestCase
         $this->expectException(InvalidArgumentException::class);
         $this->expectExceptionMessage('Class `Cake\Test\TestCase\Console\CommandFactoryTest` must be an instance of `Cake\Console\Shell` or `Cake\Console\Command`.');
 
-        $factory->create(static::class, new ConsoleIo());
+        $factory->create(static::class);
     }
 }

+ 1 - 1
tests/TestCase/Console/CommandRunnerTest.php

@@ -332,7 +332,7 @@ class CommandRunnerTest extends TestCase
         $factory = $this->createMock(CommandFactoryInterface::class);
         $factory->expects($this->once())
             ->method('create')
-            ->with(DemoCommand::class, $io)
+            ->with(DemoCommand::class)
             ->willReturn(new DemoCommand());
 
         $app = $this->makeAppWithCommands(['ex' => DemoCommand::class]);