Abstract Class Yiisoft\Yii\Gii\Generator\AbstractGenerator
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:
- {@see \
Yiisoft\ Yii\ Gii\ GeneratorInterface::getName()}: returns the name of the generator - {@see \
Yiisoft\ Yii\ Gii\ GeneratorInterface::getDescription()}: returns the detailed description of the generator - {@see \
Yiisoft\ Yii\ Gii\ GeneratorInterface::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.
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $aliases | \ |
Yiisoft\ |
|
| $parametersProvider | Yiisoft\ |
Yiisoft\ |
|
| $validator | \ |
Yiisoft\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| generate() | Yiisoft\ |
|
| getCommandClass() | Yiisoft\ |
|
| getDescription() | Returns the detailed description of the generator | Yiisoft\ |
| getId() | Returns the id of the generator | Yiisoft\ |
| getName() | Returns the name of the generator | Yiisoft\ |
| getRequiredTemplates() | Yiisoft\ |
|
| getTemplatePath() | Yiisoft\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| doGenerate() | Yiisoft\ |
|
| render() | Generates code using the specified code template and parameters. | Yiisoft\ |
Property Details
Method Details
| public mixed __construct ( \ | ||
| $aliases | \ |
|
| $validator | \ |
|
| $parametersProvider | Yiisoft\ |
|
public function __construct(
protected Aliases $aliases,
protected ValidatorInterface $validator,
protected ParametersProvider $parametersProvider,
) {}
| protected abstract array doGenerate ( Yiisoft\ | ||
| $command | Yiisoft\ |
|
abstract protected function doGenerate(GeneratorCommandInterface $command): array;
final public function generate(GeneratorCommandInterface $command): array
{
$result = $this->validator->validate($command);
if (!$result->isValid()) {
throw new InvalidGeneratorCommandException($result);
}
return $this->doGenerate($command);
}
| public abstract static string getCommandClass ( ) |
public static function getCommandClass(): string;
Defined in:
Yiisoft\
Returns the detailed description of the generator
| public abstract static string getDescription ( ) |
public static function getDescription(): string;
Defined in:
Yiisoft\
Returns the id of the generator
| public abstract static string getId ( ) |
public static function getId(): string;
Defined in:
Yiisoft\
Returns the name of the generator
| public abstract static string getName ( ) |
public static function getName(): string;
| public string getTemplatePath ( Yiisoft\ | ||
| $command | Yiisoft\ |
|
public function getTemplatePath(GeneratorCommandInterface $command): string
{
$template = $command->getTemplate();
if ($template === 'default') {
return $this->defaultTemplate();
}
$templates = $this->parametersProvider->getTemplates(static::getId());
return $templates[$template] ?? throw new InvalidConfigException("Unknown template: \"{$template}\"");
}
Generates code using the specified code template and parameters.
Note that the code template will be used as a PHP file.
| protected string render ( Yiisoft\ | ||
| $command | Yiisoft\ |
|
| $templateFile | string |
The code template file. This must be specified as a file path
relative to {@see \ |
| $params | array |
List of parameters to be passed to the template file. |
| return | string |
The generated code |
|---|---|---|
| throws | Throwable | |
protected function render(GeneratorCommandInterface $command, string $templateFile, array $params = []): string
{
$file = sprintf(
'%s/%s',
$this->aliases->get($this->getTemplatePath($command)),
$templateFile,
);
$renderer = function (): void {
/** @psalm-suppress MixedArgument,PossiblyFalseArgument */
extract(func_get_arg(1));
/** @psalm-suppress UnresolvableInclude */
require func_get_arg(0);
};
$obInitialLevel = ob_get_level();
ob_start();
ob_implicit_flush(false);
try {
/** @psalm-suppress PossiblyNullFunctionCall */
$renderer->bindTo($this)($file, array_merge($params, ['command' => $command]));
return (string) ob_get_clean();
} catch (Throwable $e) {
while (ob_get_level() > $obInitialLevel) {
if (!@ob_end_clean()) {
ob_clean();
}
}
throw $e;
}
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.