Skip to content

Commit 3014cf0

Browse files
committed
Add name inflection.
Commands will use their names when creating an option parser.
1 parent c9ef4b6 commit 3014cf0

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Console/Command.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class Command
2727
use LogTrait;
2828
use ModelAwareTrait;
2929

30+
/**
31+
* The name of this command. Inflected from the class name.
32+
*
33+
* @var string
34+
*/
35+
protected $name;
36+
3037
/**
3138
* Constructor
3239
*
@@ -37,6 +44,21 @@ public function __construct()
3744
{
3845
$locator = $this->getTableLocator() ? : 'Cake\ORM\TableRegistry';
3946
$this->modelFactory('Table', [$locator, 'get']);
47+
48+
if (!$this->name) {
49+
list(, $class) = namespaceSplit(get_class($this));
50+
$this->name = str_replace('Command', '', $class);
51+
}
52+
}
53+
54+
/**
55+
* Get the command name.
56+
*
57+
* @return string
58+
*/
59+
public function getName()
60+
{
61+
return $this->name;
4062
}
4163

4264
/**

tests/TestCase/Console/CommandTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Cake\ORM\Locator\TableLocator;
1919
use Cake\ORM\Table;
2020
use Cake\TestSuite\TestCase;
21-
21+
use TestApp\Command\ExampleCommand;
2222

2323
/**
2424
* Test case for Console\Command
@@ -48,4 +48,15 @@ public function testConstructorLoadModel()
4848
$command->loadModel('Comments');
4949
$this->assertInstanceOf(Table::class, $command->Comments);
5050
}
51+
52+
/**
53+
* Test name inflection
54+
*
55+
* @return void
56+
*/
57+
public function testNameInflection()
58+
{
59+
$command = new ExampleCommand();
60+
$this->assertSame('Example', $command->getName());
61+
}
5162
}

0 commit comments

Comments
 (0)