Skip to content

Commit

Permalink
Clean up use of conventions trait in bake tasks.
Browse files Browse the repository at this point in the history
* Consolidate and expand use of ConventionsTrait in console classes.
* Add method for entity name generation.
  • Loading branch information
markstory committed Apr 6, 2014
1 parent 07f4f94 commit 559373a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
9 changes: 3 additions & 6 deletions src/Console/Command/Task/ControllerTask.php
Expand Up @@ -18,7 +18,6 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;

/**
* Task class for creating and updating controller files.
Expand Down Expand Up @@ -65,7 +64,6 @@ public function execute() {
}

$controller = $this->_controllerName($this->args[0]);

$this->bake($controller);
}

Expand Down Expand Up @@ -102,14 +100,13 @@ public function bakeActions($controllerName) {

$modelObj = TableRegistry::get($currentModelName);

$controllerPath = $this->_controllerPath($controllerName);
$pluralName = $this->_pluralName($currentModelName);
$singularName = Inflector::variable(Inflector::singularize($currentModelName));
$singularName = $this->_singularName($currentModelName);
$singularHumanName = $this->_singularHumanName($controllerName);
$pluralHumanName = $this->_pluralName($controllerName);

$this->Template->set(compact(
'plugin', 'admin', 'controllerPath', 'pluralName', 'singularName',
'plugin', 'admin', 'pluralName', 'singularName',
'singularHumanName', 'pluralHumanName', 'modelObj', 'currentModelName'
));
$actions = $this->Template->generate('actions', 'controller_actions');
Expand Down Expand Up @@ -190,7 +187,7 @@ public function bakeController($controllerName, $data) {
public function getPath() {
$path = parent::getPath();
if (!empty($this->params['prefix'])) {
$path .= Inflector::camelize($this->params['prefix']) . DS;
$path .= $this->_camelize($this->params['prefix']) . DS;
}
return $path;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/Task/ModelTask.php
Expand Up @@ -510,7 +510,7 @@ public function bakeEntity($model, $data = []) {
if (!empty($this->params['no-entity'])) {
return;
}
$name = Inflector::singularize($model->alias());
$name = $this->_entityName($model->alias());

$ns = Configure::read('App.namespace');
$pluginPath = '';
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/Task/TestTask.php
Expand Up @@ -370,7 +370,7 @@ protected function _addFixture($name) {
} else {
$prefix = 'app.';
}
$fixture = $prefix . Inflector::underscore(Inflector::singularize($name));
$fixture = $prefix . $this->_fixtureName($name);
$this->_fixtures[$name] = $fixture;
}

Expand Down
5 changes: 1 addition & 4 deletions src/Console/Command/Task/ViewTask.php
Expand Up @@ -181,7 +181,7 @@ public function controller($table, $controller = null) {
public function getPath() {
$path = parent::getPath();
if (!empty($this->params['prefix'])) {
$path .= Inflector::camelize($this->params['prefix']) . DS;
$path .= $this->_camelize($this->params['prefix']) . DS;
}
$path .= $this->controllerName . DS;
return $path;
Expand Down Expand Up @@ -248,9 +248,6 @@ public function all() {
protected function _loadController() {
$modelObj = TableRegistry::get($this->tableName);

$singularVar = Inflector::variable(Inflector::singularize($this->controllerName));
$singularHumanName = $this->_singularHumanName($this->controllerName);

$primaryKey = (array)$modelObj->primaryKey();
$displayField = $modelObj->displayField();
$singularVar = $this->_singularName($this->controllerName);
Expand Down
28 changes: 19 additions & 9 deletions src/Utility/ConventionsTrait.php
Expand Up @@ -24,23 +24,23 @@
trait ConventionsTrait {

/**
* Creates the proper controller path for the specified controller class name
* Creates the proper controller plural name for the specified controller class name
*
* @param string $name Controller class name
* @return string Path to controller
* @return string Controller plural name
*/
protected function _controllerPath($name) {
return Inflector::underscore($name);
protected function _controllerName($name) {
return Inflector::pluralize(Inflector::camelize($name));
}

/**
* Creates the proper controller plural name for the specified controller class name
* Creates a fixture name
*
* @param string $name Controller class name
* @return string Controller plural name
* @param string $name Model class name
* @return string Singular model key
*/
protected function _controllerName($name) {
return Inflector::pluralize(Inflector::camelize($name));
protected function _fixtureName($name) {
return Inflector::underscore(Inflector::singularize($name));
}

/**
Expand All @@ -53,6 +53,16 @@ protected function _modelName($name) {
return Inflector::pluralize(Inflector::camelize($name));
}

/**
* Creates the proper entity name (singular) for the specified name
*
* @param string $name Name
* @return string Camelized and plural model name
*/
protected function _entityName($name) {
return Inflector::singularize(Inflector::camelize($name));
}

/**
* Creates the proper underscored model key for associations
*
Expand Down

0 comments on commit 559373a

Please sign in to comment.