diff --git a/src/Console/Command.php b/src/Console/Command.php index 8913ddf4a6e..46a5726795d 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -27,6 +27,13 @@ class Command use LogTrait; use ModelAwareTrait; + /** + * The name of this command. Inflected from the class name. + * + * @var string + */ + protected $name; + /** * Constructor * @@ -37,6 +44,21 @@ public function __construct() { $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; } /** diff --git a/tests/TestCase/Console/CommandTest.php b/tests/TestCase/Console/CommandTest.php index 5ee1f3cb766..1698bbed15c 100644 --- a/tests/TestCase/Console/CommandTest.php +++ b/tests/TestCase/Console/CommandTest.php @@ -18,7 +18,7 @@ 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 @@ public function testConstructorLoadModel() $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()); + } }