PHP Class BaseCCodeModel

Each code generator should have at least one code model class that extends from this class. The purpose of a code model is to represent user-supplied parameters and use them to generate customized code. Derived classes should implement the {@link prepare} method whose main task is to fill up the {@link files} property based on the user parameters. The {@link files} property should be filled with a set of {@link CCodeFile} instances, each representing a single code file to be generated. CCodeModel implements the feature of "sticky attributes". A sticky attribute is an attribute that can remember its last valid value, even if the user closes his browser window and reopen it. To declare an attribute is sticky, simply list it in a validation rule with the validator name being "sticky".
Since: 1.1.2
Author: Qiang Xue ([email protected])
Inheritance: extends CFormModel
Show file Open project: openeyes/openeyes Class Usage Examples

Public Properties

Property Type Description
$answers user confirmations on whether to overwrite existing code files with the newly generated ones. The value of this property is internally managed by this class and {@link CCodeGenerator}.
$files a list of {@link CCodeFile} objects that represent the code files to be generated. The {@link prepare()} method is responsible to populate this property.
$status the status of this model. T The value of this property is internally managed by {@link CCodeGenerator}.
$template the name of the code template that the user has selected. The value of this property is internally managed by this class and {@link CCodeGenerator}.

Public Methods

Method Description
attributeLabels ( ) : array Declares the model attribute labels.
class2id ( string $name ) : string Converts a class name into a HTML ID.
class2name ( string $name, boolean $ucwords = true ) : string Converts a class name into space-separated words.
class2var ( string $name ) : string Converts a class name into a variable name with the first letter in lower case.
classExists ( string $name ) : boolean Checks if the named class exists (in a case sensitive manner).
confirmed ( CCodeFile $file )
errorMessage ( ) : string Returns the message to be displayed when some error occurred during code file saving.
getStickyFile ( ) : string
getTemplatePath ( ) : string
getTemplates ( ) : array Returns a list of available code templates (name=>directory).
loadStickyAttributes ( ) Loads sticky attributes from a file and populates them into the model.
pluralize ( string $name ) : string Converts a word to its plural form.
prepare ( ) Prepares the code files to be generated.
render ( string $templateFile, array $_params_ = null ) : string Generates the code using the specified code template file.
renderResults ( ) : string
requiredTemplates ( ) : array Returns a list of code templates that are required.
rules ( ) : array Declares the model validation rules.
save ( ) Saves the generated code into files.
saveStickyAttributes ( ) Saves sticky attributes into a file.
sticky ( string $attribute, array $params ) The "sticky" validator.
successMessage ( ) : string Returns the message to be displayed when the newly generated code is saved successfully.
validateReservedWord ( string $attribute, array $params ) Validates an attribute to make sure it is not taking a PHP reserved keyword.
validateTemplate ( string $attribute, array $params ) Validates the template selection.

Method Details

attributeLabels() public method

Child classes must override this method in the following format:
return array_merge(parent::attributeLabels(), array(
    ...labels for the child class attributes...
));
.
public attributeLabels ( ) : array
return array the attribute labels

class2id() public method

For example, 'PostTag' will be converted as 'post-tag'.
public class2id ( string $name ) : string
$name string the string to be converted
return string the resulting ID

class2name() public method

For example, 'PostTag' will be converted as 'Post Tag'.
public class2name ( string $name, boolean $ucwords = true ) : string
$name string the string to be converted
$ucwords boolean whether to capitalize the first letter in each word
return string the resulting words

class2var() public method

This method is provided because lcfirst() PHP function is only available for PHP 5.3+.
Since: 1.1.4
public class2var ( string $name ) : string
$name string the class name
return string the variable name converted from the class name

classExists() public method

Checks if the named class exists (in a case sensitive manner).
public classExists ( string $name ) : boolean
$name string class name to be checked
return boolean whether the class exists

confirmed() public method

public confirmed ( CCodeFile $file )
$file CCodeFile whether the code file should be saved

errorMessage() public method

Child classes should override this method if the message needs to be customized.
public errorMessage ( ) : string
return string the message to be displayed when some error occurred during code file saving.

getStickyFile() public method

public getStickyFile ( ) : string
return string the file path that stores the sticky attribute values.

getTemplatePath() public method

public getTemplatePath ( ) : string
return string the directory that contains the template files.

getTemplates() public method

This method simply returns the {@link CCodeGenerator::templates} property value.
public getTemplates ( ) : array
return array a list of available code templates (name=>directory).

loadStickyAttributes() public method

Loads sticky attributes from a file and populates them into the model.

pluralize() public method

Note that this is for English only! For example, 'apple' will become 'apples', and 'child' will become 'children'.
public pluralize ( string $name ) : string
$name string the word to be pluralized
return string the pluralized word

prepare() abstract public method

This is the main method that child classes should implement. It should contain the logic that populates the {@link files} property with a list of code files to be generated.
abstract public prepare ( )

render() public method

This method is manly used in {@link generate} to generate code.
public render ( string $templateFile, array $_params_ = null ) : string
$templateFile string the code template file path
$_params_ array a set of parameters to be extracted and made available in the code template
return string the generated code

renderResults() public method

public renderResults ( ) : string
return string the code generation result log.

requiredTemplates() public method

Derived classes usually should override this method.
public requiredTemplates ( ) : array
return array list of code templates that are required. They should be file paths relative to {@link templatePath}.

rules() public method

Child classes must override this method in the following format:
return array_merge(parent::rules(), array(
    ...rules for the child class...
));
.
public rules ( ) : array
return array validation rules

save() public method

Saves the generated code into files.
public save ( )

saveStickyAttributes() public method

Saves sticky attributes into a file.

sticky() public method

This validator does not really validate the attributes. It actually saves the attribute value in a file to make it sticky.
public sticky ( string $attribute, array $params )
$attribute string the attribute to be validated
$params array the validation parameters

successMessage() public method

Child classes should override this method if the message needs to be customized.
public successMessage ( ) : string
return string the message to be displayed when the newly generated code is saved successfully.

validateReservedWord() public method

Validates an attribute to make sure it is not taking a PHP reserved keyword.
public validateReservedWord ( string $attribute, array $params )
$attribute string the attribute to be validated
$params array validation parameters

validateTemplate() public method

This method validates whether the user selects an existing template and the template contains all required template files as specified in {@link requiredTemplates}.
public validateTemplate ( string $attribute, array $params )
$attribute string the attribute to be validated
$params array validation parameters

Property Details

$answers public property

user confirmations on whether to overwrite existing code files with the newly generated ones. The value of this property is internally managed by this class and {@link CCodeGenerator}.
public $answers

$files public property

a list of {@link CCodeFile} objects that represent the code files to be generated. The {@link prepare()} method is responsible to populate this property.
public $files

$status public property

the status of this model. T The value of this property is internally managed by {@link CCodeGenerator}.
public $status

$template public property

the name of the code template that the user has selected. The value of this property is internally managed by this class and {@link CCodeGenerator}.
public $template