Skip to content
Browse files

Merge commit 'code/1.3' into 1.3-jshelper

Conflicts:
	cake/tests/cases/libs/view/helpers/html.test.php
	cake/tests/lib/header.php
  • Loading branch information...
2 parents 9a66c2c + 986af28 commit 4ebf5a4e32dc13a6adf868a48556da5b66162f49 @markstory markstory committed
Showing with 6,152 additions and 1,685 deletions.
  1. +1 −1 app/webroot/test.php
  2. +13 −13 cake/basics.php
  3. +2 −2 cake/console/cake.php
  4. +2 −2 cake/console/libs/api.php
  5. +28 −18 cake/console/libs/bake.php
  6. +2 −3 cake/console/libs/console.php
  7. +1 −1 cake/console/libs/schema.php
  8. +25 −38 cake/console/libs/shell.php
  9. +234 −354 cake/console/libs/tasks/controller.php
  10. +42 −35 cake/console/libs/tasks/db_config.php
  11. +2 −6 cake/console/libs/tasks/extract.php
  12. +409 −0 cake/console/libs/tasks/fixture.php
  13. +587 −644 cake/console/libs/tasks/model.php
  14. +77 −36 cake/console/libs/tasks/plugin.php
  15. +46 −18 cake/console/libs/tasks/project.php
  16. +183 −0 cake/console/libs/tasks/template.php
  17. +305 −115 cake/console/libs/tasks/test.php
  18. +180 −108 cake/console/libs/tasks/view.php
  19. +142 −0 cake/console/libs/templates/default/actions/controller_actions.ctp
  20. +57 −0 cake/console/libs/templates/default/classes/controller.ctp
  21. +42 −0 cake/console/libs/templates/default/classes/fixture.ctp
  22. +130 −0 cake/console/libs/templates/default/classes/model.ctp
  23. +57 −0 cake/console/libs/templates/default/classes/test.ctp
  24. 0 cake/console/libs/templates/{ → default}/views/form.ctp
  25. 0 cake/console/libs/templates/{ → default}/views/home.ctp
  26. 0 cake/console/libs/templates/{ → default}/views/index.ctp
  27. 0 cake/console/libs/templates/{ → default}/views/view.ctp
  28. +1 −1 cake/console/libs/templates/skel/webroot/test.php
  29. +1 −1 cake/console/libs/testsuite.php
  30. +2 −2 cake/dispatcher.php
  31. +1 −1 cake/libs/cache.php
  32. +1 −1 cake/libs/cake_session.php
  33. +12 −2 cake/libs/cake_socket.php
  34. +29 −54 cake/libs/configure.php
  35. +11 −7 cake/libs/controller/components/email.php
  36. +1 −1 cake/libs/controller/components/request_handler.php
  37. +1 −1 cake/libs/debugger.php
  38. +10 −3 cake/libs/http_socket.php
  39. +6 −4 cake/libs/i18n.php
  40. +4 −2 cake/libs/model/datasources/dbo/dbo_mysql.php
  41. +3 −3 cake/libs/model/datasources/dbo/dbo_oracle.php
  42. +12 −9 cake/libs/model/datasources/dbo_source.php
  43. +34 −16 cake/libs/model/model.php
  44. +3 −6 cake/libs/router.php
  45. +3 −4 cake/libs/view/helpers/ajax.php
  46. +5 −4 cake/libs/view/helpers/html.php
  47. +29 −46 cake/libs/view/helpers/javascript.php
  48. +20 −11 cake/libs/view/helpers/paginator.php
  49. +5 −6 cake/libs/view/helpers/session.php
  50. +4 −21 cake/libs/view/helpers/text.php
  51. +1 −1 cake/libs/view/helpers/time.php
  52. +1 −1 cake/libs/view/layouts/xml/default.ctp
  53. +5 −1 cake/libs/view/view.php
  54. +29 −12 cake/tests/cases/basics.test.php
  55. +5 −7 cake/tests/cases/console/libs/api.test.php
  56. +117 −0 cake/tests/cases/console/libs/bake.test.php
  57. +459 −0 cake/tests/cases/console/libs/tasks/controller.test.php
  58. +149 −0 cake/tests/cases/console/libs/tasks/db_config.test.php
  59. +259 −0 cake/tests/cases/console/libs/tasks/fixture.test.php
  60. +727 −0 cake/tests/cases/console/libs/tasks/model.test.php
  61. +252 −0 cake/tests/cases/console/libs/tasks/plugin.test.php
  62. +172 −0 cake/tests/cases/console/libs/tasks/project.test.php
  63. +167 −0 cake/tests/cases/console/libs/tasks/template.test.php
  64. +369 −18 cake/tests/cases/console/libs/tasks/test.test.php
  65. +426 −0 cake/tests/cases/console/libs/tasks/view.test.php
  66. +12 −0 cake/tests/cases/libs/cake_socket.test.php
  67. +12 −10 cake/tests/cases/libs/cake_test_case.test.php
  68. +17 −0 cake/tests/cases/libs/configure.test.php
  69. +66 −2 cake/tests/cases/libs/controller/components/email.test.php
  70. +40 −15 cake/tests/cases/libs/controller/components/request_handler.test.php
  71. +7 −9 cake/tests/cases/libs/controller/scaffold.test.php
  72. +82 −4 cake/tests/cases/libs/i18n.test.php
  73. +13 −5 cake/tests/cases/libs/model/datasources/dbo_source.test.php
