0 follower

CCodeGenerator

Package system.gii
Inheritance class CCodeGenerator » CController » CBaseController » CComponent
Since 1.1.2
Source Code framework/gii/CCodeGenerator.php
CCodeGenerator is the base class for code generator classes.

CCodeGenerator is a controller that predefines several actions for code generation purpose. Derived classes mainly need to configure the codeModel property override the getSuccessMessage method. The former specifies which code model (extending CCodeModel) that this generator should use, while the latter should return a success message to be displayed when code files are successfully generated.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
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 the code model class. CCodeGenerator
defaultAction string the name of the default action. CController
id string ID of the controller CController
layout string the layout to be used by the generator. CCodeGenerator
module CWebModule the module that this controller belongs to. CController
pageTitle string the page title CCodeGenerator
route string the route (module ID, controller ID and action ID) of the current request. CController
templates array a list of available code templates (name=>path) CCodeGenerator
uniqueId string the controller ID that is prefixed with the module ID (if any). CController
viewPath string Returns the view path of the generator. CCodeGenerator

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() CController
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
accessRules() Returns the access rules for this controller. CController
actionCode() The code preview action. CCodeGenerator
actionDiff() The code diff action. CCodeGenerator
actionIndex() The code generation action. CCodeGenerator
actions() Returns a list of external action classes. CController
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. 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() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. 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() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. 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() Evaluates a PHP expression or callback under the context of this component. 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() Returns 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() Returns stack of COutputCache objects CController
getClips() Returns the list of clips. CController
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getId() Returns ID of the controller CController
getLayoutFile() Looks for the layout view script based on the layout name. CController
getModule() Returns 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() Returns the page title CCodeGenerator
getRoute() Returns the route (module ID, controller ID and action ID) of the current request. CController
getUniqueId() Returns 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() Returns the view path of the generator. CCodeGenerator
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. 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() Raises an event. 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() Sets the action currently being executed. CController
setPageState() Saves a persistent page state value. CController
setPageTitle() Sets the page title. CController
setViewPath() Sets the view path of the generator. CCodeGenerator
widget() Creates a widget and executes it. CBaseController

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
afterAction() This method is invoked right after an action is executed. CController
afterRender() This method is invoked after the specified view 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() Prepares the code model. CCodeGenerator
replaceDynamicOutput() Replaces the dynamic content placeholders with actual content. CController
savePageStates() Saves page states as a base64 string. CController

Property Details

codeModel property
public string $codeModel;

the code model class. This can be either a class name (if it can be autoloaded) or a path alias referring to the class file. Child classes must configure this property with a concrete value.

layout property
public string $layout;

the layout to be used by the generator. Defaults to 'generator'.

pageTitle property
public string getPageTitle()
public void setPageTitle(string $value)

the page title

templates property
public array $templates;

a list of available code templates (name=>path)

viewPath property
public string getViewPath()
public void setViewPath(string $value)

Returns the view path of the generator. The "views" directory under the directory containing the generator class file will be returned.

Method Details

actionCode() method
public void actionCode()
Source Code: framework/gii/CCodeGenerator.php#80 (show)
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.');
}

The code preview action. This action shows up the specified generated code.

actionDiff() method
public void actionDiff()
Source Code: framework/gii/CCodeGenerator.php#98 (show)
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','sql')))
            
$diff=false;
        elseif(
$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.');
}

The code diff action. This action shows up the difference between the newly generated code and the corresponding existing code.

actionIndex() method
public void actionIndex()
Source Code: framework/gii/CCodeGenerator.php#61 (show)
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,
    ));
}

The code generation action. This is the action that displays the code generation interface. Child classes mainly need to provide the 'index' view for collecting user parameters for code generation.

getPageTitle() method
public string getPageTitle()
{return} string the page title
Source Code: framework/gii/CCodeGenerator.php#50 (show)
public function getPageTitle()
{
    return 
'Gii - '.ucfirst($this->id).' Generator';
}

getViewPath() method
public string getViewPath()
{return} string the view path of the generator
Source Code: framework/gii/CCodeGenerator.php#127 (show)
public function getViewPath()
{
    if(
$this->_viewPath===null)
    {
        
$class=new ReflectionClass(get_class($this));
        
$this->_viewPath=dirname($class->getFileName()).DIRECTORY_SEPARATOR.'views';
    }
    return 
$this->_viewPath;
}

Returns the view path of the generator. The "views" directory under the directory containing the generator class file will be returned.

prepare() method
protected void prepare()
Source Code: framework/gii/CCodeGenerator.php#148 (show)
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;
}

Prepares the code model.

setViewPath() method
public void setViewPath(string $value)
$value string the view path of the generator.
Source Code: framework/gii/CCodeGenerator.php#140 (show)
public function setViewPath($value)
{
    
$this->_viewPath=$value;
}