Template
in package
Analyzer class for Twig templates
Table of Contents
Properties
- $for_attributes : array<string|int, mixed>
- $blockPrefix : string
- $defaults : array<string|int, mixed>
- $fetchDictionaryNamesRegex1 : string
- $fetchDictionaryNamesRegex2 : string
- $fetchDictionaryNamesRegex3 : string
- $fetchDictionaryNamesRegex4 : string
- $fors : array<string|int, mixed>
- $fullTemplate : string
- $ignoreSet : bool
- $include_tpl : string
- $loader : array<string|int, mixed>
- $localTemplate : string
- $twig : Environment
- $twigReservedVars : array<string|int, mixed>
- $variables : array<string|int, mixed>
Methods
- __construct() : mixed
- create the Twig instance for our current template
- checkDefaultValue() : bool
- check if given variable has a default value
- checkIgnoringSetVariables() : bool
- check if ignoring set variables is activ
- defaultValue() : mixed
- fetch default value of variable
- getBlocks() : array<string|int, mixed>
- function to get a list of all block names within this Object
- getVariables() : array<string|int, mixed>
- function to get names of all defined variables
- ignoreSetVariables() : void
- function to ignore set variables
- checkForDictionaryCall() : bool
- check if a variable is used as dictionary within the given template by `{{ var[ xxx ] }}` syntax
- extractBlocks() : array<string|int, mixed>
- function to analyse the template and extract blocks to separate templates
- extractSubTemplate() : string
- function to extract a sub template – like blocks and for loops
- fetchDictionaryNames() : void
- fetch dictionary names from template
- flattenVariables() : array<string|int, mixed>
- create representation of variable names
- getBlockEnds() : array<string|int, mixed>
- function to fetch all Twig Block endings
- getBlockStarts() : array<string|int, mixed>
- function to fetch all Twig Block startings
- getForEnds() : array<string|int, mixed>
- function to fetch all Twig For loop endings
- getForStarts() : array<string|int, mixed>
- function to fetch all Twig For loop startings
- treeFromFlattened() : array<string|int, mixed>
- function to build up tree structure of array from path
- visitGetAttrExpression() : array<string|int, mixed>
- Visit Object|Array
- walkThrough() : array<string|int, mixed>
- helper function to walk throuh the Twig template
- addPreAndPostToMarks() : void
- add pre and post content to marks
- analyse() : array<string|int, mixed>
- function that analyzes all defined templates for variables
- analyseForVariables() : void
- function to finally get and map the variable names from for loops
- analyzeTemplate() : array<string|int, mixed>
- function to start analyzing the template
- appendForVariables() : void
- function that is used by `getVariables` function to append variable trees from fors
- basicVarFetch() : array<string|int, mixed>
- basic variable fetch
- checkDefaults() : void
- check for default values of variables
- deepFetchVars() : void
- deeply fetch variables from template(s)
- findNestedFors() : void
- find nested fors and their children fors
- getNestedFors() : all
- get nested for loops from full template
- orderMarks() : array<string|int, mixed>
- function to sort and group found marks
- removeSetVarsIfSet() : void
- if option is activated remove variables that are set from within the template
Properties
$for_attributes
protected
static array<string|int, mixed>
$for_attributes
= ['key' => [2], 'element' => [3], 'from' => [6], 'to' => [7], 'array' => [8], 'filter' => [10]]
key-group-mapping for "for" loop regex – see function getForStarts
$blockPrefix
private
static string
$blockPrefix
= 'block.inc_'
prefix for block names
$defaults
private
array<string|int, mixed>
$defaults
list of default values
$fetchDictionaryNamesRegex1
private
static string
$fetchDictionaryNamesRegex1
= REGEX_DELIMITER . '\\{\\{\\s*'
part one of RegEx
$fetchDictionaryNamesRegex2
private
static string
$fetchDictionaryNamesRegex2
= '\\.([^\\s]+)(\\s*\\[.+\\])?\\s*\\}\\}' . REGEX_DELIMITER . 'im'
last part of RegEx
$fetchDictionaryNamesRegex3
private
static string
$fetchDictionaryNamesRegex3
= REGEX_DELIMITER . '\\{\\{\\s*([^\\.]+\\.)?'
part one of RegEx
$fetchDictionaryNamesRegex4
private
static string
$fetchDictionaryNamesRegex4
= '\\s*\\[\\s*([^\\s]+)\\s*\\]\\s*\\}\\}' . REGEX_DELIMITER . 'im'
last part of RegEx
$fors
private
array<string|int, mixed>
$fors
list of fors with children
$fullTemplate
private
string
$fullTemplate
full template without extracted sub strings (blocks)
$ignoreSet
private
bool
$ignoreSet
= false
status if set variables should be ignored
$include_tpl
private
static string
$include_tpl
= '*||%s||*'
default format string for template include
$loader
private
array<string|int, mixed>
$loader
list of templates by names
$localTemplate
private
string
$localTemplate
= 'localTemplate'
name of local main template
$twig
private
Environment
$twig
Twig Environment
$twigReservedVars
private
static array<string|int, mixed>
$twigReservedVars
= ['loop.*', '_key']
list of Twig reseverd variables
$variables
private
array<string|int, mixed>
$variables
tree of variables
Methods
__construct()
create the Twig instance for our current template
public
__construct(string $template_string) : mixed
Parameters
- $template_string : string
-
template string to be translated
checkDefaultValue()
check if given variable has a default value
public
checkDefaultValue(string $var_name) : bool
Parameters
- $var_name : string
-
variable name
Tags
Return values
bool —true if variable has a default value, false if not
checkIgnoringSetVariables()
check if ignoring set variables is activ
public
checkIgnoringSetVariables() : bool
Return values
booldefaultValue()
fetch default value of variable
public
defaultValue(string $var_name[, bool $lookup = true ]) : mixed
Parameters
- $var_name : string
-
name of variable
- $lookup : bool = true
-
defaults to true – false if only directly defined default values should be returned
Tags
Return values
mixed —default value of variable defined within the template
getBlocks()
function to get a list of all block names within this Object
public
getBlocks() : array<string|int, mixed>
Return values
array<string|int, mixed> —list of block names
getVariables()
function to get names of all defined variables
public
getVariables() : array<string|int, mixed>
Return values
array<string|int, mixed> —list of variables
ignoreSetVariables()
function to ignore set variables
public
ignoreSetVariables() : void
checkForDictionaryCall()
check if a variable is used as dictionary within the given template by `{{ var[ xxx ] }}` syntax
protected
static checkForDictionaryCall(string $var, string $tpl) : bool
Parameters
- $var : string
-
variable name to fetch subs from
- $tpl : string
-
template string to work with
Return values
bool —value if variable is used as dictionary
extractBlocks()
function to analyse the template and extract blocks to separate templates
protected
static extractBlocks(string &$template) : array<string|int, mixed>
Parameters
- $template : string
-
the template to be analyzed and the blocks should be striped out of
Return values
array<string|int, mixed> —the list of sub-templates
extractSubTemplate()
function to extract a sub template – like blocks and for loops
protected
static extractSubTemplate(string $key, array<string|int, mixed> &$all_marks, array<string|int, mixed> &$all_offsets, array<string|int, mixed> &$templates[, array<string|int, mixed>|null $pre = null ][, string $include_tpl = null ][, mixed $name_key = null ][, array<string|int, mixed> &$persisted_keys = [] ][, array<string|int, mixed> $persist_start_keys = [] ][, array<string|int, mixed> $persist_end_keys = [] ]) : string
Parameters
- $key : string
-
current offset to start with
- $all_marks : array<string|int, mixed>
-
list of all existing block marks (starts and ends)
- $all_offsets : array<string|int, mixed>
-
list of all existing offsets
- $templates : array<string|int, mixed>
-
list of templates defined
- $pre : array<string|int, mixed>|null = null
-
null by default, string if something should be prepended to inner content of sub template
- $include_tpl : string = null
-
format string of how to replace the template within template
- $name_key : mixed = null
- $persisted_keys : array<string|int, mixed> = []
-
the result-array of persisted keys from marks, identified by same identifier as sub template
- $persist_start_keys : array<string|int, mixed> = []
-
keys of start marks to be persisted into result
- $persist_end_keys : array<string|int, mixed> = []
-
keys of end marks to be persisted into result
Return values
string —outer template containing replace string for inner template
fetchDictionaryNames()
fetch dictionary names from template
protected
static fetchDictionaryNames(string $var, string $tpl, array<string|int, mixed> &$subs) : void
Parameters
- $var : string
-
variable name to fetch subs from
- $tpl : string
-
template string to work with
- $subs : array<string|int, mixed>
-
sub keys of variable $var
flattenVariables()
create representation of variable names
protected
static flattenVariables(mixed $variables, array<string|int, mixed> $tpls[, string $glue = '.' ]) : array<string|int, mixed>
Parameters
- $variables : mixed
-
variable array to work with
- $tpls : array<string|int, mixed>
-
list of templates used – should be $this->loader in most calls
- $glue : string = '.'
-
glue for flat path representation
Return values
array<string|int, mixed> —dot representation of variables
getBlockEnds()
function to fetch all Twig Block endings
protected
static getBlockEnds(string $template) : array<string|int, mixed>
Parameters
- $template : string
-
the template string
Return values
array<string|int, mixed> —see \macwinnie\RegexFunctions\getRegexOccurences
getBlockStarts()
function to fetch all Twig Block startings
protected
static getBlockStarts(string $template) : array<string|int, mixed>
Parameters
- $template : string
-
the template string
Return values
array<string|int, mixed> —see \macwinnie\RegexFunctions\getRegexOccurences
– with name
attribute
getForEnds()
function to fetch all Twig For loop endings
protected
static getForEnds(string $template) : array<string|int, mixed>
Parameters
- $template : string
-
the template string
Return values
array<string|int, mixed> —see \macwinnie\RegexFunctions\getRegexOccurences
getForStarts()
function to fetch all Twig For loop startings
protected
static getForStarts(string $template) : array<string|int, mixed>
recognized are these cases:
-
{% for element in array %}
-
{% for key, element in array %}
-
{% for (key, )element in array | filter %}
-
{% for element in from..to %}
Parameters
- $template : string
-
the template string
Tags
Return values
array<string|int, mixed> —see \macwinnie\RegexFunctions\getRegexOccurences
– with different attributes
treeFromFlattened()
function to build up tree structure of array from path
protected
static treeFromFlattened(string $path[, string $separator = '.' ]) : array<string|int, mixed>
Parameters
- $path : string
-
path to build representation from
- $separator : string = '.'
-
separator within path representation
Return values
array<string|int, mixed> —tree array
visitGetAttrExpression()
Visit Object|Array
protected
visitGetAttrExpression(Node $ast[, string|null $subKey = null ][, array<string|int, mixed> $for = [] ]) : array<string|int, mixed>
Parameters
- $ast : Node
- $subKey : string|null = null
-
sub object|array key
- $for : array<string|int, mixed> = []
Return values
array<string|int, mixed>walkThrough()
helper function to walk throuh the Twig template
protected
walkThrough(Node $ast[, array<string|int, mixed> $for = [] ]) : array<string|int, mixed>
Parameters
- $ast : Node
-
Ast object from template
- $for : array<string|int, mixed> = []
-
for array
Return values
array<string|int, mixed> —list of variables
addPreAndPostToMarks()
add pre and post content to marks
private
static addPreAndPostToMarks(array<string|int, mixed> $all_offsets, array<string|int, mixed> &$all_marks, string $template) : void
Parameters
- $all_offsets : array<string|int, mixed>
-
list of offsets – the currently viewed mark
- $all_marks : array<string|int, mixed>
-
all marks by their offsets
- $template : string
-
template string to work on
analyse()
function that analyzes all defined templates for variables
private
analyse(string $template_name) : array<string|int, mixed>
Parameters
- $template_name : string
-
name of template to analyze
Return values
array<string|int, mixed> —list of variable names
analyseForVariables()
function to finally get and map the variable names from for loops
private
static analyseForVariables(array<string|int, mixed> &$nested, array<string|int, mixed> $tpls) : void
Parameters
- $nested : array<string|int, mixed>
-
list of fors to work on
- $tpls : array<string|int, mixed>
-
list of templates used – should be $this->loader in most calls
analyzeTemplate()
function to start analyzing the template
private
analyzeTemplate(string $template_string) : array<string|int, mixed>
Parameters
- $template_string : string
-
template to be analyzed
Return values
array<string|int, mixed> —list of template and subtemplates / blocks
appendForVariables()
function that is used by `getVariables` function to append variable trees from fors
private
appendForVariables([array<string|int, mixed> &$vars = [] ]) : void
Parameters
- $vars : array<string|int, mixed> = []
-
existing list of given variables
basicVarFetch()
basic variable fetch
private
basicVarFetch() : array<string|int, mixed>
Return values
array<string|int, mixed> —list of main variable names
checkDefaults()
check for default values of variables
private
checkDefaults() : void
deepFetchVars()
deeply fetch variables from template(s)
private
deepFetchVars(array<string|int, mixed> &$vars) : void
Parameters
- $vars : array<string|int, mixed>
-
array of variables
findNestedFors()
find nested fors and their children fors
private
static findNestedFors(string $tpl, array<string|int, mixed> &$fors, array<string|int, mixed> &$nested, mixed $for_persisted) : void
Parameters
- $tpl : string
-
current search template
- $fors : array<string|int, mixed>
-
fors to be reduced / spread
- $nested : array<string|int, mixed>
-
result array
- $for_persisted : mixed
getNestedFors()
get nested for loops from full template
private
getNestedFors() : all
Return values
all —for loops of the main template
orderMarks()
function to sort and group found marks
private
static orderMarks(array<string|int, mixed> $start_marks, array<string|int, mixed> $end_marks) : array<string|int, mixed>
Parameters
- $start_marks : array<string|int, mixed>
-
result of getRegexOccurences
- $end_marks : array<string|int, mixed>
-
result of getRegexOccurences
Return values
array<string|int, mixed> —two arrays as array:
- first: a list of all offsets
- second: all marks
removeSetVarsIfSet()
if option is activated remove variables that are set from within the template
private
removeSetVarsIfSet(array<string|int, mixed> &$vars) : void
Parameters
- $vars : array<string|int, mixed>
-
list of main variable names