Sorry, we could not display the entire diff because it was too big.
View
2 app/webroot/test.php
@@ -86,7 +86,7 @@
trigger_error("CakePHP core could not be found. Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php. It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR);
}
-$corePath = Configure::corePaths('cake');
+$corePath = App::core('cake');
if (isset($corePath[0])) {
define('TEST_CAKE_CORE_INCLUDE_PATH', rtrim($corePath[0], DS) . DS);
} else {
View
26 cake/basics.php
@@ -362,8 +362,8 @@ function am() {
*/
function env($key) {
if ($key == 'HTTPS') {
- if (isset($_SERVER) && !empty($_SERVER)) {
- return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on');
+ if (isset($_SERVER['HTTPS'])) {
+ return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
}
return (strpos(env('SCRIPT_URI'), 'https://') === 0);
}
@@ -624,9 +624,9 @@ function __n($singular, $plural, $count, $return = false) {
}
if ($return === false) {
- echo I18n::translate($singular, $plural, null, 5, $count);
+ echo I18n::translate($singular, $plural, null, 6, $count);
} else {
- return I18n::translate($singular, $plural, null, 5, $count);
+ return I18n::translate($singular, $plural, null, 6, $count);
}
}
/**
@@ -672,9 +672,9 @@ function __dn($domain, $singular, $plural, $count, $return = false) {
}
if ($return === false) {
- echo I18n::translate($singular, $plural, $domain, 5, $count);
+ echo I18n::translate($singular, $plural, $domain, 6, $count);
} else {
- return I18n::translate($singular, $plural, $domain, 5, $count);
+ return I18n::translate($singular, $plural, $domain, 6, $count);
}
}
/**
@@ -723,13 +723,13 @@ function __dc($domain, $msg, $category, $return = false) {
* Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
*
* Note that the category must be specified with a numeric value, instead of the constant name. The values are:
- * LC_CTYPE 0
- * LC_NUMERIC 1
- * LC_TIME 2
- * LC_COLLATE 3
- * LC_MONETARY 4
- * LC_MESSAGES 5
- * LC_ALL 6
+ * LC_ALL 0
+ * LC_COLLATE 1
+ * LC_CTYPE 2
+ * LC_MONETARY 3
+ * LC_NUMERIC 4
+ * LC_TIME 5
+ * LC_MESSAGES 6
*
* @param string $domain Domain
* @param string $singular Singular string to translate
View
4 cake/console/cake.php
@@ -259,8 +259,8 @@ function __bootstrap() {
Configure::getInstance(file_exists(CONFIGS . 'bootstrap.php'));
if (!file_exists(APP_PATH . 'config' . DS . 'core.php')) {
- include_once CORE_PATH . 'cake' . DS . 'console' . DS . 'templates' . DS . 'skel' . DS . 'config' . DS . 'core.php';
- Configure::buildPaths(array());
+ include_once CORE_PATH . 'cake' . DS . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel' . DS . 'config' . DS . 'core.php';
+ App::build();
}
Configure::write('debug', 1);
View
4 cake/console/libs/api.php
@@ -66,7 +66,7 @@ function main() {
return $this->help();
}
- $type = low($this->args[0]);
+ $type = strtolower($this->args[0]);
if (isset($this->paths[$type])) {
$path = $this->paths[$type];
@@ -202,7 +202,7 @@ function __parseClass($path) {
if (strpos($method, '__') === false && $method[0] != '_') {
$parsed[$method] = array(
- 'comment' => r(array('/*', '*/', '*'), '', trim($result[1][$key])),
+ 'comment' => str_replace(array('/*', '*/', '*'), '', trim($result[1][$key])),
'method' => $method,
'parameters' => trim($result[3][$key])
);
View
46 cake/console/libs/bake.php
@@ -1,5 +1,4 @@
<?php
-/* SVN FILE: $Id$ */
/**
* Command-line code generation utility to automate programmer chores.
*
@@ -21,9 +20,6 @@
* @package cake
* @subpackage cake.cake.console.libs
* @since CakePHP(tm) v 1.2.0.5012
- * @version $Revision$
- * @modifiedby $LastChangedBy$
- * @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
@@ -40,7 +36,7 @@ class BakeShell extends Shell {
* @var array
* @access public
*/
- var $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Test');
+ var $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test');
/**
* Override loadTasks() to handle paths
*
@@ -50,8 +46,16 @@ function loadTasks() {
parent::loadTasks();
$task = Inflector::classify($this->command);
if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
- $path = Inflector::underscore(Inflector::pluralize($this->command));
- $this->{$task}->path = $this->params['working'] . DS . $path . DS;
+ if (empty($this->{$task}->path)) {
+ $path = Inflector::underscore(Inflector::pluralize($this->command));
+ $this->{$task}->path = $this->params['working'] . DS . $path . DS;
+ }
+ if (isset($this->params['connection'])) {
+ $this->{$task}->connection = $this->params['connection'];
+ }
+ if (isset($this->params['plugin'])) {
+ $this->{$task}->plugin = $this->params['plugin'];
+ }
if (!is_dir($this->{$task}->path)) {
$this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path));
$this->_stop();
@@ -82,6 +86,7 @@ function main() {
$this->out('[V]iew');
$this->out('[C]ontroller');
$this->out('[P]roject');
+ $this->out('[F]ixture');
$this->out('[Q]uit');
$classToBake = strtoupper($this->in(__('What would you like to Bake?', true), array('D', 'M', 'V', 'C', 'P', 'Q')));
@@ -101,6 +106,9 @@ function main() {
case 'P':
$this->Project->execute();
break;
+ case 'F':
+ $this->Fixture->execute();
+ break;
case 'Q':
exit(0);
break;
@@ -116,22 +124,26 @@ function main() {
* @access public
*/
function all() {
- $ds = 'default';
$this->hr();
$this->out('Bake All');
$this->hr();
- if (isset($this->params['connection'])) {
- $ds = $this->params['connection'];
+ if (!isset($this->params['connection']) && empty($this->connection)) {
+ $this->connection = $this->DbConfig->getConfig();
}
if (empty($this->args)) {
- $name = $this->Model->getName($ds);
+ $this->Model->interactive = true;
+ $name = $this->Model->getName($this->connection);
+ }
+
+ foreach (array('Model', 'Controller', 'View') as $task) {
+ $this->{$task}->connection = $this->connection;
+ $this->{$task}->interactive = false;
}
if (!empty($this->args[0])) {
$name = $this->args[0];
- $this->Model->listAll($ds, false);
}
$modelExists = false;
@@ -141,7 +153,7 @@ function all() {
$modelExists = true;
} else {
App::import('Model');
- $object = new Model(array('name' => $name, 'ds' => $ds));
+ $object = new Model(array('name' => $name, 'ds' => $this->connection));
}
$modelBaked = $this->Model->bake($object, false);
@@ -149,6 +161,7 @@ function all() {
if ($modelBaked && $modelExists === false) {
$this->out(sprintf(__('%s Model was baked.', true), $model));
if ($this->_checkUnitTest()) {
+ $this->Model->bakeFixture($model);
$this->Model->bakeTest($model);
}
$modelExists = true;
@@ -165,16 +178,13 @@ function all() {
if (App::import('Controller', $controller)) {
$this->View->args = array($controller);
$this->View->execute();
+ $this->out(sprintf(__('%s Views were baked.', true), $name));
}
- $this->out(__('Bake All complete'));
+ $this->out(__('Bake All complete', true));
array_shift($this->args);
} else {
$this->err(__('Bake All could not continue without a valid model', true));
}
-
- if (empty($this->args)) {
- $this->all();
- }
$this->_stop();
}
View
5 cake/console/libs/console.php
@@ -269,8 +269,7 @@ function main($command = null) {
if ($this->_isValidModel($modelToSave)) {
// Extract the array of data we are trying to build
list($foo, $data) = explode("->save", $command);
- $badChars = array("(", ")");
- $data = str_replace($badChars, "", $data);
+ $data = preg_replace('/^\(*(array)?\(*(.+?)\)*$/i', '\\2', $data);
$saveCommand = "\$this->{$modelToSave}->save(array('{$modelToSave}' => array({$data})));";
@eval($saveCommand);
$this->out('Saved record for ' . $modelToSave);
@@ -358,4 +357,4 @@ function _loadRoutes() {
return true;
}
}
-?>
+?>
View
2 cake/console/libs/schema.php
@@ -199,7 +199,7 @@ function dump() {
}
}
$db =& ConnectionManager::getDataSource($this->Schema->connection);
- $contents = "#". $Schema->name ." sql generated on: " . date('Y-m-d H:m:s') . " : ". time()."\n\n";
+ $contents = "#". $Schema->name ." sql generated on: " . date('Y-m-d H:i:s') . " : ". time()."\n\n";
$contents .= $db->dropSchema($Schema) . "\n\n". $db->createSchema($Schema);
if ($write) {
if (strpos($write, '.sql') === false) {
View
63 cake/console/libs/shell.php
@@ -149,10 +149,10 @@ function __construct(&$dispatch) {
ClassRegistry::map($this->name, $this->alias);
if (!PHP5 && isset($this->args[0])) {
- if (strpos($this->name, low(Inflector::camelize($this->args[0]))) !== false) {
+ if (strpos($this->name, strtolower(Inflector::camelize($this->args[0]))) !== false) {
$dispatch->shiftArgs();
}
- if (low($this->command) == low(Inflector::variable($this->args[0])) && method_exists($this, $this->command)) {
+ if (strtolower($this->command) == strtolower(Inflector::variable($this->args[0])) && method_exists($this, $this->command)) {
$dispatch->shiftArgs();
}
}
@@ -329,7 +329,7 @@ function in($prompt, $options = null, $default = null) {
}
}
if (is_array($options)) {
- while ($in == '' || ($in && (!in_array(low($in), $options) && !in_array(up($in), $options)) && !in_array($in, $options))) {
+ while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
$in = $this->Dispatch->getInput($prompt, $options, $default);
}
}
@@ -427,10 +427,10 @@ function createFile ($path, $contents) {
$this->out("\n" . sprintf(__("Creating file %s", true), $path));
if (is_file($path) && $this->interactive === true) {
$key = $this->in(__("File exists, overwrite?", true). " {$path}", array('y', 'n', 'q'), 'n');
- if (low($key) == 'q') {
+ if (strtolower($key) == 'q') {
$this->out(__("Quitting.", true) ."\n");
exit;
- } elseif (low($key) != 'y') {
+ } elseif (strtolower($key) != 'y') {
$this->out(__("Skip", true) ." {$path}\n");
return false;
}
@@ -472,7 +472,7 @@ function _checkUnitTest() {
return true;
}
$unitTest = $this->in('SimpleTest is not installed. Do you want to bake unit test files anyway?', array('y','n'), 'y');
- $result = low($unitTest) == 'y' || low($unitTest) == 'yes';
+ $result = strtolower($unitTest) == 'y' || strtolower($unitTest) == 'yes';
if ($result) {
$this->out("\nYou can download SimpleTest from http://simpletest.org", true);
@@ -488,37 +488,8 @@ function _checkUnitTest() {
*/
function shortPath($file) {
$shortPath = str_replace(ROOT, null, $file);
- $shortPath = str_replace('..'.DS, '', $shortPath);
- return r(DS.DS, DS, $shortPath);
- }
-/**
- * Checks for Configure::read('Routing.admin') and forces user to input it if not enabled
- *
- * @return string Admin route to use
- * @access public
- */
- function getAdmin() {
- $admin = '';
- $cakeAdmin = null;
- $adminRoute = Configure::read('Routing.admin');
- if (!empty($adminRoute)) {
- $cakeAdmin = $adminRoute . '_';
- } else {
- $this->out('You need to enable Configure::write(\'Routing.admin\',\'admin\') in /app/config/core.php to use admin routing.');
- $this->out('What would you like the admin route to be?');
- $this->out('Example: www.example.com/admin/controller');
- while ($admin == '') {
- $admin = $this->in("What would you like the admin route to be?", null, 'admin');
- }
- if ($this->Project->cakeAdmin($admin) !== true) {
- $this->out('Unable to write to /app/config/core.php.');
- $this->out('You need to enable Configure::write(\'Routing.admin\',\'admin\') in /app/config/core.php to use admin routing.');
- $this->_stop();
- } else {
- $cakeAdmin = $admin . '_';
- }
- }
- return $cakeAdmin;
+ $shortPath = str_replace('..' . DS, '', $shortPath);
+ return str_replace(DS . DS, DS, $shortPath);
}
/**
* Creates the proper controller path for the specified controller class name
@@ -528,7 +499,7 @@ function getAdmin() {
* @access protected
*/
function _controllerPath($name) {
- return low(Inflector::underscore($name));
+ return strtolower(Inflector::underscore($name));
}
/**
* Creates the proper controller plural name for the specified controller class name
@@ -611,5 +582,21 @@ function _singularHumanName($name) {
function _pluralHumanName($name) {
return Inflector::humanize(Inflector::underscore(Inflector::pluralize($name)));
}
+/**
+ * Find the correct path for a plugin. Scans $pluginPaths for the plugin you want.
+ *
+ * @param string $pluginName Name of the plugin you want ie. DebugKit
+ * @return string $path path to the correct plugin.
+ **/
+ function _pluginPath($pluginName) {
+ $pluginPaths = App::path('plugins');
+ $pluginDirName = Inflector::underscore($pluginName);
+ foreach ($pluginPaths as $path) {
+ if (is_dir($path . $pluginDirName)) {
+ return $path . $pluginDirName . DS ;
+ }
+ }
+ return $pluginPaths[0] . $pluginDirName . DS;
+ }
}
?>
View
588 cake/console/libs/tasks/controller.php
@@ -1,5 +1,4 @@
<?php
-/* SVN FILE: $Id$ */
/**
* The ControllerTask handles creating and updating controller files.
*
@@ -8,20 +7,17 @@
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
- * Copyright 2005-2008, Cake Software Foundation, Inc.
+ * Copyright 2005-2009, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
- * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.console.libs.tasks
* @since CakePHP(tm) v 1.2
- * @version $Revision$
- * @modifiedby $LastChangedBy$
- * @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
@@ -44,7 +40,7 @@ class ControllerTask extends Shell {
* @var array
* @access public
*/
- var $tasks = array('Project');
+ var $tasks = array('Model', 'Test', 'Template', 'DbConfig', 'Project');
/**
* path to CONTROLLERS directory
*
@@ -70,16 +66,22 @@ function execute() {
}
if (isset($this->args[0])) {
+ if (!isset($this->connection)) {
+ $this->connection = 'default';
+ }
+ if (strtolower($this->args[0]) == 'all') {
+ return $this->all();
+ }
$controller = Inflector::camelize($this->args[0]);
$actions = null;
if (isset($this->args[1]) && $this->args[1] == 'scaffold') {
- $this->out('Baking scaffold for ' . $controller);
+ $this->out(__('Baking scaffold for ', true) . $controller);
$actions = $this->bakeActions($controller);
} else {
$actions = 'scaffold';
}
if ((isset($this->args[1]) && $this->args[1] == 'admin') || (isset($this->args[2]) && $this->args[2] == 'admin')) {
- if ($admin = $this->getAdmin()) {
+ if ($admin = $this->Project->getAdmin()) {
$this->out('Adding ' . Configure::read('Routing.admin') .' methods');
if ($actions == 'scaffold') {
$actions = $this->bakeActions($controller, $admin);
@@ -96,144 +98,164 @@ function execute() {
}
}
/**
+ * Bake All the controllers at once. Will only bake controllers for models that exist.
+ *
+ * @access public
+ * @return void
+ **/
+ function all() {
+ $this->interactive = false;
+ $this->listAll($this->connection, false);
+ ClassRegistry::config('Model', array('ds' => $this->connection));
+ $unitTestExists = $this->_checkUnitTest();
+ foreach ($this->__tables as $table) {
+ $model = $this->_modelName($table);
+ $controller = $this->_controllerName($model);
+ if (App::import('Model', $model)) {
+ $actions = $this->bakeActions($controller);
+ if ($this->bake($controller, $actions) && $unitTestExists) {
+ $this->bakeTest($controller);
+ }
+ }
+ }
+ }
+/**
* Interactive
*
* @access private
*/
- function __interactive($controllerName = false) {
- if (!$controllerName) {
- $this->interactive = true;
- $this->hr();
- $this->out(sprintf("Bake Controller\nPath: %s", $this->path));
- $this->hr();
- $actions = '';
- $uses = array();
- $helpers = array();
- $components = array();
- $wannaUseSession = 'y';
- $wannaDoAdmin = 'n';
- $wannaUseScaffold = 'n';
- $wannaDoScaffolding = 'y';
- $controllerName = $this->getName();
+ function __interactive() {
+ $this->interactive = true;
+ $this->hr();
+ $this->out(sprintf(__("Bake Controller\nPath: %s", true), $this->path));
+ $this->hr();
+
+ if (empty($this->connection)) {
+ $this->connection = $this->DbConfig->getConfig();
}
+
+ $controllerName = $this->getName();
$this->hr();
- $this->out("Baking {$controllerName}Controller");
+ $this->out(sprintf(__('Baking %sController', true), $controllerName));
$this->hr();
- $controllerFile = low(Inflector::underscore($controllerName));
+ $helpers = $components = array();
+ $actions = '';
+ $wannaUseSession = 'y';
+ $wannaBakeAdminCrud = 'n';
+ $useDynamicScaffold = 'n';
+ $wannaBakeCrud = 'y';
+
+ $controllerFile = strtolower(Inflector::underscore($controllerName));
$question[] = __("Would you like to build your controller interactively?", true);
if (file_exists($this->path . $controllerFile .'_controller.php')) {
$question[] = sprintf(__("Warning: Choosing no will overwrite the %sController.", true), $controllerName);
}
- $doItInteractive = $this->in(join("\n", $question), array('y','n'), 'y');
+ $doItInteractive = $this->in(join("\n", $question), array('y', 'n'), 'y');
- if (low($doItInteractive) == 'y' || low($doItInteractive) == 'yes') {
+ if (strtolower($doItInteractive) == 'y') {
$this->interactive = true;
+ $useDynamicScaffold = $this->in(
+ __("Would you like to use dynamic scaffolding?", true), array('y','n'), 'n'
+ );
- $wannaUseScaffold = $this->in(__("Would you like to use scaffolding?", true), array('y','n'), 'n');
-
- if (low($wannaUseScaffold) == 'n' || low($wannaUseScaffold) == 'no') {
-
- $wannaDoScaffolding = $this->in(__("Would you like to include some basic class methods (index(), add(), view(), edit())?", true), array('y','n'), 'n');
-
- if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') {
- $wannaDoAdmin = $this->in(__("Would you like to create the methods for admin routing?", true), array('y','n'), 'n');
- }
-
- $wannaDoHelpers = $this->in(__("Would you like this controller to use other helpers besides HtmlHelper and FormHelper?", true), array('y','n'), 'n');
+ if (strtolower($useDynamicScaffold) == 'y') {
+ $wannaBakeCrud = 'n';
+ $actions = 'scaffold';
+ } else {
+ list($wannaBakeCrud, $wannaBakeAdminCrud) = $this->_askAboutMethods();
- if (low($wannaDoHelpers) == 'y' || low($wannaDoHelpers) == 'yes') {
- $helpersList = $this->in(__("Please provide a comma separated list of the other helper names you'd like to use.\nExample: 'Ajax, Javascript, Time'", true));
- $helpersListTrimmed = str_replace(' ', '', $helpersList);
- $helpers = explode(',', $helpersListTrimmed);
- }
- $wannaDoComponents = $this->in(__("Would you like this controller to use any components?", true), array('y','n'), 'n');
+ $helpers = $this->doHelpers();
+ $components = $this->doComponents();
- if (low($wannaDoComponents) == 'y' || low($wannaDoComponents) == 'yes') {
- $componentsList = $this->in(__("Please provide a comma separated list of the component names you'd like to use.\nExample: 'Acl, Security, RequestHandler'", true));
- $componentsListTrimmed = str_replace(' ', '', $componentsList);
- $components = explode(',', $componentsListTrimmed);
- }
-
- $wannaUseSession = $this->in(__("Would you like to use Sessions?", true), array('y','n'), 'y');
- } else {
- $wannaDoScaffolding = 'n';
+ $wannaUseSession = $this->in(
+ __("Would you like to use Session flash messages?", true), array('y','n'), 'y'
+ );
}
} else {
- $wannaDoScaffolding = $this->in(__("Would you like to include some basic class methods (index(), add(), view(), edit())?", true), array('y','n'), 'y');
-
- if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') {
- $wannaDoAdmin = $this->in(__("Would you like to create the methods for admin routing?", true), array('y','n'), 'y');
- }
+ list($wannaBakeCrud, $wannaBakeCrud) = $this->_askAboutMethods();
}
- $admin = false;
- if ((low($wannaDoAdmin) == 'y' || low($wannaDoAdmin) == 'yes')) {
- $admin = $this->getAdmin();
+ if (strtolower($wannaBakeCrud) == 'y') {
+ $actions = $this->bakeActions($controllerName, null, strtolower($wannaUseSession) == 'y');
}
-
- if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') {
- $actions = $this->bakeActions($controllerName, null, in_array(low($wannaUseSession), array('y', 'yes')));
- if ($admin) {
- $actions .= $this->bakeActions($controllerName, $admin, in_array(low($wannaUseSession), array('y', 'yes')));
- }
+ if (strtolower($wannaBakeAdminCrud) == 'y') {
+ $admin = $this->Project->getAdmin();
+ $actions .= $this->bakeActions($controllerName, $admin, strtolower($wannaUseSession) == 'y');
}
if ($this->interactive === true) {
- $this->out('');
- $this->hr();
- $this->out('The following controller will be created:');
- $this->hr();
- $this->out("Controller Name: $controllerName");
-
- if (low($wannaUseScaffold) == 'y' || low($wannaUseScaffold) == 'yes') {
- $this->out(" var \$scaffold;");
- $actions = 'scaffold';
- }
-
- if (count($helpers)) {
- $this->out("Helpers: ", false);
-
- foreach ($helpers as $help) {
- if ($help != $helpers[count($helpers) - 1]) {
- $this->out(ucfirst($help) . ", ", false);
- } else {
- $this->out(ucfirst($help));
- }
- }
- }
-
- if (count($components)) {
- $this->out("Components: ", false);
-
- foreach ($components as $comp) {
- if ($comp != $components[count($components) - 1]) {
- $this->out(ucfirst($comp) . ", ", false);
- } else {
- $this->out(ucfirst($comp));
- }
- }
- }
- $this->hr();
+ $this->confirmController($controllerName, $useDynamicScaffold, $helpers, $components);
$looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y');
- if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
- $baked = $this->bake($controllerName, $actions, $helpers, $components, $uses);
+ if (strtolower($looksGood) == 'y') {
+ $baked = $this->bake($controllerName, $actions, $helpers, $components);
if ($baked && $this->_checkUnitTest()) {
$this->bakeTest($controllerName);
}
- } else {
- $this->__interactive($controllerName);
}
} else {
- $baked = $this->bake($controllerName, $actions, $helpers, $components, $uses);
+ $baked = $this->bake($controllerName, $actions, $helpers, $components);
if ($baked && $this->_checkUnitTest()) {
$this->bakeTest($controllerName);
}
}
}
/**
+ * Confirm a to be baked controller with the user
+ *
+ * @return void
+ **/
+ function confirmController($controllerName, $useDynamicScaffold, $helpers, $components) {
+ $this->out('');
+ $this->hr();
+ $this->out(__('The following controller will be created:', true));
+ $this->hr();
+ $this->out(sprintf(__("Controller Name:\n\t%s", true), $controllerName));
+
+ if (strtolower($useDynamicScaffold) == 'y') {
+ $this->out("var \$scaffold;");
+ }
+
+ $properties = array(
+ 'helpers' => __("Helpers:", true),
+ 'components' => __('Components:', true),
+ );
+
+ foreach ($properties as $var => $title) {
+ if (count($$var)) {
+ $output = '';
+ $length = count($$var);
+ foreach ($$var as $i => $propElement) {
+ if ($i != $length -1) {
+ $output .= ucfirst($propElement) . ', ';
+ } else {
+ $output .= ucfirst($propElement);
+ }
+ }
+ $this->out($title . "\n\t" . $output);
+ }
+ }
+ $this->hr();
+ }
+/**
+ * Interact with the user and ask about which methods (admin or regular they want to bake)
+ *
+ * @return array Array containing (bakeRegular, bakeAdmin) answers
+ **/
+ function _askAboutMethods() {
+ $wannaBakeCrud = $this->in(
+ __("Would you like to create some basic class methods \n(index(), add(), view(), edit())?", true),
+ array('y','n'), 'n'
+ );
+ $wannaBakeAdminCrud = $this->in(
+ __("Would you like to create the basic class methods for admin routing?", true),
+ array('y','n'), 'n'
+ );
+ return array($wannaBakeCrud, $wannaBakeAdminCrud);
+ }
+/**
* Bake scaffold actions
*
* @param string $controllerName Controller name
@@ -247,151 +269,23 @@ function bakeActions($controllerName, $admin = null, $wannaUseSession = true) {
if ($this->plugin) {
$modelImport = $this->plugin . '.' . $modelImport;
}
- if (!App::import('Model', $modelImport)) {
- $this->err(__('You must have a model for this class to build scaffold methods. Please try again.', true));
- exit;
+ if (!App::import('Model', $currentModelName)) {
+ $this->err(__('You must have a model for this class to build basic methods. Please try again.', true));
+ $this->_stop();
}
- $actions = null;
- $modelObj =& new $currentModelName();
+
+ $modelObj =& ClassRegistry::init($currentModelName);
$controllerPath = $this->_controllerPath($controllerName);
$pluralName = $this->_pluralName($currentModelName);
$singularName = Inflector::variable($currentModelName);
$singularHumanName = Inflector::humanize($currentModelName);
$pluralHumanName = Inflector::humanize($controllerName);
- $actions .= "\n";
- $actions .= "\tfunction {$admin}index() {\n";
- $actions .= "\t\t\$this->{$currentModelName}->recursive = 0;\n";
- $actions .= "\t\t\$this->set('{$pluralName}', \$this->paginate());\n";
- $actions .= "\t}\n";
- $actions .= "\n";
- $actions .= "\tfunction {$admin}view(\$id = null) {\n";
- $actions .= "\t\tif (!\$id) {\n";
- if ($wannaUseSession) {
- $actions .= "\t\t\t\$this->Session->setFlash(__('Invalid {$singularHumanName}.', true));\n";
- $actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
- } else {
- $actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n";
- }
- $actions .= "\t\t}\n";
- $actions .= "\t\t\$this->set('".$singularName."', \$this->{$currentModelName}->read(null, \$id));\n";
- $actions .= "\t}\n";
- $actions .= "\n";
-
- /* ADD ACTION */
- $compact = array();
- $actions .= "\tfunction {$admin}add() {\n";
- $actions .= "\t\tif (!empty(\$this->data)) {\n";
- $actions .= "\t\t\t\$this->{$currentModelName}->create();\n";
- $actions .= "\t\t\tif (\$this->{$currentModelName}->save(\$this->data)) {\n";
- if ($wannaUseSession) {
- $actions .= "\t\t\t\t\$this->Session->setFlash(__('The ".$singularHumanName." has been saved', true));\n";
- $actions .= "\t\t\t\t\$this->redirect(array('action'=>'index'));\n";
- } else {
- $actions .= "\t\t\t\t\$this->flash(__('{$currentModelName} saved.', true), array('action'=>'index'));\n";
- }
- $actions .= "\t\t\t} else {\n";
- if ($wannaUseSession) {
- $actions .= "\t\t\t\t\$this->Session->setFlash(__('The {$singularHumanName} could not be saved. Please, try again.', true));\n";
- }
- $actions .= "\t\t\t}\n";
- $actions .= "\t\t}\n";
- foreach ($modelObj->hasAndBelongsToMany as $associationName => $relation) {
- if (!empty($associationName)) {
- $habtmModelName = $this->_modelName($associationName);
- $habtmSingularName = $this->_singularName($associationName);
- $habtmPluralName = $this->_pluralName($associationName);
- $actions .= "\t\t\${$habtmPluralName} = \$this->{$currentModelName}->{$habtmModelName}->find('list');\n";
- $compact[] = "'{$habtmPluralName}'";
- }
- }
- foreach ($modelObj->belongsTo as $associationName => $relation) {
- if (!empty($associationName)) {
- $belongsToModelName = $this->_modelName($associationName);
- $belongsToPluralName = $this->_pluralName($associationName);
- $actions .= "\t\t\${$belongsToPluralName} = \$this->{$currentModelName}->{$belongsToModelName}->find('list');\n";
- $compact[] = "'{$belongsToPluralName}'";
- }
- }
- if (!empty($compact)) {
- $actions .= "\t\t\$this->set(compact(".join(', ', $compact)."));\n";
- }
- $actions .= "\t}\n";
- $actions .= "\n";
-
- /* EDIT ACTION */
- $compact = array();
- $actions .= "\tfunction {$admin}edit(\$id = null) {\n";
- $actions .= "\t\tif (!\$id && empty(\$this->data)) {\n";
- if ($wannaUseSession) {
- $actions .= "\t\t\t\$this->Session->setFlash(__('Invalid {$singularHumanName}', true));\n";
- $actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
- } else {
- $actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n";
- }
- $actions .= "\t\t}\n";
- $actions .= "\t\tif (!empty(\$this->data)) {\n";
- $actions .= "\t\t\tif (\$this->{$currentModelName}->save(\$this->data)) {\n";
- if ($wannaUseSession) {
- $actions .= "\t\t\t\t\$this->Session->setFlash(__('The ".$singularHumanName." has been saved', true));\n";
- $actions .= "\t\t\t\t\$this->redirect(array('action'=>'index'));\n";
- } else {
- $actions .= "\t\t\t\t\$this->flash(__('The ".$singularHumanName." has been saved.', true), array('action'=>'index'));\n";
- }
- $actions .= "\t\t\t} else {\n";
- if ($wannaUseSession) {
- $actions .= "\t\t\t\t\$this->Session->setFlash(__('The {$singularHumanName} could not be saved. Please, try again.', true));\n";
- }
- $actions .= "\t\t\t}\n";
- $actions .= "\t\t}\n";
- $actions .= "\t\tif (empty(\$this->data)) {\n";
- $actions .= "\t\t\t\$this->data = \$this->{$currentModelName}->read(null, \$id);\n";
- $actions .= "\t\t}\n";
- foreach ($modelObj->hasAndBelongsToMany as $associationName => $relation) {
- if (!empty($associationName)) {
- $habtmModelName = $this->_modelName($associationName);
- $habtmSingularName = $this->_singularName($associationName);
- $habtmPluralName = $this->_pluralName($associationName);
- $actions .= "\t\t\${$habtmPluralName} = \$this->{$currentModelName}->{$habtmModelName}->find('list');\n";
- $compact[] = "'{$habtmPluralName}'";
- }
- }
- foreach ($modelObj->belongsTo as $associationName => $relation) {
- if (!empty($associationName)) {
- $belongsToModelName = $this->_modelName($associationName);
- $belongsToPluralName = $this->_pluralName($associationName);
- $actions .= "\t\t\${$belongsToPluralName} = \$this->{$currentModelName}->{$belongsToModelName}->find('list');\n";
- $compact[] = "'{$belongsToPluralName}'";
- }
- }
- if (!empty($compact)) {
- $actions .= "\t\t\$this->set(compact(".join(',', $compact)."));\n";
- }
- $actions .= "\t}\n";
- $actions .= "\n";
- $actions .= "\tfunction {$admin}delete(\$id = null) {\n";
- $actions .= "\t\tif (!\$id) {\n";
- if ($wannaUseSession) {
- $actions .= "\t\t\t\$this->Session->setFlash(__('Invalid id for {$singularHumanName}', true));\n";
- $actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
- } else {
- $actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n";
- }
- $actions .= "\t\t}\n";
- $actions .= "\t\tif (\$this->{$currentModelName}->delete(\$id)) {\n";
- if ($wannaUseSession) {
- $actions .= "\t\t\t\$this->Session->setFlash(__('{$singularHumanName} deleted', true));\n";
- $actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
- } else {
- $actions .= "\t\t\t\$this->flash(__('{$singularHumanName} deleted', true), array('action'=>'index'));\n";
- }
- $actions .= "\t\t}\n";
- $actions .= "\t}\n";
- $actions .= "\n";
+ $this->Template->set(compact('admin', 'controllerPath', 'pluralName', 'singularName', 'singularHumanName',
+ 'pluralHumanName', 'modelObj', 'wannaUseSession', 'currentModelName'));
+ $actions = $this->Template->generate('actions', 'controller_actions');
return $actions;
}
-
-
/**
* Assembles and writes a Controller file
*
@@ -403,53 +297,22 @@ function bakeActions($controllerName, $admin = null, $wannaUseSession = true) {
* @return string Baked controller
* @access private
*/
- function bake($controllerName, $actions = '', $helpers = null, $components = null, $uses = null) {
- $out = "<?php\n";
- $out .= "class $controllerName" . "Controller extends {$this->plugin}AppController {\n\n";
- $out .= "\tvar \$name = '$controllerName';\n";
-
- if (low($actions) == 'scaffold') {
- $out .= "\tvar \$scaffold;\n";
- } else {
- if (count($uses)) {
- $out .= "\tvar \$uses = array('" . $this->_modelName($controllerName) . "', ";
-
- foreach ($uses as $use) {
- if ($use != $uses[count($uses) - 1]) {
- $out .= "'" . $this->_modelName($use) . "', ";
- } else {
- $out .= "'" . $this->_modelName($use) . "'";
- }
- }
- $out .= ");\n";
- }
+ function bake($controllerName, $actions = '', $helpers = null, $components = null) {
+ $isScaffold = ($actions === 'scaffold') ? true : false;
- $out .= "\tvar \$helpers = array('Html', 'Form'";
- if (count($helpers)) {
- foreach ($helpers as $help) {
- $out .= ", '" . Inflector::camelize($help) . "'";
- }
- }
- $out .= ");\n";
+ $this->Template->set('plugin', Inflector::camelize($this->plugin));
+ $this->Template->set(compact('controllerName', 'actions', 'helpers', 'components', 'isScaffold'));
+ $contents = $this->Template->generate('classes', 'controller');
- if (count($components)) {
- $out .= "\tvar \$components = array(";
-
- foreach ($components as $comp) {
- if ($comp != $components[count($components) - 1]) {
- $out .= "'" . Inflector::camelize($comp) . "', ";
- } else {
- $out .= "'" . Inflector::camelize($comp) . "'";
- }
- }
- $out .= ");\n";
- }
- $out .= $actions;
+ $path = $this->path;
+ if (isset($this->plugin)) {
+ $path = $this->_pluginPath($this->plugin) . 'controllers' . DS;
}
- $out .= "}\n";
- $out .= "?>";
- $filename = $this->path . $this->_controllerPath($controllerName) . '_controller.php';
- return $this->createFile($filename, $out);
+ $filename = $path . $this->_controllerPath($controllerName) . '_controller.php';
+ if ($this->createFile($filename, $contents)) {
+ return $contents;
+ }
+ return false;
}
/**
* Assembles and writes a unit test file
@@ -459,84 +322,89 @@ function bake($controllerName, $actions = '', $helpers = null, $components = nul
* @access private
*/
function bakeTest($className) {
- $import = $className;
- if ($this->plugin) {
- $import = $this->plugin . '.' . $className;
- }
- $out = "App::import('Controller', '$import');\n\n";
- $out .= "class Test{$className} extends {$className}Controller {\n";
- $out .= "\tvar \$autoRender = false;\n}\n\n";
- $out .= "class {$className}ControllerTest extends CakeTestCase {\n";
- $out .= "\tvar \${$className} = null;\n\n";
- $out .= "\tfunction startTest() {\n\t\t\$this->{$className} = new Test{$className}();";
- $out .= "\n\t\t\$this->{$className}->constructClasses();\n\t}\n\n";
- $out .= "\tfunction test{$className}ControllerInstance() {\n";
- $out .= "\t\t\$this->assertTrue(is_a(\$this->{$className}, '{$className}Controller'));\n\t}\n\n";
- $out .= "\tfunction endTest() {\n\t\tunset(\$this->{$className});\n\t}\n}\n";
-
- $path = CONTROLLER_TESTS;
- if (isset($this->plugin)) {
- $pluginPath = 'plugins' . DS . Inflector::underscore($this->plugin) . DS;
- $path = APP . $pluginPath . 'tests' . DS . 'cases' . DS . 'controllers' . DS;
- }
-
- $filename = Inflector::underscore($className).'_controller.test.php';
- $this->out("\nBaking unit test for $className...");
+ $this->Test->plugin = $this->plugin;
+ $this->Test->connection = $this->connection;
+ return $this->Test->bake('Controller', $className);
+ }
+/**
+ * Interact with the user and get a list of additional helpers
+ *
+ * @return array Helpers that the user wants to use.
+ **/
+ function doHelpers() {
+ return $this->_doPropertyChoices(
+ __("Would you like this controller to use other helpers\nbesides HtmlHelper and FormHelper?", true),
+ __("Please provide a comma separated list of the other\nhelper names you'd like to use.\nExample: 'Ajax, Javascript, Time'", true)
+ );
+ }
- $header = '$Id';
- $content = "<?php \n/* SVN FILE: $header$ */\n/* ". $className ."Controller Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
- return $this->createFile($path . $filename, $content);
+/**
+ * Interact with the user and get a list of additional components
+ *
+ * @return array Components the user wants to use.
+ **/
+ function doComponents() {
+ return $this->_doPropertyChoices(
+ __("Would you like this controller to use any components?", true),
+ __("Please provide a comma separated list of the component names you'd like to use.\nExample: 'Acl, Security, RequestHandler'", true)
+ );
}
/**
- * Outputs and gets the list of possible models or controllers from database
+ * Common code for property choice handling.
+ *
+ * @param string $prompt A yes/no question to precede the list
+ * @param sting $example A question for a comma separated list, with examples.
+ * @return array Array of values for property.
+ **/
+ function _doPropertyChoices($prompt, $example) {
+ $proceed = $this->in($prompt, array('y','n'), 'n');
+ $property = array();
+ if (strtolower($proceed) == 'y') {
+ $propertyList = $this->in($example);
+ $propertyListTrimmed = str_replace(' ', '', $propertyList);
+ $property = explode(',', $propertyListTrimmed);
+ }
+ return array_filter($property);
+ }
+/**
+ * Outputs and gets the list of possible controllers from database
*
* @param string $useDbConfig Database configuration name
+ * @param boolean $interactive Whether you are using listAll interactively and want options output.
* @return array Set of controllers
* @access public
*/
- function listAll($useDbConfig = 'default') {
- $db =& ConnectionManager::getDataSource($useDbConfig);
- $usePrefix = empty($db->config['prefix']) ? '' : $db->config['prefix'];
- if ($usePrefix) {
- $tables = array();
- foreach ($db->listSources() as $table) {
- if (!strncmp($table, $usePrefix, strlen($usePrefix))) {
- $tables[] = substr($table, strlen($usePrefix));
- }
+ function listAll($useDbConfig = null) {
+ if (is_null($useDbConfig)) {
+ $useDbConfig = $this->connection;
+ }
+ $this->__tables = $this->Model->getAllTables($useDbConfig);
+
+ if ($this->interactive == true) {
+ $this->out(__('Possible Controllers based on your current database:', true));
+ $this->_controllerNames = array();
+ $count = count($this->__tables);
+ for ($i = 0; $i < $count; $i++) {
+ $this->_controllerNames[] = $this->_controllerName($this->_modelName($this->__tables[$i]));
+ $this->out($i + 1 . ". " . $this->_controllerNames[$i]);
}
- } else {
- $tables = $db->listSources();
- }
-
- if (empty($tables)) {
- $this->err(__('Your database does not have any tables.', true));
- $this->_stop();
+ return $this->_controllerNames;
}
-
- $this->__tables = $tables;
- $this->out('Possible Controllers based on your current database:');
- $this->_controllerNames = array();
- $count = count($tables);
- for ($i = 0; $i < $count; $i++) {
- $this->_controllerNames[] = $this->_controllerName($this->_modelName($tables[$i]));
- $this->out($i + 1 . ". " . $this->_controllerNames[$i]);
- }
- return $this->_controllerNames;
+ return $this->__tables;
}
-
/**
* Forces the user to specify the controller he wants to bake, and returns the selected controller name.
*
+ * @param string $useDbConfig Connection name to get a controller name for.
* @return string Controller name
* @access public
*/
- function getName() {
- $useDbConfig = 'default';
- $controllers = $this->listAll($useDbConfig, 'Controllers');
+ function getName($useDbConfig = null) {
+ $controllers = $this->listAll($useDbConfig);
$enteredController = '';
while ($enteredController == '') {
- $enteredController = $this->in(__("Enter a number from the list above, type in the name of another controller, or 'q' to exit", true), null, 'q');
+ $enteredController = $this->in(__("Enter a number from the list above,\ntype in the name of another controller, or 'q' to exit", true), null, 'q');
if ($enteredController === 'q') {
$this->out(__("Exit", true));
@@ -544,8 +412,7 @@ function getName() {
}
if ($enteredController == '' || intval($enteredController) > count($controllers)) {
- $this->out(__('Error:', true));
- $this->out(__("The Controller name you supplied was empty, or the number \nyou selected was not an option. Please try again.", true));
+ $this->err(__("The Controller name you supplied was empty,\nor the number you selected was not an option. Please try again.", true));
$enteredController = '';
}
}
@@ -555,7 +422,6 @@ function getName() {
} else {
$controllerName = Inflector::camelize($enteredController);
}
-
return $controllerName;
}
/**
@@ -568,12 +434,26 @@ function help() {
$this->out("Usage: cake bake controller <arg1> <arg2>...");
$this->hr();
$this->out('Commands:');
- $this->out("\n\tcontroller <name>\n\t\tbakes controller with var \$scaffold");
- $this->out("\n\tcontroller <name> scaffold\n\t\tbakes controller with scaffold actions.\n\t\t(index, view, add, edit, delete)");
- $this->out("\n\tcontroller <name> scaffold admin\n\t\tbakes a controller with scaffold actions for both public and Configure::read('Routing.admin')");
- $this->out("\n\tcontroller <name> admin\n\t\tbakes a controller with scaffold actions only for Configure::read('Routing.admin')");
+ $this->out('');
+ $this->out("controller <name>");
+ $this->out("\tbakes controller with var \$scaffold");
+ $this->out('');
+ $this->out("controller <name> scaffold");
+ $this->out("\tbakes controller with scaffold actions.");
+ $this->out("\t(index, view, add, edit, delete)");
+ $this->out('');
+ $this->out("controller <name> scaffold admin");
+ $this->out("\tbakes a controller with scaffold actions for both public");
+ $this->out("\tand Configure::read('Routing.admin')");
+ $this->out('');
+ $this->out("controller <name> admin");
+ $this->out("\tbakes a controller with scaffold actions only for");
+ $this->out("\tConfigure::read('Routing.admin')");
+ $this->out('');
+ $this->out("controller all");
+ $this->out("\tbakes all controllers with CRUD methods.");
$this->out("");
$this->_stop();
}
}
-?>
+?>
View
77 cake/console/libs/tasks/db_config.php
@@ -1,5 +1,4 @@
<?php
-/* SVN FILE: $Id$ */
/**
* The DbConfig Task handles creating and updating the database.php
*
@@ -8,25 +7,19 @@
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
- * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
- * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.console.libs.tasks
* @since CakePHP(tm) v 1.2
- * @version $Revision$
- * @modifiedby $LastChangedBy$
- * @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
-if (!class_exists('File')) {
- uses('file');
-}
/**
* Task class for creating and updating the database configuration file.
*
@@ -53,6 +46,13 @@ class DbConfigTask extends Shell {
'schema'=> null, 'prefix'=> null, 'encoding' => null, 'port' => null
);
/**
+ * String name of the database config class name.
+ * Used for testing.
+ *
+ * @var string
+ **/
+ var $databaseClassName = 'DATABASE_CONFIG';
+/**
* initialization callback
*
* @var string
@@ -92,35 +92,27 @@ function __interactive() {
if (preg_match('/[^a-z0-9_]/i', $name)) {
$name = '';
$this->out('The name may only contain unaccented latin characters, numbers or underscores');
- }
- else if (preg_match('/^[^a-z_]/i', $name)) {
+ } else if (preg_match('/^[^a-z_]/i', $name)) {
$name = '';
$this->out('The name must start with an unaccented latin character or an underscore');
}
}
- $driver = '';
-
- while ($driver == '') {
- $driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'mysqli', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql');
- }
- $persistent = '';
- while ($persistent == '') {
- $persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n');
- }
+ $driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'mysqli', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql');
+ $persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n');
if (low($persistent) == 'n') {
$persistent = 'false';
} else {
$persistent = 'true';
}
- $host = '';
+ $host = '';
while ($host == '') {
$host = $this->in('Database Host:', null, 'localhost');
}
- $port = '';
+ $port = '';
while ($port == '') {
$port = $this->in('Port?', null, 'n');
}
@@ -128,8 +120,8 @@ function __interactive() {
if (low($port) == 'n') {
$port = null;
}
- $login = '';
+ $login = '';
while ($login == '') {
$login = $this->in('User:', null, 'root');
}
@@ -141,43 +133,39 @@ function __interactive() {
if ($password == '') {
$blank = $this->in('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
- if ($blank == 'y')
- {
+ if ($blank == 'y') {
$blankPassword = true;
}
}
}
- $database = '';
+ $database = '';
while ($database == '') {
$database = $this->in('Database Name:', null, 'cake');
}
- $prefix = '';
+ $prefix = '';
while ($prefix == '') {
$prefix = $this->in('Table Prefix?', null, 'n');
}
-
if (low($prefix) == 'n') {
$prefix = null;
}
- $encoding = '';
+ $encoding = '';
while ($encoding == '') {
$encoding = $this->in('Table encoding?', null, 'n');
}
-
if (low($encoding) == 'n') {
$encoding = null;
}
- $schema = '';
+ $schema = '';
if ($driver == 'postgres') {
while ($schema == '') {
$schema = $this->in('Table schema?', null, 'n');
}
}
-
if (low($schema) == 'n') {
$schema = null;
}
@@ -240,7 +228,7 @@ function __verify($config) {
$this->hr();
$looksGood = $this->in('Look okay?', array('y', 'n'), 'y');
- if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
+ if (strtolower($looksGood) == 'y') {
return $config;
}
return false;
@@ -262,7 +250,7 @@ function bake($configs) {
$oldConfigs = array();
if (file_exists($filename)) {
- $db = new DATABASE_CONFIG;
+ $db = new $this->databaseClassName;
$temp = get_class_vars(get_class($db));
foreach ($temp as $configName => $info) {
@@ -346,8 +334,27 @@ function bake($configs) {
$out .= "}\n";
$out .= "?>";
- $filename = $this->path.'database.php';
+ $filename = $this->path . 'database.php';
return $this->createFile($filename, $out);
}
+/**
+ * Get a user specified Connection name
+ *
+ * @return void
+ **/
+ function getConfig() {
+ $useDbConfig = 'default';
+ $configs = get_class_vars($this->databaseClassName);
+
+ if (!is_array($configs)) {
+ return $this->execute();
+ }
+
+ $connections = array_keys($configs);
+ if (count($connections) > 1) {
+ $useDbConfig = $this->in(__('Use Database Config', true) .':', $connections, 'default');
+ }
+ return $useDbConfig;
+ }
}
?>
View
8 cake/console/libs/tasks/extract.php
@@ -1,5 +1,4 @@
<?php
-/* SVN FILE: $Id$ */
/**
* Short description for file.
*
@@ -8,20 +7,17 @@
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
- * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
- * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.console.libs
* @since CakePHP(tm) v 1.2.0.5012
- * @version $Revision$
- * @modifiedby $LastChangedBy$
- * @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
View
409 cake/console/libs/tasks/fixture.php
@@ -0,0 +1,409 @@
+<?php
+/**
+ * The FixtureTask handles creating and updating fixture files.
+ *
+ * PHP versions 4 and 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
+ * Copyright 2005-2009, Cake Software Foundation, Inc.
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @filesource
+ * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
+ * @package cake
+ * @subpackage cake.cake.console.libs.tasks
+ * @since CakePHP(tm) v 1.3
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+/**
+ * Task class for creating and updating fixtures files.
+ *
+ * @package cake
+ * @subpackage cake.cake.console.libs.tasks
+ */
+class FixtureTask extends Shell {
+/**
+ * Name of plugin
+ *
+ * @var string
+ * @access public
+ */
+ var $plugin = null;
+/**
+ * Tasks to be loaded by this Task
+ *
+ * @var array
+ * @access public
+ */
+ var $tasks = array('DbConfig', 'Model', 'Template');
+/**
+ * path to fixtures directory
+ *
+ * @var string
+ * @access public
+ */
+ var $path = null;
+/**
+ * The db connection being used for baking
+ *
+ * @var string
+ **/
+ var $connection = null;
+/**
+ * Schema instance
+ *
+ * @var object
+ **/
+ var $_Schema = null;
+/**
+ * Override initialize
+ *
+ * @access public
+ */
+ function __construct(&$dispatch) {
+ parent::__construct($dispatch);
+ $this->path = $this->params['working'] . DS . 'tests' . DS . 'fixtures' . DS;
+ if (!class_exists('CakeSchema')) {
+ App::import('Model', 'CakeSchema');
+ }
+ }
+/**
+ * Execution method always used for tasks
+ * Handles dispatching to interactive, named, or all processess.
+ *
+ * @access public
+ */
+ function execute() {
+ if (empty($this->args)) {
+ $this->__interactive();
+ }
+
+ if (isset($this->args[0])) {
+ if (!isset($this->connection)) {
+ $this->connection = 'default';
+ }
+ if (strtolower($this->args[0]) == 'all') {
+ return $this->all();
+ }
+ $model = Inflector::camelize($this->args[0]);
+ $this->bake($model);
+ }
+ }
+/**
+ * Bake All the Fixtures at once. Will only bake fixtures for models that exist.
+ *
+ * @access public
+ * @return void
+ **/
+ function all() {
+ $this->interactive = false;
+ $tables = $this->Model->listAll($this->connection, false);
+ foreach ($tables as $table) {
+ $model = $this->_modelName($table);
+ $this->bake($model);
+ }
+ }
+/**
+ * Interactive baking function
+ *
+ * @access private
+ */
+ function __interactive() {
+ $this->interactive = true;
+ $this->hr();
+ $this->out(sprintf("Bake Fixture\nPath: %s", $this->path));
+ $this->hr();
+
+ $useDbConfig = $this->connection;
+ if (!isset($this->connection)) {
+ $this->connection = $this->DbConfig->getConfig();
+ }
+ $modelName = $this->Model->getName($this->connection);
+ $useTable = $this->Model->getTable($modelName, $this->connection);
+ $importOptions = $this->importOptions($modelName);
+ $this->bake($modelName, $useTable, $importOptions);
+ }
+/**
+ * Interacts with the User to setup an array of import options. For a fixture.
+ *
+ * @param string $modelName Name of model you are dealing with.
+ * @return array Array of import options.
+ **/
+ function importOptions($modelName) {
+ $options = array();
+ $doSchema = $this->in(__('Would you like to import schema for this fixture?', true), array('y', 'n'), 'n');
+ if ($doSchema == 'y') {
+ $options['schema'] = $modelName;
+ }
+ $doRecords = $this->in(__('Would you like to use record importing for this fixture?', true), array('y', 'n'), 'n');
+ if ($doRecords == 'y') {
+ $options['records'] = true;
+ }
+ if ($doRecords == 'n') {
+ $prompt = sprintf(__("Would you like to build this fixture with data from %s's table?", true), $modelName);
+ $fromTable = $this->in($prompt, array('y', 'n'), 'n');
+ if (strtolower($fromTable) == 'y') {
+ $options['fromTable'] = true;
+ }
+ }
+ return $options;
+ }
+/**
+ * Assembles and writes a Fixture file
+ *
+ * @param string $model Name of model to bake.
+ * @param string $useTable Name of table to use.
+ * @param array $importOptions Options for var $import
+ * @return string Baked fixture
+ * @access private
+ */
+ function bake($model, $useTable = false, $importOptions = array()) {
+ $table = $schema = $records = $import = $modelImport = $recordImport = null;
+ if (!$useTable) {
+ $useTable = Inflector::tableize($model);
+ } elseif ($useTable != Inflector::tableize($model)) {
+ $table = $useTable;
+ }
+
+ if (!empty($importOptions)) {
+ if (isset($importOptions['schema'])) {
+ $modelImport = "'model' => '{$importOptions['schema']}'";
+ }
+ if (isset($importOptions['records'])) {
+ $recordImport = "'records' => true";
+ }
+ if ($modelImport && $recordImport) {
+ $modelImport .= ', ';
+ }
+ if (!empty($modelImport) || !empty($recordImport)) {
+ $import = sprintf("array(%s%s)", $modelImport, $recordImport);
+ }
+ }
+
+ $this->_Schema = new CakeSchema();
+ $data = $this->_Schema->read(array('models' => false, 'connection' => $this->connection));
+
+ if (!isset($data['tables'][$useTable])) {
+ $this->err('Could not find your selected table ' . $useTable);
+ return false;
+ }
+
+ $tableInfo = $data['tables'][$useTable];
+ if (is_null($modelImport)) {
+ $schema = $this->_generateSchema($tableInfo);
+ }
+
+ if (!isset($importOptions['records']) && !isset($importOptions['fromTable'])) {
+ $recordCount = 1;
+ if (isset($this->params['count'])) {
+ $recordCount = $this->params['count'];
+ }
+ $records = $this->_makeRecordString($this->_generateRecords($tableInfo, $recordCount));
+ }
+ if (isset($importOptions['fromTable'])) {
+ $records = $this->_makeRecordString($this->_getRecordsFromTable($model, $useTable));
+ }
+ $out = $this->generateFixtureFile($model, compact('records', 'table', 'schema', 'import', 'fields'));
+ return $out;
+ }
+/**
+ * Generate the fixture file, and write to disk
+ *
+ * @param string $model name of the model being generated
+ * @param string $fixture Contents of the fixture file.
+ * @access public
+ * @return void
+ **/
+ function generateFixtureFile($model, $otherVars) {
+ $defaults = array('table' => null, 'schema' => null, 'records' => null, 'import' => null, 'fields' => null);
+ $vars = array_merge($defaults, $otherVars);
+
+ $path = $this->path;
+ if (isset($this->plugin)) {
+ $path = $this->_pluginPath($this->plugin) . 'tests' . DS . 'fixtures' . DS;
+ }
+ $filename = Inflector::underscore($model) . '_fixture.php';
+
+ $this->Template->set('model', $model);
+ $this->Template->set($vars);
+ $content = $this->Template->generate('classes', 'fixture');
+
+ $this->out("\nBaking test fixture for $model...");
+ $this->createFile($path . $filename, $content);
+ return $content;
+ }
+/**
+ * Generates a string representation of a schema.
+ *
+ * @param array $table Table schema array
+ * @return string fields definitions
+ **/
+ function _generateSchema($tableInfo) {
+ $cols = array();
+ $out = "array(\n";
+ foreach ($tableInfo as $field => $fieldInfo) {
+ if (is_array($fieldInfo)) {
+ if ($field != 'indexes') {
+ $col = "\t\t'{$field}' => array('type'=>'" . $fieldInfo['type'] . "', ";
+ $col .= join(', ', $this->_Schema->__values($fieldInfo));
+ } else {
+ $col = "\t\t'indexes' => array(";
+ $props = array();
+ foreach ((array)$fieldInfo as $key => $index) {
+ $props[] = "'{$key}' => array(".join(', ', $this->_Schema->__values($index)).")";
+ }
+ $col .= join(', ', $props);
+ }
+ $col .= ")";
+ $cols[] = $col;
+ }
+ }
+ $out .= join(",\n", $cols);
+ $out .= "\n\t)";
+ return $out;
+ }
+/**
+ * Generate String representation of Records
+ *
+ * @param array $table Table schema array
+ * @return array Array of records to use in the fixture.
+ **/
+ function _generateRecords($tableInfo, $recordCount = 1) {
+ $records = array();
+ for ($i = 0; $i < $recordCount; $i++) {
+ $record = array();
+ foreach ($tableInfo as $field => $fieldInfo) {
+ if (empty($fieldInfo['type'])) {
+ continue;
+ }
+ switch ($fieldInfo['type']) {
+ case 'integer':
+ $insert = $i + 1;
+ break;
+ case 'string';
+ $isPrimaryUuid = (
+ isset($fieldInfo['key']) && strtolower($fieldInfo['key']) == 'primary' &&
+ isset($fieldInfo['length']) && $fieldInfo['length'] == 36
+ );
+ if ($isPrimaryUuid) {
+ $insert = String::uuid();
+ } else {
+ $insert = "Lorem ipsum dolor sit amet";
+ if (!empty($fieldInfo['length'])) {
+ $insert = substr($insert, 0, (int)$fieldInfo['length'] - 2);
+ }
+ }
+ $insert = "'$insert'";
+ break;
+ case 'timestamp':
+ $ts = time();
+ $insert = "'$ts'";
+ break;
+ case 'datetime':
+ $ts = date('Y-m-d H:i:s');
+ $insert = "'$ts'";
+ break;
+ case 'date':
+ $ts = date('Y-m-d');
+ $insert = "'$ts'";
+ break;
+ case 'time':
+ $ts = date('H:i:s');
+ $insert = "'$ts'";
+ break;
+ case 'boolean':
+ $insert = 1;
+ break;
+ case 'text':