Browse Source

Add name inflection.

Commands will use their names when creating an option parser.
Mark Story 8 years ago
parent
commit
3014cf0399
2 changed files with 34 additions and 1 deletions
  1. 22 0
      src/Console/Command.php
  2. 12 1
      tests/TestCase/Console/CommandTest.php

+ 22 - 0
src/Console/Command.php

@@ -28,6 +28,13 @@ class Command
     use ModelAwareTrait;
 
     /**
+     * The name of this command. Inflected from the class name.
+     *
+     * @var string
+     */
+    protected $name;
+
+    /**
      * Constructor
      *
      * By default CakePHP will construct command objects when
@@ -37,6 +44,21 @@ class Command
     {
         $locator = $this->getTableLocator() ? : 'Cake\ORM\TableRegistry';
         $this->modelFactory('Table', [$locator, 'get']);
+
+        if (!$this->name) {
+            list(, $class) = namespaceSplit(get_class($this));
+            $this->name = str_replace('Command', '', $class);
+        }
+    }
+
+    /**
+     * Get the command name.
+     *
+     * @return string
+     */
+    public function getName()
+    {
+        return $this->name;
     }
 
     /**

+ 12 - 1
tests/TestCase/Console/CommandTest.php

@@ -18,7 +18,7 @@ use Cake\Console\Command;
 use Cake\ORM\Locator\TableLocator;
 use Cake\ORM\Table;
 use Cake\TestSuite\TestCase;
-
+use TestApp\Command\ExampleCommand;
 
 /**
  * Test case for Console\Command
@@ -48,4 +48,15 @@ class CommandTest extends TestCase
         $command->loadModel('Comments');
         $this->assertInstanceOf(Table::class, $command->Comments);
     }
+
+    /**
+     * Test name inflection
+     *
+     * @return void
+     */
+    public function testNameInflection()
+    {
+        $command = new ExampleCommand();
+        $this->assertSame('Example', $command->getName());
+    }
 }