Browse Source

Make autoDiscover() return an array.

Returning an array of information allows users to manipulate the
discovered commands before adding the commands to the collection.
Mark Story 8 years ago
parent
commit
39f494367b
2 changed files with 32 additions and 18 deletions
  1. 29 15
      src/Console/CommandCollection.php
  2. 3 3
      tests/TestCase/Console/CommandCollectionTest.php

+ 29 - 15
src/Console/CommandCollection.php

@@ -73,6 +73,22 @@ class CommandCollection implements IteratorAggregate, Countable
     }
 
     /**
+     * Add multiple commands at once.
+     *
+     * @param array $commands A map of command names => command classes/instances.
+     * @return $this
+     * @see \Cake\Console\CommandCollection::add()
+     */
+    public function addMany(array $commands)
+    {
+        foreach ($commands as $name => $class) {
+            $this->add($name, $class);
+        }
+
+        return $this;
+    }
+
+    /**
      * Remove a command from the collection if it exists.
      *
      * @param string $name The named shell.
@@ -150,16 +166,16 @@ class CommandCollection implements IteratorAggregate, Countable
      * the full `plugin_name.shell` name will be used. Plugin shells are added
      * in the order that plugins were loaded.
      *
-     * @return $this
+     * @return array An array of command names and their classes.
      */
     public function autoDiscover()
     {
         $scanner = new CommandScanner();
         $shells = $scanner->scanAll();
 
-        $adder = function ($shells, $key) {
+        $adder = function ($out, $shells, $key) {
             if (empty($shells[$key])) {
-                return;
+                return $out;
             }
             foreach ($shells[$key] as $info) {
                 $name = $info['name'];
@@ -168,27 +184,25 @@ class CommandCollection implements IteratorAggregate, Countable
                 // If the short name has been used, use the full name.
                 // This allows app shells to have name preference.
                 // and app shells to overwrite core shells.
-                if ($this->has($name) && $addLong) {
+                if (isset($out[$name]) && $addLong) {
                     $name = $info['fullName'];
                 }
 
-                try {
-                    $this->add($name, $info['class']);
-                    if ($addLong) {
-                        $this->add($info['fullName'], $info['class']);
-                    }
-                } catch (InvalidArgumentException $e) {
-                    Log::warning("Could not add {$info['class']} via autodiscovery. " . $e->getMessage());
+                $out[$name] = $info['class'];
+                if ($addLong) {
+                    $out[$info['fullName']] = $info['class'];
                 }
             }
+
+            return $out;
         };
 
-        $adder($shells, 'CORE');
-        $adder($shells, 'app');
+        $out = $adder([], $shells, 'CORE');
+        $out = $adder($out, $shells, 'app');
         foreach (array_keys($shells['plugins']) as $key) {
-            $adder($shells['plugins'], $key);
+            $out = $adder($out, $shells['plugins'], $key);
         }
 
-        return $this;
+        return $out;
     }
 }

+ 3 - 3
tests/TestCase/Console/CommandCollectionTest.php

@@ -186,7 +186,7 @@ class CommandCollectionTest extends TestCase
     public function testAutoDiscoverApp()
     {
         $collection = new CommandCollection();
-        $this->assertSame($collection, $collection->autoDiscover());
+        $collection->addMany($collection->autoDiscover());
 
         $this->assertTrue($collection->has('i18m'));
         $this->assertTrue($collection->has('sample'));
@@ -204,7 +204,7 @@ class CommandCollectionTest extends TestCase
     public function testAutoDiscoverCore()
     {
         $collection = new CommandCollection();
-        $collection->autoDiscover();
+        $collection->addMany($collection->autoDiscover());
 
         $this->assertTrue($collection->has('routes'));
         $this->assertTrue($collection->has('i18n'));
@@ -228,7 +228,7 @@ class CommandCollectionTest extends TestCase
         Plugin::load('TestPlugin');
         Plugin::load('Company/TestPluginThree');
         $collection = new CommandCollection();
-        $collection->autoDiscover();
+        $collection->addMany($collection->autoDiscover());
 
         $this->assertTrue(
             $collection->has('example'),