Skip to content

Commit

Permalink
Extract SimpleBakeTask
Browse files Browse the repository at this point in the history
HelperTask and BehaviorTask were so similar that their differences could
be expressed as simple set of properties.
  • Loading branch information
markstory committed Apr 5, 2014
1 parent 835b01a commit 6448639
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 179 deletions.
8 changes: 4 additions & 4 deletions src/Console/Command/Task/BakeTask.php
Expand Up @@ -28,11 +28,11 @@ class BakeTask extends Shell {
use ConventionsTrait;

/**
* The default path to bake files into.
* The pathFragment appended to the plugin/app path.
*
* @var string
*/
public $path;
public $pathFragment;

/**
* Name of plugin
Expand Down Expand Up @@ -67,9 +67,9 @@ public function startup() {
* @return string Path to output.
*/
public function getPath() {
$path = $this->path;
$path = APP . $this->pathFragment;
if (isset($this->plugin)) {
$path = $this->_pluginPath($this->plugin) . $this->name . DS;
$path = $this->_pluginPath($this->plugin) . $this->pathFragment;
}
return $path;
}
Expand Down
97 changes: 12 additions & 85 deletions src/Console/Command/Task/BehaviorTask.php
Expand Up @@ -14,112 +14,39 @@
*/
namespace Cake\Console\Command\Task;

use Cake\Console\Command\Task\BakeTask;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Utility\Inflector;
use Cake\Console\Command\Task\SimpleBakeTask;

/**
* Behavior code generator.
*/
class BehaviorTask extends BakeTask {

/**
* Tasks to be loaded by this Task
*
* @var array
*/
public $tasks = ['Test', 'Template'];
class BehaviorTask extends SimpleBakeTask {

/**
* Task name used in path generation.
*
* @var string
*/
public $name = 'Model/Behavior';

/**
* Override initialize
*
* @return void
*/
public function initialize() {
$this->path = current(App::path('Model/Behavior'));
}
public $pathFragment = 'Model/Behavior/';

/**
* Execute method
* The name of the task used in menus and output.
*
* @return void
*/
public function execute() {
parent::execute();
$name = Inflector::classify($this->args[0]);
$this->bake($name);
$this->bakeTest($name);
}

/**
* Generate a class stub
*
* @param string $className The classname to generate.
* @return void
* @var string
*/
public function bake($name) {
$namespace = Configure::read('App.namespace');
if ($this->plugin) {
$namespace = Plugin::getNamespace($this->plugin);
}
$data = compact('name', 'namespace');
$this->Template->set($data);
$contents = $this->Template->generate('classes', 'behavior');

$path = $this->getPath();
$filename = $path . $name . 'Behavior.php';
$this->createFile($filename, $contents);
return $contents;
}
public $name = 'behavior';

/**
* Generate a test case.
* The suffix appended to generated class files.
*
* @return void
* @var string
*/
public function bakeTest($className) {
if (!empty($this->params['no-test'])) {
return;
}
$this->Test->plugin = $this->plugin;
return $this->Test->bake('Behavior', $className);
}
public $suffix = 'Behavior';

/**
* Gets the option parser instance and configures it.
* Template name to use.
*
* @return \Cake\Console\ConsoleOptionParser
* @var string
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
$parser->description(
__d('cake_console', 'Bake a behavior class file.')
)->addArgument('name', [
'help' => __d('cake_console', 'Name of the Behavior to bake. Can use Plugin.name to bake controllers into plugins.')
])->addOption('plugin', [
'short' => 'p',
'help' => __d('cake_console', 'Plugin to bake the behavior into.')
])->addOption('theme', [
'short' => 't',
'help' => __d('cake_console', 'Theme to use when baking code.')
])->addOption('no-test', [
'boolean' => true,
'help' => __d('cake_console', 'Do not generate a test skeleton.')
])->addOption('force', [
'short' => 'f',
'boolean' => true,
'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
]);
public $template = 'behavior';

return $parser;
}
}
97 changes: 12 additions & 85 deletions src/Console/Command/Task/HelperTask.php
Expand Up @@ -14,112 +14,39 @@
*/
namespace Cake\Console\Command\Task;

use Cake\Console\Command\Task\BakeTask;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Utility\Inflector;
use Cake\Console\Command\Task\SimpleBakeTask;

/**
* Helper code generator.
*/
class HelperBakeTask {

/**
* Tasks to be loaded by this Task
*
* @var array
*/
public $tasks = ['Test', 'Template'];
class HelperTask extends SimpleBakeTask {

/**
* Task name used in path generation.
*
* @var string
*/
public $name = 'View/Helper';

/**
* Override initialize
*
* @return void
*/
public function initialize() {
$this->path = current(App::path('View/Helper'));
}
public $pathFragment = 'View/Helper/';

/**
* Execute method
* The name of the task used in menus and output.
*
* @return void
*/
public function execute() {
parent::execute();
$name = Inflector::classify($this->args[0]);
$this->bake($name);
$this->bakeTest($name);
}

/**
* Generate a class stub
*
* @param string $className The classname to generate.
* @return void
* @var string
*/
public function bake($name) {
$namespace = Configure::read('App.namespace');
if ($this->plugin) {
$namespace = Plugin::getNamespace($this->plugin);
}
$data = compact('name', 'namespace');
$this->Template->set($data);
$contents = $this->Template->generate('classes', 'helper');

$path = $this->getPath();
$filename = $path . $name . 'Helper.php';
$this->createFile($filename, $contents);
return $contents;
}
public $name = 'helper';

/**
* Generate a test case.
* The suffix appended to generated class files.
*
* @return void
* @var string
*/
public function bakeTest($className) {
if (!empty($this->params['no-test'])) {
return;
}
$this->Test->plugin = $this->plugin;
return $this->Test->bake('Helper', $className);
}
public $suffix = 'Helper';

/**
* Gets the option parser instance and configures it.
* Template name to use.
*
* @return \Cake\Console\ConsoleOptionParser
* @var string
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
$parser->description(
__d('cake_console', 'Bake a helper class file.')
)->addArgument('name', [
'help' => __d('cake_console', 'Name of the helper to bake. Can use Plugin.name to bake controllers into plugins.')
])->addOption('plugin', [
'short' => 'p',
'help' => __d('cake_console', 'Plugin to bake the helper into.')
])->addOption('theme', [
'short' => 't',
'help' => __d('cake_console', 'Theme to use when baking code.')
])->addOption('no-test', [
'boolean' => true,
'help' => __d('cake_console', 'Do not generate a test skeleton.')
])->addOption('force', [
'short' => 'f',
'boolean' => true,
'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
]);
public $template = 'helper';

return $parser;
}
}

0 comments on commit 6448639

Please sign in to comment.