Fr3nch13/CakePHP Utilities

I18nExtractCommand extends Command
in package

Language string extractor

Table of Contents

Properties

$_countMarkerError  : int
Count number of marker errors found
$_exclude  : array<string|int, string>
An array of directories to exclude.
$_extractCore  : bool
Holds whether this call should extract the CakePHP Lib messages
$_file  : string
Current file being processed
$_files  : array<string|int, string>
Files from where to extract
$_markerError  : bool
Displays marker error(s) if true
$_merge  : bool
Merge all domain strings into the default.pot file
$_modelFactories  : array<string|int, callable|LocatorInterface>
A list of overridden model factory functions.
$_modelType  : string
The model type to use.
$_output  : string
Destination path
$_paths  : array<string|int, string>
Paths to use when looking for strings
$_storage  : array<string, mixed>
Contains all content waiting to be written
$_tableLocator  : LocatorInterface|null
Table locator instance
$_tokens  : array<string|int, mixed>
Extracted tokens
$_translations  : array<string, mixed>
Extracted strings indexed by domain.
$defaultTable  : string|null
This object's default table alias.
$modelClass  : string|null
This object's primary model class name. Should be a plural form.
$name  : string
The name of this command.

Methods

__construct()  : mixed
Constructor
abort()  : void
Halt the the current process with a StopException.
buildOptionParser()  : ConsoleOptionParser
Gets the option parser instance and configures it.
defaultName()  : string
Get the command name.
execute()  : int|null
Execute the command
executeCommand()  : int|null
Execute another command with the provided set of arguments.
fetchTable()  : Table
Convenience method to get a table instance.
getDescription()  : string
Get the command description.
getModelType()  : string
Get the model type to be used by this class
getName()  : string
Get the command name.
getOptionParser()  : ConsoleOptionParser
Get the option parser.
getRootName()  : string
Get the root command name.
getTableLocator()  : LocatorInterface
Gets the table locator.
initialize()  : void
Hook method invoked by CakePHP when a command is about to be executed.
loadModel()  : RepositoryInterface
Loads and constructs repository objects required by this object
log()  : bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
modelFactory()  : void
Override a existing callable to generate repositories of a given type.
run()  : int|null
Run the command.
setModelType()  : $this
Set the model type to be used by this class
setName()  : $this
Set the name this command uses in the collection.
setTableLocator()  : $this
Sets the table locator.
_addTranslation()  : void
Add a translation to the internal translations property
_buildFiles()  : void
Build the translate template file contents out of obtained strings
_extract()  : void
Extract text
_extractTokens()  : void
Extract tokens out of all files to be processed
_formatString()  : string
Format a string to be added as a translatable string
_getPaths()  : void
Method to interact with the user and get path selections.
_getStrings()  : array<string|int, mixed>
Get the strings from the position forward
_isExtractingApp()  : bool
Returns whether this execution is meant to extract string only from directories in folder represented by the APP constant, i.e. this task is extracting strings from same application.
_isPathUsable()  : bool
Checks whether a given path is usable for writing.
_markerError()  : void
Indicate an invalid marker on a processed file
_parse()  : void
Parse tokens
_searchFiles()  : void
Search files that may contain translatable strings
_setModelClass()  : void
Set the modelClass property based on conventions.
_store()  : void
Prepare a file to be stored
_writeFiles()  : void
Write the files that need to be stored
_writeHeader()  : string
Build the translation template header
checkUnchanged()  : bool
Check whether the old and new output are the same, thus unchanged
displayHelp()  : void
Output help content
setOutputLevel()  : void
Set the output level based on the Arguments.

Properties

$_countMarkerError

Count number of marker errors found

protected int $_countMarkerError = 0

$_exclude

An array of directories to exclude.

protected array<string|int, string> $_exclude = []

$_extractCore

Holds whether this call should extract the CakePHP Lib messages

protected bool $_extractCore = false

$_files

Files from where to extract

protected array<string|int, string> $_files = []

$_markerError

Displays marker error(s) if true

protected bool $_markerError = false

$_merge

Merge all domain strings into the default.pot file

protected bool $_merge = false

$_modelType

The model type to use.

protected string $_modelType = 'Table'

$_paths

Paths to use when looking for strings

protected array<string|int, string> $_paths = []

$_storage

Contains all content waiting to be written

protected array<string, mixed> $_storage = []

$_translations

Extracted strings indexed by domain.

protected array<string, mixed> $_translations = []

$defaultTable

This object's default table alias.

protected string|null $defaultTable = null

$modelClass

This object's primary model class name. Should be a plural form.

Use Cake\ORM\Locator\LocatorAwareTrait::$defaultTable instead.

protected string|null $modelClass

CakePHP will not inflect the name.

Example: For an object named 'Comments', the modelClass would be 'Comments'. Plugin classes should use Plugin.Comments style names to correctly load models from the correct plugin.

Use empty string to not use auto-loading on this object. Null auto-detects based on controller name.

$name

The name of this command.

protected string $name = 'cake unknown'

Methods

__construct()

Constructor

public __construct() : mixed

By default CakePHP will construct command objects when building the CommandCollection for your application.

abort()

Halt the the current process with a StopException.

public abort([int $code = self::CODE_ERROR ]) : void
Parameters
$code : int = self::CODE_ERROR

The exit code to use.

Tags
throws
StopException
psalm-return

never-return

defaultName()

Get the command name.

public static defaultName() : string
Tags
inheritDoc
Return values
string

executeCommand()

Execute another command with the provided set of arguments.

public executeCommand(CommandInterface|string $command[, array<string|int, mixed> $args = [] ][, ConsoleIo|null $io = null ]) : int|null

If you are using a string command name, that command's dependencies will not be resolved with the application container. Instead you will need to pass the command as an object with all of its dependencies.

Parameters
$command : CommandInterface|string

The command class name or command instance.

$args : array<string|int, mixed> = []

The arguments to invoke the command with.

$io : ConsoleIo|null = null

The ConsoleIo instance to use for the executed command.

Return values
int|null

The exit code or null for success of the command.

fetchTable()

Convenience method to get a table instance.

public fetchTable([string|null $alias = null ][, array<string, mixed> $options = [] ]) : Table
Parameters
$alias : string|null = null

The alias name you want to get. Should be in CamelCase format. If null then the value of $defaultTable property is used.

$options : array<string, mixed> = []

The options you want to build the table with. If a table has already been loaded the registry options will be ignored.

Tags
throws
CakeException

If $alias argument and $defaultTable property both are null.

see
TableLocator::get()
since
4.3.0
Return values
Table

getDescription()

Get the command description.

public static getDescription() : string
Return values
string

getModelType()

Get the model type to be used by this class

public getModelType() : string
Return values
string

getName()

Get the command name.

public getName() : string
Return values
string

getOptionParser()

Get the option parser.

public getOptionParser() : ConsoleOptionParser

You can override buildOptionParser() to define your options & arguments.

Tags
throws
RuntimeException

When the parser is invalid

Return values
ConsoleOptionParser

getRootName()

Get the root command name.

public getRootName() : string
Return values
string

initialize()

Hook method invoked by CakePHP when a command is about to be executed.

public initialize() : void

Override this method and implement expensive/important setup steps that should not run on every command run. This method will be called before the options and arguments are validated and processed.

loadModel()

Loads and constructs repository objects required by this object

public loadModel([string|null $modelClass = null ][, string|null $modelType = null ]) : RepositoryInterface

Use LocatorAwareTrait::fetchTable() instead.

Typically used to load ORM Table objects as required. Can also be used to load other types of repository objects your application uses.

If a repository provider does not return an object a MissingModelException will be thrown.

Parameters
$modelClass : string|null = null

Name of model class to load. Defaults to $this->modelClass. The name can be an alias like 'Post' or FQCN like App\Model\Table\PostsTable::class.

$modelType : string|null = null

The type of repository to load. Defaults to the getModelType() value.

Tags
throws
MissingModelException

If the model class cannot be found.

throws
UnexpectedValueException

If $modelClass argument is not provided and ModelAwareTrait::$modelClass property value is empty.

Return values
RepositoryInterface

The model instance created.

log()

Convenience method to write a message to Log. See Log::write() for more information on writing to logs.

