📄 controller.php
字号:
$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\t\$this->_stop();\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\t\$this->_stop();\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}->del(\$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"; return $actions; }/** * Assembles and writes a Controller file * * @param string $controllerName Controller name * @param string $actions Actions to add, or set the whole controller to use $scaffold (set $actions to 'scaffold') * @param array $helpers Helpers to use in controller * @param array $components Components to use in controller * @param array $uses Models to use in controller * @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"; } $out .= "\tvar \$helpers = array('Html', 'Form'"; if (count($helpers)) { foreach ($helpers as $help) { $out .= ", '" . Inflector::camelize($help) . "'"; } } $out .= ");\n"; 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; } $out .= "}\n"; $out .= "?>"; $filename = $this->path . $this->_controllerPath($controllerName) . '_controller.php'; return $this->createFile($filename, $out); }/** * Assembles and writes a unit test file * * @param string $className Controller class name * @return string Baked test * @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 setUp() {\n\t\t\$this->{$className} = new Test{$className}();\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 tearDown() {\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..."); $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); }/** * Outputs and gets the list of possible models or controllers from database * * @param string $useDbConfig Database configuration name * @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)); } } } else { $tables = $db->listSources(); } if (empty($tables)) { $this->err(__('Your database does not have any tables.', true)); $this->_stop(); } $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; }/** * Forces the user to specify the controller he wants to bake, and returns the selected controller name. * * @return string Controller name * @access public */ function getName() { $useDbConfig = 'default'; $controllers = $this->listAll($useDbConfig, 'Controllers'); $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'); if ($enteredController === 'q') { $this->out(__("Exit", true)); $this->_stop(); } 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)); $enteredController = ''; } } if (intval($enteredController) > 0 && intval($enteredController) <= count($controllers) ) { $controllerName = $controllers[intval($enteredController) - 1]; } else { $controllerName = Inflector::camelize($enteredController); } return $controllerName; }/** * Displays help contents * * @access public */ function help() { $this->hr(); $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->_stop(); }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -