Browse Source

Update default command name generation code.

This avoid needs for additional name manipulation in CommandScanner.
ADmad 6 years ago
parent
commit
bfdee0f074

+ 7 - 4
src/Console/Command.php

@@ -112,11 +112,14 @@ class Command
      */
     public static function defaultName(): string
     {
-        $class = static::class;
-        $class = preg_replace('/Command$/', '', $class);
-        $class = Inflector::underscore(substr($class, (int)strripos($class, '\\') + 1));
+        $pos = strrpos(static::class, '\\');
+        $name = substr(static::class, $pos + 1, -7);
 
-        return $class;
+        return str_replace(
+            '_',
+            ' ',
+            Inflector::underscore($name)
+        );
     }
 
     /**

+ 0 - 24
src/Console/CommandScanner.php

@@ -49,35 +49,11 @@ class CommandScanner
             '',
             ['command_list']
         );
-        $coreCommands = $this->inflectCommandNames($coreCommands);
 
         return array_merge($coreShells, $coreCommands);
     }
 
     /**
-     * Inflect multi-word command names based on conventions
-     *
-     * @param array $commands The array of command metadata to mutate
-     * @return array The updated command metadata
-     * @see \Cake\Console\CommandScanner::scanDir()
-     */
-    protected function inflectCommandNames(array $commands): array
-    {
-        foreach ($commands as $i => $command) {
-            $file = basename($command['file'], '.php');
-            $name = Inflector::underscore(preg_replace('/(Shell|Command)$/', '', $file));
-            //if names are equal, we did not use the defaultName()
-            if ($name === $command['name']) {
-                $command['name'] = str_replace('_', ' ', $command['name']);
-                $command['fullName'] = str_replace('_', ' ', $command['fullName']);
-            }
-            $commands[$i] = $command;
-        }
-
-        return $commands;
-    }
-
-    /**
      * Scan the application for shells & commands.
      *
      * @return array A list of command metadata.

+ 5 - 0
tests/test_app/TestApp/Command/AutoLoadModelCommand.php

@@ -8,4 +8,9 @@ use Cake\Console\Command;
 class AutoLoadModelCommand extends Command
 {
     public $modelClass = 'Posts';
+
+    public static function defaultName(): string
+    {
+        return 'auto_load_model';
+    }
 }