Abstract Class yii\gii\Generator
Inheritance | yii\gii\Generator » yii\base\Model |
---|---|
Subclasses | yii\gii\generators\controller\Generator, yii\gii\generators\crud\Generator, yii\gii\generators\extension\Generator, yii\gii\generators\form\Generator, yii\gii\generators\model\Generator, yii\gii\generators\module\Generator |
Available since version | 2.0 |
This is the base class for all generator classes.
A generator instance is responsible for taking user inputs, validating them, and using them to generate the corresponding code based on a set of code template files.
A generator class typically needs to implement the following methods:
- getName(): returns the name of the generator
- getDescription(): returns the detailed description of the generator
- generate(): generates the code based on the current user input and the specified code template files. This is the place where main code generation code resides.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$enableI18N | boolean | Whether the strings will be generated using Yii::t() or normal strings. |
yii\gii\Generator |
$messageCategory | string | The message category used by Yii::t() when $enableI18N is true . |
yii\gii\Generator |
$template | string | The name of the code template that the user has selected. | yii\gii\Generator |
$templates | array | A list of available code templates. | yii\gii\Generator |
Public Methods
Method | Description | Defined By |
---|---|---|
attributeLabels() | yii\gii\Generator | |
autoCompleteData() | Returns the list of auto complete values. | yii\gii\Generator |
defaultTemplate() | Returns the root path to the default code template files. | yii\gii\Generator |
formView() | Returns the view file for the input form of the generator. | yii\gii\Generator |
generate() | Generates the code based on the current user input and the specified code template files. | yii\gii\Generator |
generateString() | Generates a string depending on enableI18N property | yii\gii\Generator |
getDescription() | yii\gii\Generator | |
getName() | yii\gii\Generator | |
getStickyDataFile() | yii\gii\Generator | |
getTemplatePath() | yii\gii\Generator | |
hints() | Returns the list of hint messages. | yii\gii\Generator |
init() | yii\gii\Generator | |
isReservedKeyword() | yii\gii\Generator | |
loadStickyAttributes() | Loads sticky attributes from an internal file and populates them into the generator. | yii\gii\Generator |
render() | Generates code using the specified code template and parameters. | yii\gii\Generator |
requiredTemplates() | Returns a list of code template files that are required. | yii\gii\Generator |
rules() | yii\gii\Generator | |
save() | Saves the generated code into files. | yii\gii\Generator |
saveStickyAttributes() | Saves sticky attributes into an internal file. | yii\gii\Generator |
stickyAttributes() | Returns the list of sticky attributes. | yii\gii\Generator |
successMessage() | Returns the message to be displayed when the newly generated code is saved successfully. | yii\gii\Generator |
validateClass() | An inline validator that checks if the attribute value refers to an existing class name. | yii\gii\Generator |
validateMessageCategory() | Checks if message category is not empty when I18N is enabled. | yii\gii\Generator |
validateNewClass() | An inline validator that checks if the attribute value refers to a valid namespaced class name. | yii\gii\Generator |
validateTemplate() | Validates the template selection. | yii\gii\Generator |
Property Details
Whether the strings will be generated using Yii::t()
or normal strings.
The message category used by Yii::t()
when $enableI18N
is true
.
Defaults to app
.
The name of the code template that the user has selected. The value of this property is internally managed by this class.
A list of available code templates. The array keys are the template names, and the array values are the corresponding template paths or path aliases.
Method Details
public void attributeLabels ( ) |
Returns the list of auto complete values.
The array keys are the attribute names, and the array values are the corresponding auto complete values. Auto complete values can also be callable typed in order one want to make postponed data generation.
public array autoCompleteData ( ) | ||
return | array | The list of auto complete values |
---|
Returns the root path to the default code template files.
The default implementation will return the "templates" subdirectory of the directory containing the generator class file.
public string defaultTemplate ( ) | ||
return | string | The root path to the default code template files. |
---|
Returns the view file for the input form of the generator.
The default implementation will return the "form.php" file under the directory that contains the generator class file.
public string formView ( ) | ||
return | string | The view file for the input form of the generator. |
---|
Generates the code based on the current user input and the specified code template files.
This is the main method that child classes should implement. Please refer to yii\gii\generators\controller\Generator::generate() as an example on how to implement this method.
public abstract yii\gii\CodeFile[] generate ( ) | ||
return | yii\gii\CodeFile[] | A list of code files to be created. |
---|
Generates a string depending on enableI18N property
public string generateString ( $string = '', $placeholders = [] ) | ||
$string | string | The text be generated |
$placeholders | array | The placeholders to use by |
public string getDescription ( ) | ||
return | string | The detailed description of the generator. |
---|
public abstract string getName ( ) | ||
return | string | Name of the code generator |
---|
public string getStickyDataFile ( ) | ||
return | string | The file path that stores the sticky attribute values. |
---|
public string getTemplatePath ( ) | ||
return | string | The root path of the template files that are currently being used. |
---|---|---|
throws | \yii\base\InvalidConfigException | if $template is invalid |
Returns the list of hint messages.
The array keys are the attribute names, and the array values are the corresponding hint messages. Hint messages will be displayed to end users when they are filling the form for the generator.
public array hints ( ) | ||
return | array | The list of hint messages |
---|
public void init ( ) |
public boolean isReservedKeyword ( $value ) | ||
$value | string | The attribute to be validated |
return | boolean | Whether the value is a reserved PHP keyword. |
---|
Loads sticky attributes from an internal file and populates them into the generator.
public void loadStickyAttributes ( ) |
Generates code using the specified code template and parameters.
Note that the code template will be used as a PHP file.
public string render ( $template, $params = [] ) | ||
$template | string | The code template file. This must be specified as a file path relative to \yii\gii\templatePath. |
$params | array | List of parameters to be passed to the template file. |
return | string | The generated code |
---|
Returns a list of code template files that are required.
Derived classes usually should override this method if they require the existence of certain template files.
public array requiredTemplates ( ) | ||
return | array | List of code template files that are required. They should be file paths relative to \yii\gii\templatePath. |
---|
Child classes should override this method like the following so that the parent rules are included:
return array_merge(parent::rules(), [
...rules for the child class...
]);
public void rules ( ) |
Saves the generated code into files.
public boolean save ( $files, $answers, &$results ) | ||
$files | yii\gii\CodeFile[] | The code files to be saved |
$answers | array | |
$results | string | This parameter receives a value from this method indicating the log messages generated while saving the code files. |
return | boolean | Whether files are successfully saved without any error. |
---|
Saves sticky attributes into an internal file.
public void saveStickyAttributes ( ) |
Returns the list of sticky attributes.
A sticky attribute will remember its value and will initialize the attribute with this value when the generator is restarted.
public array stickyAttributes ( ) | ||
return | array | List of sticky attributes |
---|
Returns the message to be displayed when the newly generated code is saved successfully.
Child classes may override this method to customize the message.
public string successMessage ( ) | ||
return | string | The message to be displayed when the newly generated code is saved successfully. |
---|
An inline validator that checks if the attribute value refers to an existing class name.
If the extends
option is specified, it will also check if the class is a child class
of the class represented by the extends
option.
public void validateClass ( $attribute, $params ) | ||
$attribute | string | The attribute being validated |
$params | array | The validation options |
Checks if message category is not empty when I18N is enabled.
public void validateMessageCategory ( ) |
An inline validator that checks if the attribute value refers to a valid namespaced class name.
The validator will check if the directory containing the new class file exist or not.
public void validateNewClass ( $attribute, $params ) | ||
$attribute | string | The attribute being validated |
$params | array | The validation options |
Validates the template selection.
This method validates whether the user selects an existing template and the template contains all required template files as specified in requiredTemplates().
public void validateTemplate ( ) |