| 包 | system.gii |
|---|---|
| 继承 | class CCodeGenerator » CController » CBaseController » CComponent |
| 源自 | 1.1.2 |
| 版本 | $Id: CCodeGenerator.php 3426 2011-10-25 00:01:09Z alexander.makarow $ |
| 源码 |
CCodeGenerator是各种代码生成器类的基类。
CCodeGenerator是一个控制器,它为了代码生成的目的预 定义了若干动作。派生类主要需要配置codeModel 属性和覆盖getSuccessMessage方法。前者 指定了生成器将使用哪一个代码模型(扩展自CCodeModel), 而后者则定义了当代码文件被成功生成后返回的需要 显示的成功信息。
CCodeGenerator是一个控制器,它为了代码生成的目的预 定义了若干动作。派生类主要需要配置codeModel 属性和覆盖getSuccessMessage方法。前者 指定了生成器将使用哪一个代码模型(扩展自CCodeModel), 而后者则定义了当代码文件被成功生成后返回的需要 显示的成功信息。
公共属性
| 属性 | 类型 | 描述 | 定义在 |
|---|---|---|---|
| action | CAction | the action currently being executed, null if no active action. | CController |
| actionParams | array | Returns the request parameters that will be used for action parameter binding. | CController |
| cachingStack | CStack | stack of COutputCache objects | CController |
| clips | CMap | Returns the list of clips. | CController |
| codeModel | string | 代码模型类。它可以是一个类的名字(如果该类可以被自动加载) 或一个指向类文件的路径别名。 子类必须使用一个具体的值配置这个属性。 | CCodeGenerator |
| defaultAction | string | the name of the default action. | CController |
| id | string | ID of the controller | CController |
| layout | string | 生成器使用的布局。默认值是“generator”。 | CCodeGenerator |
| module | CWebModule | the module that this controller belongs to. | CController |
| pageTitle | string | 页面标题 | CCodeGenerator |
| route | string | the route (module ID, controller ID and action ID) of the current request. | CController |
| templates | array | 可用的代码模板的列表 (name=>path) | CCodeGenerator |
| uniqueId | string | the controller ID that is prefixed with the module ID (if any). | CController |
| viewPath | string | 返回生成器的视图的路径。 | CCodeGenerator |
公共方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| __call() | 如果类中没有调的方法名,则调用这个方法。 | CComponent |
| __construct() | CController | |
| __get() | 返回一个属性值、一个事件处理程序列表或一个行为名称。 | CComponent |
| __isset() | 检查一个属性是否为null。 | CComponent |
| __set() | 设置一个组件的属性值。 | CComponent |
| __unset() | 设置一个组件的属性为null。 | CComponent |
| accessRules() | Returns the access rules for this controller. | CController |
| actionCode() | 代码预览动作。 | CCodeGenerator |
| actionDiff() | 代码差异动作。 | CCodeGenerator |
| actionIndex() | 生成代码的动作。 | CCodeGenerator |
| actions() | Returns a list of external action classes. | CController |
| asa() | 返回这个名字的行为对象。 | CComponent |
| attachBehavior() | 附加一个行为到组件。 | CComponent |
| attachBehaviors() | 附加一个行为列表到组件。 | CComponent |
| attachEventHandler() | 为事件附加一个事件处理程序。 | CComponent |
| beginCache() | Begins fragment caching. | CBaseController |
| beginClip() | Begins recording a clip. | CBaseController |
| beginContent() | Begins the rendering of content that is to be decorated by the specified view. | CBaseController |
| beginWidget() | Creates a widget and executes it. | CBaseController |
| behaviors() | Returns a list of behaviors that this controller should behave as. | CController |
| canGetProperty() | 确定属性是否可读。 | CComponent |
| canSetProperty() | 确定属性是否可写。 | CComponent |
| clearPageStates() | Removes all page states. | CController |
| createAbsoluteUrl() | Creates an absolute URL for the specified action defined in this controller. | CController |
| createAction() | Creates the action instance based on the action name. | CController |
| createUrl() | Creates a relative URL for the specified action defined in this controller. | CController |
| createWidget() | Creates a widget and initializes it. | CBaseController |
| detachBehavior() | 从组件中分离一个行为。 | CComponent |
| detachBehaviors() | 从组件中分离所有行为。 | CComponent |
| detachEventHandler() | 分离一个存在的事件处理程序。 | CComponent |
| disableBehavior() | 禁用一个附加行为。 | CComponent |
| disableBehaviors() | 禁用组件附加的所有行为。 | CComponent |
| enableBehavior() | 启用一个附加行为。 | CComponent |
| enableBehaviors() | 启用组件附加的所有行为。 | CComponent |
| endCache() | Ends fragment caching. | CBaseController |
| endClip() | Ends recording a clip. | CBaseController |
| endContent() | Ends the rendering of content. | CBaseController |
| endWidget() | Ends the execution of the named widget. | CBaseController |
| evaLuateExpression() | 计算一个PHP表达式,或根据组件上下文执行回调。 | CComponent |
| filterAccessControl() | The filter method for 'accessControl' filter. | CController |
| filterAjaxOnly() | The filter method for 'ajaxOnly' filter. | CController |
| filterPostOnly() | The filter method for 'postOnly' filter. | CController |
| filters() | Returns the filter configurations. | CController |
| forward() | Processes the request using another controller action. | CController |
| getAction() | 返回the action currently being executed, null if no active action. | CController |
| getActionParams() | Returns the request parameters that will be used for action parameter binding. | CController |
| getCachingStack() | 返回stack of COutputCache objects | CController |
| getClips() | Returns the list of clips. | CController |
| getEventHandlers() | 返回一个事件的附加处理程序列表。 | CComponent |
| getId() | 返回ID of the controller | CController |
| getLayoutFile() | Looks for the layout view script based on the layout name. | CController |
| getModule() | 返回the module that this controller belongs to. It returns null if the controller does not belong to any module | CController |
| getPageState() | Returns a persistent page state value. | CController |
| getPageTitle() | 返回页面标题 | CCodeGenerator |
| getRoute() | 返回the route (module ID, controller ID and action ID) of the current request. | CController |
| getUniqueId() | 返回the controller ID that is prefixed with the module ID (if any). | CController |
| getViewFile() | Looks for the view file according to the given view name. | CController |
| getViewPath() | 返回生成器的视图的路径。 | CCodeGenerator |
| hasEvent() | 确定一个事件是否定义。 | CComponent |
| hasEventHandler() | 检查事件是否有附加的处理程序。 | CComponent |
| hasProperty() | 确定属性是否被定义。 | CComponent |
| init() | Initializes the controller. | CController |
| invalidActionParams() | This method is invoked when the request parameters do not satisfy the requirement of the specified action. | CController |
| isCachingStackEmpty() | Returns whether the caching stack is empty. | CController |
| missingAction() | Handles the request whose action is not recognized. | CController |
| processDynamicOutput() | Postprocesses the dynamic output. | CController |
| processOutput() | Postprocesses the output generated by render(). | CController |
| raiseEvent() | 发起一个事件。 | CComponent |
| recordCachingAction() | Records a method call when an output cache is in effect. | CController |
| redirect() | Redirects the browser to the specified URL or route (controller/action). | CController |
| refresh() | Refreshes the current page. | CController |
| render() | Renders a view with a layout. | CController |
| renderClip() | Renders a named clip with the supplied parameters. | CController |
| renderDynamic() | Renders dynamic content returned by the specified callback. | CController |
| renderDynamicInternal() | This method is internally used. | CController |
| renderFile() | Renders a view file. | CBaseController |
| renderInternal() | Renders a view file. | CBaseController |
| renderPartial() | Renders a view. | CController |
| renderText() | Renders a static text string. | CController |
| resolveViewFile() | Finds a view file based on its name. | CController |
| run() | Runs the named action. | CController |
| runAction() | Runs the action after passing through all filters. | CController |
| runActionWithFilters() | Runs an action with the specified filters. | CController |
| setAction() | 设置the action currently being executed. | CController |
| setPageState() | Saves a persistent page state value. | CController |
| setPageTitle() | 设置the page title. | CController |
| setViewPath() | 设置生成器的视图的路径。 | CCodeGenerator |
| widget() | Creates a widget and executes it. | CBaseController |
受保护方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| afterAction() | This method is invoked right after an action is executed. | CController |
| afterRender() | This method is invoked after the specified is rendered by calling render(). | CController |
| beforeaction() | This method is invoked right before an action is to be executed (after all possible filters.) | CController |
| beforeRender() | This method is invoked at the beginning of render(). | CController |
| createActionFromMap() | Creates the action instance based on the action map. | CController |
| loadPageStates() | Loads page states from a hidden input. | CController |
| prepare() | 准备代码模型。 | CCodeGenerator |
| replaceDynamicOutput() | Replaces the dynamic content placeholders with actual content. | CController |
| savePageStates() | Saves page states as a base64 string. | CController |
属性详细
codeModel
属性
public string $codeModel;
代码模型类。它可以是一个类的名字(如果该类可以被自动加载) 或一个指向类文件的路径别名。 子类必须使用一个具体的值配置这个属性。
layout
属性
public string $layout;
生成器使用的布局。默认值是“generator”。
pageTitle
属性
public string getPageTitle()
public void setPageTitle(string $value)
public void setPageTitle(string $value)
页面标题
templates
属性
public array $templates;
可用的代码模板的列表 (name=>path)
viewPath
属性
public string getViewPath()
public void setViewPath(string $value)
public void setViewPath(string $value)
返回生成器的视图的路径。 返回生成器类文件所在目录下的“views”子目录。
方法详细
actionCode()
方法
|
public void actionCode()
|
public function actionCode()
{
$model=$this->prepare();
if(isset($_GET['id']) && isset($model->files[$_GET['id']]))
{
$this->renderPartial('/common/code', array(
'file'=>$model->files[$_GET['id']],
));
}
else
throw new CHttpException(404,'Unable to find the code you requested.');
}
代码预览动作。 此动作显示指定的已生成代码。
actionDiff()
方法
|
public void actionDiff()
|
public function actionDiff()
{
Yii::import('gii.components.TextDiff');
$model=$this->prepare();
if(isset($_GET['id']) && isset($model->files[$_GET['id']]))
{
$file=$model->files[$_GET['id']];
if(!in_array($file->type,array('php', 'txt','js','css')))
$diff=false;
else if($file->operation===CCodeFile::OP_OVERWRITE)
$diff=TextDiff::compare(file_get_contents($file->path), $file->content);
else
$diff='';
$this->renderPartial('/common/diff',array(
'file'=>$file,
'diff'=>$diff,
));
}
else
throw new CHttpException(404,'Unable to find the code you requested.');
}
代码差异动作。 此动作显示新生成的代码与已有代码之间的不同。
actionIndex()
方法
|
public void actionIndex()
|
public function actionIndex()
{
$model=$this->prepare();
if($model->files!=array() && isset($_POST['generate'], $_POST['answers']))
{
$model->answers=$_POST['answers'];
$model->status=$model->save() ? CCodeModel::STATUS_SUCCESS : CCodeModel::STATUS_ERROR;
}
$this->render('index',array(
'model'=>$model,
));
}
生成代码的动作。 这个动作显示代码生成的界面。 子类主要需要提供'index'视图以为代码生成 收集用户参数。
getPageTitle()
方法
|
public string getPageTitle()
| ||
| {return} | string | 页面标题 |
public function getPageTitle()
{
return 'Gii - '.ucfirst($this->id).' Generator';
}
getViewPath()
方法
|
public string getViewPath()
| ||
| {return} | string | 生成器的视图的路径。 |
public function getViewPath()
{
if($this->_viewPath===null)
{
$class=new ReflectionClass(get_class($this));
$this->_viewPath=dirname($class->getFileName()).DIRECTORY_SEPARATOR.'views';
}
return $this->_viewPath;
}
返回生成器的视图的路径。 返回生成器类文件所在目录下的“views”子目录。
prepare()
方法
|
protected void prepare()
|
protected function prepare()
{
if($this->codeModel===null)
throw new CException(get_class($this).'.codeModel property must be specified.');
$modelClass=Yii::import($this->codeModel,true);
$model=new $modelClass;
$model->loadStickyAttributes();
if(isset($_POST[$modelClass]))
{
$model->attributes=$_POST[$modelClass];
$model->status=CCodeModel::STATUS_PREVIEW;
if($model->validate())
{
$model->saveStickyAttributes();
$model->prepare();
}
}
return $model;
}
准备代码模型。
setViewPath()
方法
|
public void setViewPath(string $value)
| ||
| $value | string | 生成器的视图的路径。 |
public function setViewPath($value)
{
$this->_viewPath=$value;
}