📄 bake.php.svn-base
字号:
} } else { $this->stdout('Bake Aborted.'); } } else { $this->bakeController($controllerName, $uses, $helpers, $components, $actions, $wannaUseScaffold); if ($this->doUnitTest()) { $this->bakeUnitTest('controller', $controllerName); } exit(); } } function __bakeActions($controllerName, $admin = null, $admin_url = null, $wannaUseSession = 'y') { $currentModelName = $this->__modelName($controllerName); $modelObj =& new $currentModelName(); $controllerPath = $this->__controllerPath($currentModelName); $pluralName = $this->__pluralName($currentModelName); $singularName = $this->__singularName($currentModelName); $singularHumanName = $this->__singularHumanName($currentModelName); $pluralHumanName = $this->__pluralHumanName($controllerName); if(!class_exists($currentModelName)) { $this->stdout('You must have a model for this class to build scaffold methods. Please try again.'); exit; } $actions .= "\n"; $actions .= "\tfunction {$admin}index() {\n"; $actions .= "\t\t\$this->{$currentModelName}->recursive = 0;\n"; $actions .= "\t\t\$this->set('{$pluralName}', \$this->{$currentModelName}->findAll());\n"; $actions .= "\t}\n"; $actions .= "\n"; $actions .= "\tfunction {$admin}view(\$id = null) {\n"; $actions .= "\t\tif(!\$id) {\n"; if (low($wannaUseSession) == 'y' || low($wannaUseSession) == 'yes') { $actions .= "\t\t\t\$this->Session->setFlash('Invalid id for {$singularHumanName}.');\n"; $actions .= "\t\t\t\$this->redirect('{$admin_url}/{$controllerPath}/index');\n"; } else { $actions .= "\t\t\t\$this->flash('Invalid id for {$singularHumanName}', '{$admin_url}/{$controllerPath}/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 */ $actions .= "\tfunction {$admin}add() {\n"; $actions .= "\t\tif(empty(\$this->data)) {\n"; foreach($modelObj->hasAndBelongsToMany as $associationName => $relation) { if(!empty($associationName)) { $otherModelName = $this->__modelName($associationName); $otherSingularName = $this->__singularName($associationName); $otherPluralName = $this->__pluralName($associationName); $selectedOtherPluralName = 'selected' . ucfirst($otherPluralName); $actions .= "\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n"; $actions .= "\t\t\t\$this->set('{$selectedOtherPluralName}', null);\n"; } } foreach($modelObj->belongsTo as $associationName => $relation) { if(!empty($associationName)) { $otherModelName = $this->__modelName($associationName); $otherSingularName = $this->__singularName($associationName); $otherPluralName = $this->__pluralName($associationName); $actions .= "\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n"; } } $actions .= "\t\t\t\$this->render();\n"; $actions .= "\t\t} else {\n"; $actions .= "\t\t\t\$this->cleanUpFields();\n"; $actions .= "\t\t\tif(\$this->{$currentModelName}->save(\$this->data)) {\n"; if (low($wannaUseSession) == 'y' || low($wannaUseSession) == 'yes') { $actions .= "\t\t\t\t\$this->Session->setFlash('The ".$this->__singularHumanName($currentModelName)." has been saved');\n"; $actions .= "\t\t\t\t\$this->redirect('{$admin_url}/{$controllerPath}/index');\n"; } else { $actions .= "\t\t\t\t\$this->flash('{$currentModelName} saved.', '{$admin_url}/{$controllerPath}/index');\n"; } $actions .= "\t\t\t} else {\n"; if (low($wannaUseSession) == 'y' || low($wannaUseSession) == 'yes') { $actions .= "\t\t\t\t\$this->Session->setFlash('Please correct errors below.');\n"; } foreach($modelObj->hasAndBelongsToMany as $associationName => $relation) { if(!empty($associationName)) { $otherModelName = $this->__modelName($associationName); $otherSingularName = $this->__singularName($associationName); $otherPluralName = $this->__pluralName($associationName); $selectedOtherPluralName = 'selected' . ucfirst($otherPluralName); $actions .= "\t\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n"; $actions .= "\t\t\t\tif(empty(\$this->data['{$associationName}']['{$associationName}'])) { \$this->data['{$associationName}']['{$associationName}'] = null; }\n"; $actions .= "\t\t\t\t\$this->set('{$selectedOtherPluralName}', \$this->data['{$associationName}']['{$associationName}']);\n"; } } foreach($modelObj->belongsTo as $associationName => $relation) { if(!empty($associationName)) { $otherModelName = $this->__modelName($associationName); $otherSingularName = $this->__singularName($associationName); $otherPluralName = $this->__pluralName($associationName); $actions .= "\t\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n"; } } $actions .= "\t\t\t}\n"; $actions .= "\t\t}\n"; $actions .= "\t}\n"; $actions .= "\n"; /* EDIT ACTION */ $actions .= "\tfunction {$admin}edit(\$id = null) {\n"; $actions .= "\t\tif(empty(\$this->data)) {\n"; $actions .= "\t\t\tif(!\$id) {\n"; if (low($wannaUseSession) == 'y' || low($wannaUseSession) == 'yes') { $actions .= "\t\t\t\t\$this->Session->setFlash('Invalid id for {$singularHumanName}');\n"; $actions .= "\t\t\t\t\$this->redirect('{$admin_url}/{$controllerPath}/index');\n"; } else { $actions .= "\t\t\t\t\$this->flash('Invalid id for {$singularHumanName}', '{$admin_url}/{$controllerPath}/index');\n"; } $actions .= "\t\t\t}\n"; $actions .= "\t\t\t\$this->data = \$this->{$currentModelName}->read(null, \$id);\n"; foreach($modelObj->hasAndBelongsToMany as $associationName => $relation) { if(!empty($associationName)) { $otherModelName = $this->__modelName($associationName); $otherSingularName = $this->__singularName($associationName); $otherPluralName = $this->__pluralName($associationName); $otherModelKey = Inflector::underscore($otherModelName); $otherModelObj =& ClassRegistry::getObject($otherModelKey); $selectedOtherPluralName = 'selected' . ucfirst($otherPluralName); $actions .= "\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n"; $actions .= "\t\t\tif(empty(\$this->data['{$associationName}'])) { \$this->data['{$associationName}'] = null; }\n"; $actions .= "\t\t\t\$this->set('{$selectedOtherPluralName}', \$this->_selectedArray(\$this->data['{$associationName}']));\n"; } } foreach($modelObj->belongsTo as $associationName => $relation) { if(!empty($associationName)) { $otherModelName = $this->__modelName($associationName); $otherSingularName = $this->__singularName($associationName); $otherPluralName = $this->__pluralName($associationName); $actions .= "\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n"; } } $actions .= "\t\t} else {\n"; $actions .= "\t\t\t\$this->cleanUpFields();\n"; $actions .= "\t\t\tif(\$this->{$currentModelName}->save(\$this->data)) {\n"; if (low($wannaUseSession) == 'y' || low($wannaUseSession) == 'yes') { $actions .= "\t\t\t\t\$this->Session->setFlash('The ".Inflector::humanize($currentModelName)." has been saved');\n"; $actions .= "\t\t\t\t\$this->redirect('{$admin_url}/{$controllerPath}/index');\n"; } else { $actions .= "\t\t\t\t\$this->flash('{$currentModelName} saved.', '{$admin_url}/{$controllerPath}/index');\n"; } $actions .= "\t\t\t} else {\n"; if (low($wannaUseSession) == 'y' || low($wannaUseSession) == 'yes') { $actions .= "\t\t\t\t\$this->Session->setFlash('Please correct errors below.');\n"; } foreach($modelObj->hasAndBelongsToMany as $associationName => $relation) { if(!empty($associationName)) { $otherModelName = $this->__modelName($associationName); $otherSingularName = $this->__singularName($associationName); $otherPluralName = $this->__pluralName($associationName); $selectedOtherPluralName = 'selected' . ucfirst($otherPluralName); $actions .= "\t\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n"; $actions .= "\t\t\t\tif(empty(\$this->data['{$associationName}']['{$associationName}'])) { \$this->data['{$associationName}']['{$associationName}'] = null; }\n"; $actions .= "\t\t\t\t\$this->set('{$selectedOtherPluralName}', \$this->data['{$associationName}']['{$associationName}']);\n"; } } foreach($modelObj->belongsTo as $associationName => $relation) { if(!empty($associationName)) { $otherModelName = $this->__modelName($associationName); $otherSingularName = $this->__singularName($associationName); $otherPluralName = $this->__pluralName($associationName); $actions .= "\t\t\t\t\$this->set('{$otherPluralName}', \$this->{$currentModelName}->{$otherModelName}->generateList());\n"; } } $actions .= "\t\t\t}\n"; $actions .= "\t\t}\n"; $actions .= "\t}\n"; $actions .= "\n"; $actions .= "\tfunction {$admin}delete(\$id = null) {\n"; $actions .= "\t\tif(!\$id) {\n"; if (low($wannaUseSession) == 'y' || low($wannaUseSession) == 'yes') { $actions .= "\t\t\t\$this->Session->setFlash('Invalid id for {$singularHumanName}');\n"; $actions .= "\t\t\t\$this->redirect('{$admin_url}/{$controllerPath}/index');\n"; } else { $actions .= "\t\t\t\$this->flash('Invalid id for {$singularHumanName}', '{$admin_url}/{$controllerPath}/index');\n"; } $actions .= "\t\t}\n"; $actions .= "\t\tif(\$this->{$currentModelName}->del(\$id)) {\n"; if (low($wannaUseSession) == 'y' || low($wannaUseSession) == 'yes') { $actions .= "\t\t\t\$this->Session->setFlash('The ".$this->__singularHumanName($currentModelName)." deleted: id '.\$id.'');\n"; $actions .= "\t\t\t\$this->redirect('{$admin_url}/{$controllerPath}/index');\n"; } else { $actions .= "\t\t\t\$this->flash('{$currentModelName} deleted: id '.\$id.'.', '{$admin_url}/{$controllerPath}/index');\n"; } $actions .= "\t\t}\n"; $actions .= "\t}\n"; $actions .= "\n"; return $actions; }/** * Action to create a Unit Test. * * @return Success */ function doUnitTest() { if (is_dir(VENDORS.'simpletest') || is_dir(ROOT.DS.APP_DIR.DS.'vendors'.DS.'simpletest')) { return true; } $unitTest = $this->getInput('Cake test suite not installed. Do you want to bake unit test files anyway?', array('y','n'), 'y'); $result = low($unitTest) == 'y' || low($unitTest) == 'yes'; if ($result) { $this->stdout("\nYou can download the Cake test suite from http://cakeforge.org/projects/testsuite/", true); } return $result; }/** * Creates a database configuration file for Bake. * * @param string $host * @param string $login * @param string $password * @param string $database */ function bakeDbConfig( $driver, $connect, $host, $login, $password, $database, $prefix) { $out = "<?php\n"; $out .= "class DATABASE_CONFIG {\n\n"; $out .= "\tvar \$default = array(\n"; $out .= "\t\t'driver' => '{$driver}',\n"; $out .= "\t\t'connect' => '{$connect}',\n"; $out .= "\t\t'host' => '{$host}',\n"; $out .= "\t\t'login' => '{$login}',\n"; $out .= "\t\t'password' => '{$password}',\n"; $out .= "\t\t'database' => '{$database}', \n"; $out .= "\t\t'prefix' => '{$prefix}' \n"; $out .= "\t);\n"; $out .= "}\n"; $out .= "?>"; $filename = CONFIGS.'database.php'; $this->__createFile($filename, $out); }/** * Assembles and writes a Model file. * * @param string $name * @param object $useDbConfig * @param string $useTable * @param string $primaryKey * @param array $validate * @param array $associations */ function bakeModel($name, $useDbConfig = 'default', $useTable = null, $primaryKey = 'id', $validate=array(), $associations=array()) { $out = "<?php\n"; $out .= "class {$name} extends AppModel {\n\n"; $out .= "\tvar \$name = '{$name}';\n"; if ($useDbConfig != 'default') { $out .= "\tvar \$useDbConfig = '$useDbConfig';\n"; } if ($useTable != null) { $out .= "\tvar \$useTable = '$useTable';\n"; } if ($primaryKey != 'id') { $out .= "\tvar \$primaryKey = '$primaryKey';\n"; } if (count($validate)) { $out .= "\tvar \$validate = array(\n"; $keys = array_keys($validate); for($i = 0; $i < count($validate); $i++) { $out .= "\t\t'" . $keys[$i] . "' => " . $validate[$keys[$i]] . ",\n"; } $out .= "\t);\n"; } $out .= "\n"; if(!empty($associations)) { $out.= "\t//The Associations below have been created with all possible keys, those that are not needed can be removed\n"; if(!empty($associations['belongsTo'])) { $out .= "\tvar \$belongsTo = array(\n"; for($i = 0; $i < count($associations['belongsTo']); $i++) { $out .= "\t\t\t'{$associations['belongsTo'][$i]['alias']}' =>\n"; $out .= "\t\t\t\tarray('className' => '{$associations['belongsTo'][$i]['className']}',\n"; $out .= "\t\t\t\t\t\t'foreignKey' => '{$associations['belongsTo'][$i]['foreignKey']}',\n"; $out .= "\t\t\t\t\t\t'conditions' => '',\n"; $out .= "\t\t\t\t\t\t'fields' => '',\n"; $out .= "\t\t\t\t\t\t'order' => '',\n"; $out .= "\t\t\t\t\t\t'counterCache' => ''\n"; $out .= "\t\t\t\t),\n\n"; } $out .= "\t);\n\n"; } if(!empty($associations['hasOne'])) { $out .= "\tvar \$hasOne = array(\n"; for($i = 0; $i < count($associations['hasOne']); $i++) { $out .= "\t\t\t'{$associations['hasOne'][$i]['alias']}' =>\n"; $out .= "\t\t\t\tarray('className' => '{$associations['hasOne'][$i]['className']}',\n"; $out .= "\t\t\t\t\t\t'foreignKey' => '{$associations['hasOne'][$i]['foreignKey']}',\n"; $out .= "\t\t\t\t\t\t'conditions' => '',\n"; $out .= "\t\t\t\t\t\t'fields' => '',\n"; $out .= "\t\t\t\t\t\t'order' => '',\n"; $out .= "\t\t\t\t\t\t'dependent' => ''\n"; $out .= "\t\t\t\t),\n\n"; } $out .= "\t);\n\n"; } if(!empty($associations['hasMany'])) { $out .= "\tvar \$hasMany = array(\n"; for($i = 0; $i < count($associations['hasMany']); $i++) { $out .= "\t\t\t'{$associations['hasMany'][$i]['alias']}' =>\n"; $out .= "\t\t\t\tarray('className' => '{$associations['hasMany'][$i]['className']}',\n"; $out .= "\t\t\t\t\t\t'foreignKe
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -