\ReflectionTemplate
Introspects a SS template, working similar to PHP's {@link ReflectionClass}, providing an API for collecting all the variables and blocks.
Attempts to infer what field type each variable might be based on context
and methods that are invoked against them. Identifies any variables that may
be a custom has_one relation.
Excludes any variables that are globally available to all templates, or
in the the case of SiteTree or Email, any variables that are made available
to those templates without customisation, e.g. $Menu, $Top, $ID, etc.
This utility is very experimental and highly dependent on naming conventions,
especially that template methods are always UpperCamelCase.
<code>
$reflector = ReflectionTemplate::create();
$reflector->process(file_get_contents('/path/to/template.ss'));
foreach($reflector->getTopLevelVars() as $varName => $type) {
echo "The template variable $varName is likely a $type\n";
}
foreach($reflector->getBlocks() as $block) {
echo "There is a block named {$block->getName()}\n";
echo $block->isLoop() ? "This block is a loop\n" : "This block is a with\n";
foreach($block->getVars() as $var => $type) {
echo "The block contains a variable named $var that is likely a $type\n";
foreach($block->getChildren() as $child) {
echo "There is a child block named {$child->getName()}. It has the following vars:\n";
foreach($child->getVars() as $v => $t) {
// etc ...
}
}
}
}
</code>
- Author: Uncle Cheese <unclecheese@leftandmain.com>
Synopsis
- // members
- protected $templateAccessors;
- protected $dbfieldFunctions;
- protected $listFunctions;
- protected $code;
- protected array $blockManifest = ;
- protected ReflectionTemplate_Block $topBlock;
- // methods
- public array getTemplateAccessors()
- public array getDBFieldFunctions()
- public array getListFunctions()
- public string inferDatatype()
- public void process()
- public ReflectionTemplate_Block getBlockByID()
- public ReflectionTemplate_Block getBlockByName()
- public array getBlocks()
- public array getLoops()
- public array getWiths()
- public array getTopLevelVars()
- public array getTopLevelBlocks()
- public array getTopLevelBooleans()
- public string getCode()
Hierarchy
Extends
- Object
Tasks
Line | Task |
---|---|
109+ | This gets really ugly with some hardcoded opinions. Needs rethinking. |
Members
protected
- $blockManifest
—
array
The list of blocks in the template, indexed by their strpos - $code
—
string
The code being analysed - $dbfieldFunctions
—
array
A computed list of all the functions available to {@link DBField} classes - $listFunctions
—
array
A computed list of all the functions available to {@link SS_List} instances - $templateAccessors
—
array
A computed list of all the core template accessors, e.g. $Up, $Top - $topBlock
—
ReflectionTemplate_Block
A reference to the $Top block
Methods
public
- getBlockByID() — Gets a block by its identifier, or offset, in the template string
- getBlockByName() — Gets a block by its name, e.g. <% loop $Items %> is named "Items" Not very reliable, as there may be multiple blocks with the same name.
- getBlocks() — Gets all the blocks in the template, including those that are nested
- getCode() — Gets the code being analysed
- getDBFieldFunctions() — Gets all methods that are defined on {@link DBField} instances and caches the result
- getListFunctions() — Gets all the methods available to {@link SS_List} instances and caches the result.
- getLoops() — Gets all the blocks that are <% loop %>
- getTemplateAccessors() — Gets all the template accessors, looking at the {@link TemplateGlobalProvider} implementors and caches the result.
- getTopLevelBlocks() — Gets all the blocks at the top level
- getTopLevelBooleans() — Gets all the possible boolean variables at the top level, e.g. <% if $Foo %>
- getTopLevelVars() — Gets all the variables at the top level, mapped as $VariableName => $FieldType
- getWiths() — Gets all the blocks that are <% with %>
- inferDatatype() — Given a variable name, figure out what type it might be. This can be guided via the {@link Config} layer
- process() — Does all the heavy lifting of processing the template, getting all the vars and indexing the blocks by their strpos(), and assigning their contents to {@link ReflectionTemplate_Block} instances.