Class Dwoo_Compiler

Description

Implements interfaces:

default dwoo compiler class, compiles dwoo templates into php

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Located in /Dwoo/Compiler.php (line 19)


	
			
Class Constant Summary
 PHP_CLOSE = "?>"
 PHP_OPEN = "<?php "
Variable Summary
Method Summary
static string implode_r ( $params, [bool $recursiveCall = false])
string addBlock (string $type,  $params, int $paramtype)
string addCustomBlock (string $type,  $params, int $paramtype)
void addPostProcessor (mixed $callback, [bool $autoload = false])
void addPreProcessor (mixed $callback, [bool $autoload = false])
string compile (Dwoo $dwoo, Dwoo_ITemplate $template, string $tpl)
&array &findBlock (string $type, [bool $closeAlong = false])
string flattenVarTree ( $tree, [bool $recursed = false])
array forceScope (mixed $scope)
bool getAutoEscape ()
array getCompiledParams ( $params)
&array &getCurrentBlock ()
array getDelimiters ()
int getLine ()
array getParamMap (callback $callback)
int getPluginType (string $name)
int getPointer ()
array getRealParams ( $params)
string getTemplateSource ([mixed $fromPointer = false])
void injectBlock (string $type,  $params)
void loadProcessor (string $class, string $name)
array mapParams ( $params, callback $callback, [int $callType = 2])
string parse (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
string parseConst (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
string parseConstKey (string $key, string $curBlock)
string parseFunction (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
string parseMethodCall (string $output, string $methodCall, string $curBlock,  &$pointer, int $pointer)
string parseOthers (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
string parseString (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
string parseVar (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
string parseVarKey (string $key, string $curBlock)
string phpTagEncodingHelper (array $match)
void push (string $content, [int $lineCount = null])
string removeBlock (string $type)
void removePostProcessor (mixed $callback)
void removePreProcessor (mixed $callback)
string removeTopBlock ()
string replaceModifiers ( $m, [string $curBlock = null], [ &$pointer = null])
string replaceStringVars (string $string, string $first, [string $curBlock = ''])
string replaceVarKeyHelper (array $match)
void setAutoEscape (bool $enabled)
void setCustomPlugins ( $customPlugins)
void setDelimiters (string $left, string $right)
void setLine (int $number, [bool $isOffset = false])
void setLooseOpeningHandling ([bool $allow = false])
void setNestedCommentsHandling ([bool $allow = true])
void setPointer (int $position, [bool $isOffset = false])
array setScope (mixed $scope, [bool $absolute = false])
void setSecurityPolicy ([Dwoo_Security_Policy $policy = null])
string setTemplateSource (string $newSource, [bool $fromPointer = false])
Variables
static Dwoo_Compiler $instance (line 211)

holds an instance of this class, used by getInstance when you don't provide a custom compiler in order to save resources

  • access: protected
bool $allowLooseOpenings = false (line 92)

defines whether opening and closing tags can contain spaces before valid data or not

turn to true if you want to be sloppy with the syntax, but when set to false it allows to skip javascript and css tags as long as they are in the form "{ something", which is nice. default is false.

  • access: protected
bool $allowNestedComments = false (line 81)

defines whether the nested comments should be parsed as nested or not

defaults to false (classic block comment parsing as in all languages)

  • access: protected
bool $autoEscape = false (line 101)

defines whether the compiler will automatically html-escape variables or not

default is false

  • access: protected
Dwoo_Block_Plugin $curBlock (line 196)

current block at the top of the block plugins stack, accessible through getCurrentBlock

array $customPlugins = array() (line 115)

stores the custom plugins registered with this compiler

  • access: protected
array $data (line 154)

stores the data within which the scope moves

  • access: protected
bool $debug = false (line 44)

boolean flag to enable or disable debugging output

  • access: public
Dwoo $dwoo (line 203)

current dwoo object that uses this compiler, or null

  • access: protected
string $ld = '{' (line 51)

left script delimiter

  • access: protected
string $ldr = '\\{' (line 58)

left script delimiter with escaped regex meta characters

  • access: protected
int $pointer (line 147)

stores the current pointer position inside the template

  • access: protected
array $processors = array('pre'=>array(),'post'=>array()) (line 122)

stores the pre- and post-processors callbacks

  • access: protected
string $rd = '}' (line 65)

right script delimiter

  • access: protected
string $rdr = '\\}' (line 72)

right script delimiter with escaped regex meta characters

  • access: protected
mixed $scope (line 163)

variable scope of the compiler, set to null if it can not be resolved to a static string (i.e. if some plugin defines a new scope based on a variable array key)

  • access: protected
array $scopeTree (line 171)

variable scope tree, that allows to rebuild the current scope if required, i.e. when going to a parent level

  • access: protected
Dwoo_Security_Policy $securityPolicy (line 108)

security policy object

  • access: protected
array $stack = array() (line 186)

block plugins stack, accessible through some methods

string $template (line 140)

stores the template undergoing compilation

  • access: protected
array $usedPlugins (line 133)

stores a list of plugins that are used in the currently compiled template, and that are not compilable. these plugins will be loaded during the template's runtime if required.

it is a 1D array formatted as key:pluginName value:pluginType

  • access: protected
Methods
static compilerFactory (line 2847)

returns a default instance of this compiler, used by default by all Dwoo templates that do not have a specific compiler assigned and when you do not override the default compiler factory function

static Dwoo_Compiler compilerFactory ()
static implode_r (line 2643)

recursively implodes an array in a similar manner as var_export() does but with some tweaks to handle pre-compiled values and the fact that we do not need to enclose everything with "array" and do not require top-level keys to be displayed

  • return: the imploded array
  • access: public
static string implode_r ( $params, [bool $recursiveCall = false])
  • array $params: the array to implode
  • bool $recursiveCall: if set to true, the function outputs key names for the top level
addBlock (line 891)

adds a block to the top of the block stack

  • return: the preProcessing() method's output
  • access: public
string addBlock (string $type,  $params, int $paramtype)
  • string $type: block type (name)
  • array $params: the parameters array
  • int $paramtype: the parameters type (see mapParams), 0, 1 or 2
addCustomBlock (line 913)

adds a custom block to the top of the block stack

  • return: the preProcessing() method's output
  • access: public
string addCustomBlock (string $type,  $params, int $paramtype)
  • string $type: block type (name)
  • array $params: the parameters array
  • int $paramtype: the parameters type (see mapParams), 0, 1 or 2
addPostProcessor (line 373)

adds a postprocessor to the compiler, it will be called before the template is compiled

  • access: public
void addPostProcessor (mixed $callback, [bool $autoload = false])
  • mixed $callback: either a valid callback to the postprocessor or a simple name if the autoload is set to true
  • bool $autoload: if set to true, the postprocessor is auto-loaded from one of the plugin directories, else you must provide a valid callback
addPreProcessor (line 324)

adds a preprocessor to the compiler, it will be called before the template is compiled

  • access: public
void addPreProcessor (mixed $callback, [bool $autoload = false])
  • mixed $callback: either a valid callback to the preprocessor or a simple name if the autoload is set to true
  • bool $autoload: if set to true, the preprocessor is auto-loaded from one of the plugin directories, else you must provide a valid callback
compile (line 576)

compiles the provided string down to php code

  • return: a compiled php string
  • access: public
string compile (Dwoo $dwoo, Dwoo_ITemplate $template, string $tpl)

Implementation of:
Dwoo_ICompiler::compile()
compiles the provided string down to php code
findBlock (line 1002)

returns a reference to the first block of the given type encountered and optionally closes all blocks until it finds it

this is mainly used by {else} plugins to close everything that was opened between their parent and themselves

  • return: the array is as such: array('type'=>pluginName, 'params'=>parameter array, 'custom'=>bool defining whether it's a custom plugin or not, for internal use)
  • access: public
&array &findBlock (string $type, [bool $closeAlong = false])
  • string $type: the block type (name)
  • bool $closeAlong: whether to close all blocks encountered while going down the block stack or not
flattenVarTree (line 2185)

flattens a variable tree, this helps in parsing very complex variables such as $var.foo[$foo.bar->baz].baz, it computes the contents of the brackets first and works out from there

  • return: flattened tree
  • access: protected
string flattenVarTree ( $tree, [bool $recursed = false])
  • array $tree: the variable tree parsed by he parseVar() method that must be flattened
  • bool $recursed: leave that to false by default, it is only for internal use
forceScope (line 878)

forces an absolute scope

  • return: the current scope tree
  • deprecated:
  • access: public
array forceScope (mixed $scope)
  • mixed $scope: a scope as a string or array
getAutoEscape (line 312)

returns the auto escape setting

default setting is disabled/false

  • access: public
bool getAutoEscape ()
getCompiledParams (line 1063)

returns the compiled parameters (for example a variable's compiled parameter will be "$this->scope['key']") out of the given parameter array

  • return: filtered parameters
  • access: public
array getCompiledParams ( $params)
  • array $params: parameter array
getCurrentBlock (line 1030)

returns a reference to the current block array

  • return: the array is as such: array('type'=>pluginName, 'params'=>parameter array, 'custom'=>bool defining whether it's a custom plugin or not, for internal use)
  • access: public
&array &getCurrentBlock ()
getDelimiters (line 235)

returns the left and right template delimiters

  • return: containing the left and the right delimiters
  • access: public
array getDelimiters ()
getDwoo (line 531)

returns the dwoo object that initiated this template compilation, only available during compilation of a template

  • access: public
Dwoo getDwoo ()
getLine (line 521)

returns the current line number, only available during compilation of a template

  • access: public
int getLine ()
getLooseOpeningHandling (line 284)

returns the tag openings handling strictness setting

bool getLooseOpeningHandling ()
getNestedCommentsHandling (line 259)

returns the nested comments handling setting

bool getNestedCommentsHandling ()
getParamMap (line 2812)

returns the parameter map of the given callback, it filters out entries typed as Dwoo and Dwoo_Compiler and turns the rest parameter into a "*"

  • return: processed parameter map
  • access: protected
array getParamMap (callback $callback)
  • callback $callback: the function/method to reflect on
getPluginType (line 2668)

returns the plugin type of a plugin and adds it to the used plugins array if required

  • return: type as a multi bit flag composed of the Dwoo plugin types constants
  • access: protected
int getPluginType (string $name)
  • string $name: plugin name, as found in the template
getPointer (line 496)

returns the current pointer position, only available during compilation of a template

  • access: public
int getPointer ()
getRealParams (line 1079)

returns the real parameters (for example a variable's real parameter will be its key, etc) out of the given parameter array

  • return: filtered parameters
  • access: public
array getRealParams ( $params)
  • array $params: parameter array
getSecurityPolicy (line 471)

returns the current security policy object or null by default

  • return: the security policy object if any
  • access: public
Dwoo_Security_Policy|null getSecurityPolicy ()
getTemplateSource (line 559)

returns the template that is being compiled

  • return: the template or partial template
  • access: public
string getTemplateSource ([mixed $fromPointer = false])
  • mixed $fromPointer: if set to true, only the source from the current pointer position is returned, if a number is given it overrides the current pointer
injectBlock (line 937)

injects a block at the top of the plugin stack without calling its preProcessing method

used by {else} blocks to re-add themselves after having closed everything up to their parent

  • access: public
void injectBlock (string $type,  $params)
  • string $type: block type (name)
  • array $params: parameters array
loadProcessor (line 421)

internal function to autoload processors at runtime if required

  • access: protected
void loadProcessor (string $class, string $name)
  • string $class: the class/function name
  • string $name: the plugin name (without Dwoo_Plugin_ prefix)
mapParams (line 2747)

maps the parameters received from the template onto the parameters required by the given callback

  • return: parameters sorted in the correct order with missing optional parameters filled
  • access: protected
array mapParams ( $params, callback $callback, [int $callType = 2])
  • array $params: the array of parameters
  • callback $callback: the function or method to reflect on to find out the required parameters
  • int $callType: the type of call in the template, 0 = no params, 1 = php-style call, 2 = named parameters call
parse (line 1100)

entry point of the parser, it redirects calls to other parse* functions

  • return: parsed values
  • access: protected
string parse (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
  • string $in: the string within which we must parse something
  • int $from: the starting offset of the parsed area
  • int $to: the ending offset of the parsed area
  • mixed $parsingParams: must be an array if we are parsing a function or modifier's parameters, or false by default
  • string $curBlock: the current parser-block being processed
  • mixed $pointer: a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  • &$pointer
parseConst (line 1758)

parses a constant

  • return: parsed values
  • access: protected
string parseConst (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
  • string $in: the string within which we must parse something
  • int $from: the starting offset of the parsed area
  • int $to: the ending offset of the parsed area
  • mixed $parsingParams: must be an array if we are parsing a function or modifier's parameters, or false by default
  • string $curBlock: the current parser-block being processed
  • mixed $pointer: a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  • &$pointer
parseConstKey (line 1793)

parses a constant

  • return: parsed constant
  • access: protected
string parseConstKey (string $key, string $curBlock)
  • string $key: the constant to parse
  • string $curBlock: the current parser-block being processed
parseFunction (line 1376)

parses a function call

  • return: parsed values
  • access: protected
string parseFunction (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
  • string $in: the string within which we must parse something
  • int $from: the starting offset of the parsed area
  • int $to: the ending offset of the parsed area
  • mixed $parsingParams: must be an array if we are parsing a function or modifier's parameters, or false by default
  • string $curBlock: the current parser-block being processed
  • mixed $pointer: a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  • &$pointer
parseMethodCall (line 2020)

parses any number of chained method calls/property reads

  • return: parsed call(s)/read(s)
  • access: protected
string parseMethodCall (string $output, string $methodCall, string $curBlock,  &$pointer, int $pointer)
  • string $output: the variable or whatever upon which the method are called
  • string $methodCall: method call source, starting at "->"
  • string $curBlock: the current parser-block being processed
  • int $pointer: a reference to a pointer that will be increased by the amount of characters parsed
  • &$pointer
parseOthers (line 2258)

parses various constants, operators or non-quoted strings

  • return: parsed values
  • access: protected
string parseOthers (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
  • string $in: the string within which we must parse something
  • int $from: the starting offset of the parsed area
  • int $to: the ending offset of the parsed area
  • mixed $parsingParams: must be an array if we are parsing a function or modifier's parameters, or false by default
  • string $curBlock: the current parser-block being processed
  • mixed $pointer: a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  • &$pointer
parseString (line 1691)

parses a string

  • return: parsed values
  • access: protected
string parseString (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
  • string $in: the string within which we must parse something
  • int $from: the starting offset of the parsed area
  • int $to: the ending offset of the parsed area
  • mixed $parsingParams: must be an array if we are parsing a function or modifier's parameters, or false by default
  • string $curBlock: the current parser-block being processed
  • mixed $pointer: a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  • &$pointer
parseVar (line 1819)

parses a variable

  • return: parsed values
  • access: protected
string parseVar (string $in, int $from, int $to, [mixed $parsingParams = false], [string $curBlock = ''], [ &$pointer = null], mixed $pointer)
  • string $in: the string within which we must parse something
  • int $from: the starting offset of the parsed area
  • int $to: the ending offset of the parsed area
  • mixed $parsingParams: must be an array if we are parsing a function or modifier's parameters, or false by default
  • string $curBlock: the current parser-block being processed
  • mixed $pointer: a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  • &$pointer
parseVarKey (line 2074)

parses a constant variable (a variable that doesn't contain another variable) and preprocesses it to save runtime processing time

  • return: parsed variable
  • access: protected
string parseVarKey (string $key, string $curBlock)
  • string $key: the variable to parse
  • string $curBlock: the current parser-block being processed
phpTagEncodingHelper (line 2734)

runs htmlentities over the matched <?php ?> blocks when the security policy enforces that

  • return: the htmlentities-converted string
  • access: protected
string phpTagEncodingHelper (array $match)
  • array $match: matched php block
push (line 799)

adds compiled content to the current block

  • access: public
void push (string $content, [int $lineCount = null])
  • string $content: the content to push
  • int $lineCount: newlines count in content, optional
removeBlock (line 954)

removes the closest-to-top block of the given type and all other blocks encountered while going down the block stack

  • return: the output of all postProcessing() method's return values of the closed blocks
  • access: public
string removeBlock (string $type)
  • string $type: block type (name)
removePostProcessor (line 398)

removes a postprocessor from the compiler

  • access: public
void removePostProcessor (mixed $callback)
  • mixed $callback: either a valid callback to the postprocessor or a simple name if it was autoloaded
removePreProcessor (line 349)

removes a preprocessor from the compiler

  • access: public
void removePreProcessor (mixed $callback)
  • mixed $callback: either a valid callback to the preprocessor or a simple name if it was autoloaded
removeTopBlock (line 1040)

removes the block at the top of the stack and calls its postProcessing() method

  • return: the postProcessing() method's output
  • access: public
string removeTopBlock ()
replaceModifiers (line 2391)

replaces the modifiers applied to a string or a variable

  • return: the input enclosed with various function calls according to the modifiers found
  • access: protected
string replaceModifiers ( $m, [string $curBlock = null], [ &$pointer = null])
  • array $m: the regex matches that must be array(1=>"double or single quotes enclosing a string, when applicable", 2=>"the string or var", 3=>"the modifiers matched")
  • string $curBlock: the current parser-block being processed
  • &$pointer
replaceStringVars (line 2347)

replaces variables within a parsed string

  • return: the original string with variables replaced
  • access: protected
string replaceStringVars (string $string, string $first, [string $curBlock = ''])
  • string $string: the parsed string
  • string $first: the first character parsed in the string, which is the string delimiter (' or ")
  • string $curBlock: the current parser-block being processed
replaceVarKeyHelper (line 2242)

helper function that parses a variable

  • return: parsed variable
  • access: protected
string replaceVarKeyHelper (array $match)
  • array $match: the matched variable, array(1=>"string match")
setAutoEscape (line 300)

changes the auto escape setting

if enabled, the compiler will automatically html-escape variables, unless they are passed through the safe function such as {$var|safe} or {safe $var}

default setting is disabled/false

  • access: public
void setAutoEscape (bool $enabled)
  • bool $enabled: set to true to enable, false to disable
setCustomPlugins (line 448)

adds the custom plugins loaded into Dwoo to the compiler so it can load them

  • see: Dwoo::addPlugin
  • access: public
void setCustomPlugins ( $customPlugins)
  • array $customPlugins: an array of custom plugins

Implementation of:
Dwoo_ICompiler::setCustomPlugins()
adds the custom plugins loaded into Dwoo to the compiler so it can load them
setDelimiters (line 222)

sets the delimiters to use in the templates

delimiters can be multi-character strings but should not be one of those as they will make it very hard to work with templates or might even break the compiler entirely : "\", "$", "|", ":" and finally "#" only if you intend to use config-vars with the #var# syntax.

  • access: public
void setDelimiters (string $left, string $right)
  • string $left: left delimiter
  • string $right: right delimiter
setLine (line 507)

sets the line number

  • access: public
void setLine (int $number, [bool $isOffset = false])
  • int $number: the new line number
  • bool $isOffset: if set to true, the position acts as an offset and not an absolute position
setLooseOpeningHandling (line 273)

sets the tag openings handling strictness, if set to true, template tags can contain spaces before the first function/string/variable such as { $foo} is valid.

if set to false (default setting), { $foo} is invalid but that is however a good thing as it allows css (i.e. #foo { color:red; }) to be parsed silently without triggering an error, same goes for javascript.

  • access: public
void setLooseOpeningHandling ([bool $allow = false])
  • bool $allow: true to allow loose handling, false to restore default setting
setNestedCommentsHandling (line 249)

sets the way to handle nested comments, if set to true {* foo {* some other *} comment *} will be stripped correctly.

if false it will remove {* foo {* some other *} and leave "comment *}" alone, this is the default behavior

  • access: public
void setNestedCommentsHandling ([bool $allow = true])
  • bool $allow: allow nested comments or not, defaults to true (but the default internal value is false)
setPointer (line 482)

sets the pointer position

  • access: public
void setPointer (int $position, [bool $isOffset = false])
  • int $position: the new pointer position
  • bool $isOffset: if set to true, the position acts as an offset and not an absolute position
setScope (line 829)

sets the scope

set to null if the scope becomes "unstable" (i.e. too variable or unknown) so that variables are compiled in a more evaluative way than just $this->scope['key']

  • return: the current scope tree
  • access: public
array setScope (mixed $scope, [bool $absolute = false])
  • mixed $scope: a string i.e. "level1.level2" or an array i.e. array("level1", "level2")
  • bool $absolute: if true, the scope is set from the top level scope and not from the current scope
setSecurityPolicy (line 461)

sets the security policy object to enforce some php security settings

use this if untrusted persons can modify templates, set it on the Dwoo object as it will be passed onto the compiler automatically

  • access: public
void setSecurityPolicy ([Dwoo_Security_Policy $policy = null])

Implementation of:
Dwoo_ICompiler::setSecurityPolicy()
sets the security policy object to enforce some php security settings
setTemplateSource (line 543)

overwrites the template that is being compiled

  • return: the template or partial template
  • access: public
string setTemplateSource (string $newSource, [bool $fromPointer = false])
  • string $newSource: the template source that must replace the current one
  • bool $fromPointer: if set to true, only the source from the current pointer position is replaced
Class Constants
PHP_CLOSE = "?>" (line 37)

constant that represents a php closing tag

use it in case it needs to be adjusted

PHP_OPEN = "<?php " (line 28)

constant that represents a php opening tag

use it in case it needs to be adjusted

Documentation generated on Wed, 24 Dec 2008 02:13:22 +0100 by phpDocumentor 1.4.0