0 follower

Final Class Yiisoft\Yii\Gii\Generator\Controller\Generator

InheritanceYiisoft\Yii\Gii\Generator\Controller\Generator » Yiisoft\Yii\Gii\Generator\AbstractGenerator
ImplementsYiisoft\Yii\Gii\GeneratorInterface

This generator will generate a controller and one or a few action view files.

Protected Methods

Hide inherited methods

Method Description Defined By
render() Generates code using the specified code template and parameters. Yiisoft\Yii\Gii\Generator\AbstractGenerator

Method Details

Hide inherited methods

__construct() public method
public __construct( \Yiisoft\Aliases\Aliases $aliases, \Yiisoft\Validator\ValidatorInterface $validator, Yiisoft\Yii\Gii\ParametersProvider $parametersProvider ): mixed
$aliases \Yiisoft\Aliases\Aliases
$validator \Yiisoft\Validator\ValidatorInterface
$parametersProvider Yiisoft\Yii\Gii\ParametersProvider

                public function __construct(
    protected Aliases $aliases,
    protected ValidatorInterface $validator,
    protected ParametersProvider $parametersProvider,
) {
}

            
doGenerate() public method

public doGenerate( Yiisoft\Yii\Gii\GeneratorCommandInterface $command ): array
$command Yiisoft\Yii\Gii\GeneratorCommandInterface

                public function doGenerate(GeneratorCommandInterface $command): array
{
    if (!$command instanceof Command) {
        throw new InvalidArgumentException();
    }
    $files = [];
    $rootPath = $this->aliases->get('@root');
    $codeFile = (new CodeFile(
        $this->getControllerFile($command),
        $this->render($command, 'controller.php')
    ))->withBasePath($rootPath);
    $files[$codeFile->getId()] = $codeFile;
    foreach ($command->getActions() as $action) {
        $codeFile = (new CodeFile(
            $this->getViewFile($command, $action),
            $this->render($command, 'view.php', ['action' => $action])
        ))->withBasePath($rootPath);
        $files[$codeFile->getId()] = $codeFile;
    }
    return $files;
}

            
generate() public method
public generate( Yiisoft\Yii\Gii\GeneratorCommandInterface $command ): Yiisoft\Yii\Gii\Component\CodeFile\CodeFile[]
$command Yiisoft\Yii\Gii\GeneratorCommandInterface
throws Yiisoft\Yii\Gii\Exception\InvalidGeneratorCommandException

                final public function generate(GeneratorCommandInterface $command): array
{
    $result = $this->validator->validate($command);
    if (!$result->isValid()) {
        throw new InvalidGeneratorCommandException($result);
    }
    return $this->doGenerate($command);
}

            
getCommandClass() public static method

public static getCommandClass( ): string

                public static function getCommandClass(): string
{
    return Command::class;
}

            
getDescription() public static method

public static getDescription( ): string

                public static function getDescription(): string
{
    return 'This generator helps you to quickly generate a new controller class with
        one or several controller actions and their corresponding views.';
}

            
getId() public static method

public static getId( ): string

                public static function getId(): string
{
    return 'controller';
}

            
getName() public static method

public static getName( ): string

                public static function getName(): string
{
    return 'Controller';
}

            
getRequiredTemplates() public method

public getRequiredTemplates( ): array

                public function getRequiredTemplates(): array
{
    return [
        'controller.php',
        'view.php',
    ];
}

            
getTemplatePath() public method
public getTemplatePath( Yiisoft\Yii\Gii\GeneratorCommandInterface $command ): string
$command Yiisoft\Yii\Gii\GeneratorCommandInterface

                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}\"");
}

            
getViewFile() public method

public getViewFile( Yiisoft\Yii\Gii\Generator\Controller\Command $command, string $action ): string
$command Yiisoft\Yii\Gii\Generator\Controller\Command
$action string

The action ID

return string

The action view file path

                public function getViewFile(Command $command, string $action): string
{
    $directory = empty($command->getViewsPath()) ? '@views/' : $command->getViewsPath();
    return $this->aliases->get(
        str_replace(
            ['\\', '//'],
            '/',
            sprintf(
                '%s/%s/%s.php',
                $directory,
                $command->getControllerID(),
                $action,
            ),
        ),
    );
}

            
render() protected method

Defined in: Yiisoft\Yii\Gii\Generator\AbstractGenerator::render()

Generates code using the specified code template and parameters.

Note that the code template will be used as a PHP file.

protected render( Yiisoft\Yii\Gii\GeneratorCommandInterface $command, string $templateFile, array $params = [] ): string
$command Yiisoft\Yii\Gii\GeneratorCommandInterface
$templateFile string

The code template file. This must be specified as a file path relative to getTemplatePath().

$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;
    }
}