* @link http://www.yiiframework.com/ * @copyright Copyright © 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ * @version $Id$ */ /** * ControllerCommand generates a controller class. * * @author Qiang Xue * @version $Id$ * @package system.cli.commands.shell * @since 1.0 */ class ControllerCommand extends CConsoleCommand { /** * @var string the template file for the controller class. * Defaults to null, meaning using 'framework/cli/views/shell/controller/controller.php'. */ public $templateFile; public function getHelp() { return << [action-name] ... DESCRIPTION This command generates a controller and views associated with the specified actions. PARAMETERS * controller-name: required, the controller name. * action-name: optional, action name. You may supply one or or several action names. A default 'index' action will always be generated. EOD; } /** * Execute the action. * @param array command line parameters specific for this command */ public function run($args) { if(!isset($args[0])) { echo "Error: controller name is required.\n"; echo $this->getHelp(); return; } $controllerName=$args[0]; $controllerName[0]=strtolower($controllerName); $args[]='index'; $actions=array_unique(array_splice($args,1)); $controllerFile=ucfirst($controllerName).'Controller.php'; $templateFile=$this->templateFile===null?YII_PATH.'/cli/views/shell/controller/controller.php':$this->templateFile; $list=array( $controllerFile=>array( 'source'=>$templateFile, 'target'=>Yii::app()->controllerPath.DIRECTORY_SEPARATOR.$controllerFile, 'callback'=>array($this,'generateController'), 'params'=>array(ucfirst($controllerName).'Controller', $actions), ), ); $viewPath=Yii::app()->viewPath.DIRECTORY_SEPARATOR.$controllerName; foreach($actions as $name) { $list[$name.'.php']=array( 'source'=>YII_PATH.'/cli/views/shell/controller/view.php', 'target'=>$viewPath.DIRECTORY_SEPARATOR.$name.'.php', ); } $this->copyFiles($list); $path=Yii::app()->controllerPath.DIRECTORY_SEPARATOR.$controllerFile; echo <<render('{View}'); } EOD; $actionCode=''; foreach($actions as $name) $actionCode.=strtr($actionTemplate,array('{Name}'=>ucfirst($name),'{View}'=>$name)); return strtr($content,array( '{ClassName}'=>$className, '{Actions}'=>$actionCode)); } }