Skip to content

Commit

Permalink
Merge pull request #368 from cakephp/options-trait
Browse files Browse the repository at this point in the history
Move common options setting into a trait.
  • Loading branch information
markstory authored Oct 13, 2017
2 parents 5dd01be + b1942f6 commit 49e689c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 46 deletions.
19 changes: 3 additions & 16 deletions src/Shell/BakeShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*/
class BakeShell extends Shell
{
use CommonOptionsTrait;
use ConventionsTrait;

/**
Expand Down Expand Up @@ -292,29 +293,15 @@ public function getOptionParser()
'Usage: "bake all --everything"',
'default' => false,
'boolean' => true,
])->addOption('connection', [
'help' => 'Database connection to use in conjunction with `bake all`.',
'short' => 'c',
'default' => 'default'
])->addOption('force', [
'short' => 'f',
'boolean' => true,
'help' => 'Force overwriting existing files without prompting.'
])->addOption('plugin', [
'short' => 'p',
'help' => 'Plugin to bake into.'
])->addOption('prefix', [
'help' => 'Prefix to bake controllers and templates into.'
])->addOption('tablePrefix', [
'help' => 'Table prefix to be used in models.',
'default' => null
])->addOption('theme', [
'short' => 't',
'help' => 'The theme to use when baking code.',
'default' => Configure::read('Bake.theme'),
'choices' => $bakeThemes
]);

$parser = $this->_setCommonOptions($parser);

foreach ($this->_taskMap as $task => $config) {
$taskParser = $this->{$task}->getOptionParser();
$parser->addSubcommand(Inflector::underscore($task), [
Expand Down
59 changes: 59 additions & 0 deletions src/Shell/CommonOptionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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 1.4.3
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Bake\Shell;

use Cake\Core\Configure;
use Cake\Core\Plugin;

trait CommonOptionsTrait
{

/**
* Set common options used by all bake tasks.
*
* @param \Cake\Console\ConsoleOptionParser $parser Options parser.
* @return \Cake\Console\ConsoleOptionParser
*/
protected function _setCommonOptions($parser)
{
$bakeThemes = [];
foreach (Plugin::loaded() as $plugin) {
$path = Plugin::classPath($plugin);
if (is_dir($path . 'Template' . DS . 'Bake')) {
$bakeThemes[] = $plugin;
}
}

$parser->addOption('plugin', [
'short' => 'p',
'help' => 'Plugin to bake into.'
])->addOption('force', [
'short' => 'f',
'boolean' => true,
'help' => 'Force overwriting existing files without prompting.'
])->addOption('connection', [
'short' => 'c',
'default' => 'default',
'help' => 'The datasource connection to get data from.'
])->addOption('theme', [
'short' => 't',
'help' => 'The theme to use when baking code.',
'default' => Configure::read('Bake.theme'),
'choices' => $bakeThemes
]);

return $parser;
}
}
33 changes: 3 additions & 30 deletions src/Shell/Task/BakeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
*/
namespace Bake\Shell\Task;

use Bake\Shell\CommonOptionsTrait;
use Cake\Cache\Cache;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Core\ConventionsTrait;
use Cake\Core\Plugin;
use Cake\Filesystem\File;

/**
Expand All @@ -27,6 +27,7 @@
*/
class BakeTask extends Shell
{
use CommonOptionsTrait;
use ConventionsTrait;

/**
Expand Down Expand Up @@ -226,34 +227,6 @@ protected function _deleteEmptyFile($path)
*/
public function getOptionParser()
{
$parser = parent::getOptionParser();

$bakeThemes = [];
foreach (Plugin::loaded() as $plugin) {
$path = Plugin::classPath($plugin);
if (is_dir($path . 'Template' . DS . 'Bake')) {
$bakeThemes[] = $plugin;
}
}

$parser->addOption('plugin', [
'short' => 'p',
'help' => 'Plugin to bake into.'
])->addOption('force', [
'short' => 'f',
'boolean' => true,
'help' => 'Force overwriting existing files without prompting.'
])->addOption('connection', [
'short' => 'c',
'default' => 'default',
'help' => 'The datasource connection to get data from.'
])->addOption('theme', [
'short' => 't',
'help' => 'The theme to use when baking code.',
'default' => Configure::read('Bake.theme'),
'choices' => $bakeThemes
]);

return $parser;
return $this->_setCommonOptions(parent::getOptionParser());
}
}

0 comments on commit 49e689c

Please sign in to comment.