Skip to content

Commit

Permalink
Add name inflection.
Browse files Browse the repository at this point in the history
Commands will use their names when creating an option parser.
  • Loading branch information
markstory committed Sep 26, 2017
1 parent c9ef4b6 commit 3014cf0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Console/Command.php
Expand Up @@ -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
*
Expand All @@ -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;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion tests/TestCase/Console/CommandTest.php
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 3014cf0

Please sign in to comment.