Browse Source

Improve the Command Task reflection ability

To be more in sync with real live usage of the autocompletion through the CompletionShell, the plugin dot notation is only mandatory if there are multiple shells with the same name.

If plugins implement a Shell which has a unique name accross the core, the app and other plugins, the shell can be used without the dot notation. The CompletionShell should reflect that.
Yves P 10 years ago
parent
commit
e7639ffd49
1 changed files with 16 additions and 2 deletions
  1. 16 2
      src/Shell/Task/CommandTask.php

+ 16 - 2
src/Shell/Task/CommandTask.php

@@ -19,6 +19,7 @@ use Cake\Console\Shell;
 use Cake\Core\App;
 use Cake\Core\Plugin;
 use Cake\Filesystem\Folder;
+use Cake\Utility\Hash;
 use Cake\Utility\Inflector;
 use ReflectionClass;
 use ReflectionMethod;
@@ -109,11 +110,14 @@ class CommandTask extends Shell
     public function commands()
     {
         $shellList = $this->getShellList();
+        $flatten = Hash::flatten($shellList);
+        $duplicates = array_intersect($flatten, array_unique(array_diff_key($flatten, array_unique($flatten))));
+        $duplicates = Hash::expand($duplicates);
 
         $options = [];
         foreach ($shellList as $type => $commands) {
             $prefix = '';
-            if (!in_array(strtolower($type), ['app', 'core'])) {
+            if (!in_array(strtolower($type), ['app', 'core']) && isset($duplicates[$type])) {
                 $prefix = $type . '.';
             }
 
@@ -143,7 +147,7 @@ class CommandTask extends Shell
         $return = array_keys($taskMap);
         $return = array_map('Cake\Utility\Inflector::underscore', $return);
 
-        $shellMethodNames = ['main', 'help', 'getOptionParser'];
+        $shellMethodNames = ['main', 'help', 'getOptionParser', 'initialize', 'runCommand'];
 
         $baseClasses = ['Object', 'Shell', 'AppShell'];
 
@@ -182,6 +186,16 @@ class CommandTask extends Shell
             return false;
         }
 
+        if (empty($pluginDot)) {
+            $shellList = $this->getShellList();
+            unset($shellList['CORE'], $shellList['app']);
+            foreach ($shellList as $plugin => $commands) {
+                if (in_array($commandName, $commands)) {
+                    $pluginDot = $plugin . '.';
+                }
+            }
+        }
+
         $name = Inflector::camelize($name);
         $pluginDot = Inflector::camelize($pluginDot);
         $class = App::className($pluginDot . $name, 'Shell', 'Shell');