?? controller.php
字號:
<?php/* SVN FILE: $Id: controller.php 7118 2008-06-04 20:49:29Z gwoo $ *//** * The ControllerTask handles creating and updating controller files. * * Long description for file * * PHP versions 4 and 5 * * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> * Copyright 2005-2008, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. * @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: 7118 $ * @modifiedby $LastChangedBy: gwoo $ * @lastmodified $Date: 2008-06-04 13:49:29 -0700 (Wed, 04 Jun 2008) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License *//** * Task class for creating and updating controller files. * * @package cake * @subpackage cake.cake.console.libs.tasks */class ControllerTask 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('Project');/** * path to CONTROLLERS directory * * @var array * @access public */ var $path = CONTROLLERS;/** * Override initialize * * @access public */ function initialize() { }/** * Execution method always used for tasks * * @access public */ function execute() { if (empty($this->args)) { $this->__interactive(); } if (isset($this->args[0])) { $controller = Inflector::camelize($this->args[0]); $actions = null; if (isset($this->args[1]) && $this->args[1] == 'scaffold') { $this->out('Baking scaffold for ' . $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()) { $this->out('Adding ' . Configure::read('Routing.admin') .' methods'); if ($actions == 'scaffold') { $actions = $this->bakeActions($controller, $admin); } else { $actions .= $this->bakeActions($controller, $admin); } } } if ($this->bake($controller, $actions)) { if ($this->_checkUnitTest()) { $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(); } $this->hr(); $this->out("Baking {$controllerName}Controller"); $this->hr(); $controllerFile = low(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'); if (low($doItInteractive) == 'y' || low($doItInteractive) == 'yes') { $this->interactive = true; $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 (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'); 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'; } } 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'); } } $admin = false; if ((low($wannaDoAdmin) == 'y' || low($wannaDoAdmin) == 'yes')) { $admin = $this->getAdmin(); } 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 ($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(); $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 ($baked && $this->_checkUnitTest()) { $this->bakeTest($controllerName); } } else { $this->__interactive($controllerName); } } else { $baked = $this->bake($controllerName, $actions, $helpers, $components, $uses); if ($baked && $this->_checkUnitTest()) { $this->bakeTest($controllerName); } } }/** * Bake scaffold actions * * @param string $controllerName Controller name * @param string $admin Admin route to use * @param boolean $wannaUseSession Set to true to use sessions, false otherwise * @return string Baked actions * @access private */ function bakeActions($controllerName, $admin = null, $wannaUseSession = true) { $currentModelName = $this->_modelName($controllerName); if (!App::import('Model', $currentModelName)) { $this->err(__('You must have a model for this class to build scaffold methods. Please try again.', true)); exit; } $actions = null; $modelObj =& new $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\t\$this->_stop();\n"; }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -