CodeIgniter v4.5 API

GeneratorTrait

GeneratorTrait contains a collection of methods to build the commands that generates a file.

Table of Contents

Properties

$classNameLang  : string
Language string key for required class names.
$component  : string
Component Name
$directory  : string
File directory
$namespace  : string|null
Namespace to use for class.
$template  : string
View template name for fallback
$templatePath  : string|null
(Optional) View template path

Methods

basename()  : string
Change file basename before saving.
buildContent()  : string
Builds the contents for class being generated, doing all the replacements necessary, and alphabetically sorts the imports for a given template.
buildPath()  : string
Builds the file path from the class name.
execute()  : void
Execute the command.
generateClass()  : void
Generates a class file from an existing template.
generateView()  : void
Generate a view file from an existing template.
getNamespace()  : string
Gets the namespace from the command-line option, or the default namespace if the option is not set.
getOption()  : bool|string|null
Gets a single command-line option. Returns TRUE if the option exists, but doesn't have a value, and is simply acting as a flag.
parseTemplate()  : string
Performs pseudo-variables contained within view file.
prepare()  : string
Prepare options and do the necessary replacements.
qualifyClassName()  : string
Parses the class name and checks if it is already qualified.
renderTemplate()  : string
Gets the generator view as defined in the `Config\Generators::$views`, with fallback to `$template` when the defined view does not exist.
setEnabledSuffixing()  : $this
Allows child generators to modify the internal `$enabledSuffixing` flag.
setHasClassName()  : $this
Allows child generators to modify the internal `$hasClassName` flag.
setSortImports()  : $this
Allows child generators to modify the internal `$sortImports` flag.
generateFile()  : void
Handles writing the file to disk, and all of the safety checks around that.
normalizeInputClassName()  : string
Normalize input classname.

Properties

$classNameLang

Language string key for required class names.

protected string $classNameLang = ''

$namespace

Namespace to use for class.

protected string|null $namespace = null

Leave null to use the default namespace.

$template

View template name for fallback

protected string $template

$templatePath

(Optional) View template path

protected string|null $templatePath = null

We use special namespaced paths like: CodeIgniter\Commands\Generators\Views\cell.tpl.php.

Methods

basename()

Change file basename before saving.

protected basename(string $filename) : string

Useful for components where the file name has a date.

Parameters
$filename : string
Return values
string

buildContent()

Builds the contents for class being generated, doing all the replacements necessary, and alphabetically sorts the imports for a given template.

protected buildContent(string $class) : string
Parameters
$class : string
Return values
string

buildPath()

Builds the file path from the class name.

protected buildPath(string $class) : string
Parameters
$class : string

namespaced classname or namespaced view.

Return values
string

execute()

Execute the command.

protected execute(array<int|string, string|null> $params) : void

use generateClass() instead

Parameters
$params : array<int|string, string|null>

generateClass()

Generates a class file from an existing template.

protected generateClass(array<int|string, string|null> $params) : void
Parameters
$params : array<int|string, string|null>

generateView()

Generate a view file from an existing template.

protected generateView(string $view, array<int|string, string|null> $params) : void
Parameters
$view : string

namespaced view name that is generated

$params : array<int|string, string|null>

getNamespace()

Gets the namespace from the command-line option, or the default namespace if the option is not set.

protected getNamespace() : string

Can be overridden by directly setting $this->namespace.

Return values
string

getOption()

Gets a single command-line option. Returns TRUE if the option exists, but doesn't have a value, and is simply acting as a flag.

protected getOption(string $name) : bool|string|null
Parameters
$name : string
Return values
bool|string|null

parseTemplate()

Performs pseudo-variables contained within view file.

protected parseTemplate(string $class[, array<int, string> $search = [] ][, array<int, string> $replace = [] ][, array<string, bool|string|null> $data = [] ]) : string
Parameters
$class : string

namespaced classname or namespaced view.

$search : array<int, string> = []
$replace : array<int, string> = []
$data : array<string, bool|string|null> = []
Return values
string

generated file content

prepare()

Prepare options and do the necessary replacements.

protected prepare(string $class) : string
Parameters
$class : string

namespaced classname or namespaced view.

Return values
string

generated file content

qualifyClassName()

Parses the class name and checks if it is already qualified.

protected qualifyClassName() : string
Return values
string

renderTemplate()

Gets the generator view as defined in the `Config\Generators::$views`, with fallback to `$template` when the defined view does not exist.

protected renderTemplate([array<string, mixed> $data = [] ]) : string
Parameters
$data : array<string, mixed> = []
Return values
string

setEnabledSuffixing()

Allows child generators to modify the internal `$enabledSuffixing` flag.

protected setEnabledSuffixing(bool $enabledSuffixing) : $this
Parameters
$enabledSuffixing : bool
Return values
$this

setHasClassName()

Allows child generators to modify the internal `$hasClassName` flag.

protected setHasClassName(bool $hasClassName) : $this
Parameters
$hasClassName : bool
Return values
$this

setSortImports()

Allows child generators to modify the internal `$sortImports` flag.

protected setSortImports(bool $sortImports) : $this
Parameters
$sortImports : bool
Return values
$this

generateFile()

Handles writing the file to disk, and all of the safety checks around that.

private generateFile(string $target, string $content) : void
Parameters
$target : string

file path

$content : string

normalizeInputClassName()

Normalize input classname.

private normalizeInputClassName() : string
Return values
string

        
On this page

Search results