public log(string $message[, string|int $level = LogLevel::ERROR ][, array<string|int, mixed>|string $context = [] ]) : bool
Parameters
$message : string

Log message.

$level : string|int = LogLevel::ERROR

Error level.

$context : array<string|int, mixed>|string = []

Additional log data relevant to this message.

Return values
bool

Success of log write.

modelFactory()

Override a existing callable to generate repositories of a given type.

public modelFactory(string $type, LocatorInterface|callable $factory) : void
Parameters
$type : string

The name of the repository type the factory function is for.

$factory : LocatorInterface|callable

The factory function used to create instances.

run()

Run the command.

public run(array<string|int, mixed> $argv, ConsoleIo $io) : int|null
Parameters
$argv : array<string|int, mixed>

Arguments from the CLI environment.

$io : ConsoleIo

The console io

Tags
inheritDoc
Return values
int|null

Exit code or null for success.

setModelType()

Set the model type to be used by this class

public setModelType(string $modelType) : $this
Parameters
$modelType : string

The model type

Return values
$this

setName()

Set the name this command uses in the collection.

public setName(string $name) : $this
Parameters
$name : string

The name the command uses in the collection.

Tags
inheritDoc
Return values
$this

_addTranslation()

Add a translation to the internal translations property

protected _addTranslation(string $domain, string $msgid[, array<string|int, mixed> $details = [] ]) : void

Takes care of duplicate translations

Parameters
$domain : string

The domain

$msgid : string

The message string

$details : array<string|int, mixed> = []

Context and plural form if any, file and line references

_buildFiles()

Build the translate template file contents out of obtained strings

protected _buildFiles(Arguments $args) : void
Parameters
$args : Arguments

Console arguments

_formatString()

Format a string to be added as a translatable string

protected _formatString(string $string) : string
Parameters
$string : string

String to format

Return values
string

Formatted string

_getStrings()

Get the strings from the position forward

protected _getStrings(int &$position, int $target) : array<string|int, mixed>
Parameters
$position : int

Actual position on tokens array

$target : int

Number of strings to extract

Return values
array<string|int, mixed>

Strings extracted

_isExtractingApp()

Returns whether this execution is meant to extract string only from directories in folder represented by the APP constant, i.e. this task is extracting strings from same application.

protected _isExtractingApp() : bool
Return values
bool

_isPathUsable()

Checks whether a given path is usable for writing.

protected _isPathUsable(string $path) : bool
Parameters
$path : string

Path to folder

Return values
bool

true if it exists and is writable, false otherwise

_markerError()

Indicate an invalid marker on a processed file

protected _markerError(ConsoleIo $io, string $file, int $line, string $marker, int $count) : void
Parameters
$io : ConsoleIo

The io instance.

$file : string

File where invalid marker resides

$line : int

Line number

$marker : string

Marker found

$count : int

Count

_parse()

Parse tokens

protected _parse(ConsoleIo $io, string $functionName, array<string|int, mixed> $map) : void
Parameters
$io : ConsoleIo

The io instance

$functionName : string

Function name that indicates translatable string (e.g: '__')

$map : array<string|int, mixed>

Array containing what variables it will find (e.g: domain, singular, plural)

_searchFiles()

Search files that may contain translatable strings

protected _searchFiles() : void

_setModelClass()

Set the modelClass property based on conventions.

protected _setModelClass(string $name) : void

If the property is already set it will not be overwritten

Parameters
$name : string

Class name.

_store()

Prepare a file to be stored

protected _store(string $domain, string $header, string $sentence) : void
Parameters
$domain : string

The domain

$header : string

The header content.

$sentence : string

The sentence to store.

_writeHeader()

Build the translation template header

protected _writeHeader(string $domain) : string
Parameters
$domain : string

Domain

Return values
string

Translation template header

checkUnchanged()

Check whether the old and new output are the same, thus unchanged

protected checkUnchanged(string $oldFile, int $headerLength, string $newFileContent) : bool

Compares the sha1 hashes of the old and new file without header.

Parameters
$oldFile : string

The existing file.

$headerLength : int

The length of the file header in bytes.

$newFileContent : string

The content of the new file.

Return values
bool

Whether the old and new file are unchanged.


        
On this page

Search results