Skip to content

Commit

Permalink
Merge pull request #3240 from cakephp/3.0-helper-bake
Browse files Browse the repository at this point in the history
3.0 Add helper & component bake
  • Loading branch information
lorenzo committed Apr 5, 2014
2 parents e9cd0be + f470a44 commit a17c310
Show file tree
Hide file tree
Showing 22 changed files with 490 additions and 241 deletions.
19 changes: 17 additions & 2 deletions src/Console/Command/BakeShell.php
Expand Up @@ -40,7 +40,13 @@ class BakeShell extends Shell {
*
* @var array
*/
public $tasks = ['Behavior', 'Project', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test'];
public $tasks = [
'Plugin', 'Project',
'Model', 'Behavior',
'Controller', 'Component',
'View', 'Helper',
'Fixture', 'Test'
];

/**
* The connection being used.
Expand Down Expand Up @@ -85,10 +91,13 @@ public function main() {
$this->out(__d('cake_console', 'The following commands you can generate skeleton code your your application.'));
$this->out(__d('cake_console', 'Available bake commands:'));
$this->out('');
$this->out(__d('cake_console', 'behavior'));
$this->out(__d('cake_console', 'model'));
$this->out(__d('cake_console', 'behavior'));
$this->out(__d('cake_console', 'view'));
$this->out(__d('cake_console', 'controller'));
$this->out(__d('cake_console', 'component'));
$this->out(__d('cake_console', 'project'));
$this->out(__d('cake_console', 'plugin'));
$this->out(__d('cake_console', 'fixture'));
$this->out(__d('cake_console', 'project'));
$this->out(__d('cake_console', 'test'));
Expand Down Expand Up @@ -170,9 +179,15 @@ public function getOptionParser() {
])->addSubcommand('controller', [
'help' => __d('cake_console', 'Bake a controller.'),
'parser' => $this->Controller->getOptionParser()
])->addSubcommand('component', [
'help' => __d('cake_console', 'Bake a component.'),
'parser' => $this->Controller->getOptionParser()
])->addSubcommand('fixture', [
'help' => __d('cake_console', 'Bake a fixture.'),
'parser' => $this->Fixture->getOptionParser()
])->addSubcommand('helper', [
'help' => __d('cake_console', 'Bake a helper.'),
'parser' => $this->Helper->getOptionParser()
])->addSubcommand('test', [
'help' => __d('cake_console', 'Bake a unit test.'),
'parser' => $this->Test->getOptionParser()
Expand Down
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
*
* @return void
* {@inheritDoc}
*/
public function execute() {
parent::execute();
$name = Inflector::classify($this->args[0]);
$this->bake($name);
$this->bakeTest($name);
public function name() {
return 'behavior';
}

/**
* Generate a class stub
*
* @param string $className The classname to generate.
* @return void
* {@inheritDoc}
*/
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 function fileName($name) {
return $name . 'Behavior.php';
}

/**
* Generate a test case.
*
* @return void
* {@inheritDoc}
*/
public function bakeTest($className) {
if (!empty($this->params['no-test'])) {
return;
}
$this->Test->plugin = $this->plugin;
return $this->Test->bake('Behavior', $className);
public function template() {
return 'behavior';
}

/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
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.')
]);

return $parser;
}
}
52 changes: 52 additions & 0 deletions src/Console/Command/Task/ComponentTask.php
@@ -0,0 +1,52 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Console\Command\Task;

use Cake\Console\Command\Task\SimpleBakeTask;

/**
* Component code generator.
*/
class ComponentTask extends SimpleBakeTask {

/**
* Task name used in path generation.
*
* @var string
*/
public $pathFragment = 'Controller/Component/';

/**
* {@inheritDoc}
*/
public function name() {
return 'component';
}

/**
* {@inheritDoc}
*/
public function fileName($name) {
return $name . 'Component.php';
}

/**
* {@inheritDoc}
*/
public function template() {
return 'component';
}

}
8 changes: 3 additions & 5 deletions src/Console/Command/Task/ControllerTask.php
Expand Up @@ -34,13 +34,11 @@ class ControllerTask extends BakeTask {
public $tasks = ['Model', 'Test', 'Template'];

/**
* Override initialize
* Path fragment for generated code.
*
* @return void
* @var string
*/
public function initialize() {
$this->path = current(App::path('Controller'));
}
public $pathFragment = 'Controller/';

/**
* Execution method always used for tasks
Expand Down
36 changes: 9 additions & 27 deletions src/Console/Command/Task/FixtureTask.php
Expand Up @@ -37,22 +37,17 @@ class FixtureTask extends BakeTask {
public $tasks = ['Model', 'Template'];

/**
* path to fixtures directory
* Get the file path.
*
* @var string
* @return string
*/
public $path = null;

/**
* Override initialize
*
* @param \Cake\Console\ConsoleOutput $stdout A ConsoleOutput object for stdout.
* @param \Cake\Console\ConsoleOutput $stderr A ConsoleOutput object for stderr.
* @param \Cake\Console\ConsoleInput $stdin A ConsoleInput object for stdin.
*/
public function __construct(ConsoleOutput $stdout = null, ConsoleOutput $stderr = null, ConsoleInput $stdin = null) {
parent::__construct($stdout, $stderr, $stdin);
$this->path = ROOT . DS . 'Test' . DS . 'Fixture' . DS;
public function getPath() {
$dir = 'Test/Fixture/';
$path = ROOT . DS . $dir;
if (isset($this->plugin)) {
$path = $this->_pluginPath($this->plugin) . $dir;
}
return $path;
}

/**
Expand Down Expand Up @@ -265,19 +260,6 @@ public function generateFixtureFile($model, $otherVars) {
return $content;
}

/**
* Get the path to the fixtures.
*
* @return string Path for the fixtures
*/
public function getPath() {
$path = $this->path;
if (isset($this->plugin)) {
$path = $this->_pluginPath($this->plugin) . 'Test/Fixture/';
}
return $path;
}

/**
* Generates a string representation of a schema.
*
Expand Down
52 changes: 52 additions & 0 deletions src/Console/Command/Task/HelperTask.php
@@ -0,0 +1,52 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Console\Command\Task;

use Cake\Console\Command\Task\SimpleBakeTask;

/**
* Helper code generator.
*/
class HelperTask extends SimpleBakeTask {

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

/**
* {@inheritDoc}
*/
public function name() {
return 'helper';
}

/**
* {@inheritDoc}
*/
public function fileName($name) {
return $name . 'Helper.php';
}

/**
* {@inheritDoc}
*/
public function template() {
return 'helper';
}

}
11 changes: 1 addition & 10 deletions src/Console/Command/Task/ModelTask.php
Expand Up @@ -33,7 +33,7 @@ class ModelTask extends BakeTask {
*
* @var string
*/
public $path = null;
public $pathFragment = 'Model/';

/**
* tasks
Expand Down Expand Up @@ -70,15 +70,6 @@ class ModelTask extends BakeTask {
*/
protected $_validations = [];

/**
* Override initialize
*
* @return void
*/
public function initialize() {
$this->path = APP . 'Model' . DS;
}

/**
* Execution method always used for tasks
*
Expand Down

0 comments on commit a17c310

Please sign in to comment.