|
eZ Publish
[4.2]
|
eZPHPCreator provides a simple interface for creating and executing PHP code. More...
Public Member Functions | |
| eZPHPCreator ($dir, $file, $prefix= '', $options=array()) | |
| thisVariableText ($value, $column=0, $iteration=0, $maxIterations=2) | |
| addDefine ($name, $value, $caseSensitive=true, $parameters=array()) | |
| addRawVariable ($name, $value) | |
| addVariable ($name, $value, $assignmentType=eZPHPCreator::VARIABLE_ASSIGNMENT, $parameters=array()) | |
| addVariableUnset ($name, $parameters=array()) | |
| addVariableUnsetList ($list, $parameters=array()) | |
| addSpace ($lines=1) | |
| addText ($text) | |
| addMethodCall ($objectName, $methodName, $methodParameters, $returnValue=false, $parameters=array()) | |
| addCodePiece ($code, $parameters=array()) | |
| addComment ($comment, $eol=true, $whitespaceHandling=true, $parameters=array()) | |
| addInclude ($file, $type=eZPHPCreator::INCLUDE_ONCE_STATEMENT, $parameters=array()) | |
Static Public Member Functions | |
| static | prependSpacing ($text, $spacing, $skipEmptyLines=true, $spacingString=" ", $splitString="\n") |
| static | variableNameText ($variableName, $assignmentType, $variableParameters=array()) |
| static | variableText ($value, $column=0, $iteration=0, $maxIterations=false) |
Public Attributes | |
| $ClusterFileScope = false | |
| $ClusteringEnabled = false | |
| $Elements | |
| $FileResource | |
| $isAtomic | |
| $PHPDir | |
| $PHPFile | |
| $requestedFilename | |
| $Spacing = true | |
| $TextChunks | |
| $tmpFilename | |
| const | CODE_PIECE = 5 |
| const | DEFINE = 9 |
| const | EOL_COMMENT = 6 |
| const | INCLUDE_ALWAYS_STATEMENT = 2 |
| const | INCLUDE_ONCE_STATEMENT = 1 |
| const | INCLUDE_STATEMENT = 7 |
| const | METHOD_CALL = 4 |
| const | METHOD_CALL_PARAMETER_VALUE = 1 |
| const | METHOD_CALL_PARAMETER_VARIABLE = 2 |
| const | RAW_VARIABLE = 11 |
| const | SPACE = 2 |
| const | TEXT = 3 |
| const | VARIABLE = 1 |
| const | VARIABLE_APPEND_ELEMENT = 3 |
| const | VARIABLE_APPEND_TEXT = 2 |
| const | VARIABLE_ASSIGNMENT = 1 |
| const | VARIABLE_UNSET = 8 |
| const | VARIABLE_UNSET_LIST = 10 |
Private Member Functions | |
| flushChunks () | |
| temporaryVariableName ($prefix) | |
| write ($text) | |
| writeChunks () | |
| writeCodePiece ($element) | |
| writeComment ($element) | |
| writeDefine ($element) | |
| writeElements () | |
| writeInclude ($element) | |
| writeMethodCall ($element) | |
| writeRawVariable ($variableName, $variableValue) | |
| writeSpace ($element) | |
| writeText ($element) | |
| writeVariable ($variableName, $variableValue, $assignmentType=eZPHPCreator::VARIABLE_ASSIGNMENT, $variableParameters=array()) | |
| writeVariableUnset ($element) | |
| writeVariableUnsetList ($element) | |
| open ($atomic=false) | |
| close () | |
| exists () | |
| canRestore ($timestamp=false) | |
| restore ($variableDefinitions) | |
| store ($atomic=false) | |
| fetch ($addPHPMarkers=true) | |
| _restoreCall ($path, $mtime, $variableDefinitions) | |
eZPHPCreator provides a simple interface for creating and executing PHP code.
To create PHP code you must create an instance of this class, add any number of elements you choose with addDefine(), addVariable(), addVariableUnset(), addVariableUnsetList(), addSpace(), addText(), addMethodCall(), addCodePiece(), addComment() and addInclude(). After that you call store() to write all changes to disk.
$php = new eZPHPCreator( 'cache', 'code.php' ); $php->addComment( 'Auto generated' ); $php->addInclude( 'inc.php' ); $php->addVariable( 'count', 10 ); $php->store();
To restore PHP code you must create an instance of this class, check if you can restore it with canRestore() then restore variables with restore(). The class will include PHP file and run all code, once the file is done it will catch any variables you require and return it to you.
$php = new eZPHPCreator( 'cache', 'code.php' ); if ( $php->canRestore() ) { $variables = $php->restore( array( 'max_count' => 'count' ) ); print( "Max count was " . $variables['max_count'] ); } $php->close();
Definition at line 75 of file ezphpcreator.php.
| eZPHPCreator::_restoreCall | ( | $ | path, |
| $ | mtime, | ||
| $ | variableDefinitions | ||
| ) | [private] |
Processes the PHP file and returns the specified data.
Definition at line 926 of file ezphpcreator.php.
Referenced by restore().
| eZPHPCreator::addCodePiece | ( | $ | code, |
| $ | parameters = array() |
||
| ) |
Adds custom PHP code to the file, you should only use this a last resort if any of the other add functions done give you the required result.
| $code | Contains the code as text, the text will not be modified (except for spacing). This means that each expression must be ended with a newline even if it's just one. |
| $parameters | Optional parameters, can be any of the following:
|
Example:
$php->addCodePiece( "if ( \$value > 2 )\n{\n \$value = 2;\n}\n" );
Would result in the PHP code.
if ( $value > 2 )
{
$value = 2;
}
Definition at line 378 of file ezphpcreator.php.
| eZPHPCreator::addComment | ( | $ | comment, |
| $ | eol = true, |
||
| $ | whitespaceHandling = true, |
||
| $ | parameters = array() |
||
| ) |
Adds a comment to the code, the comment will be display using multiple end-of-line comments (//), one for each newline in the text $comment.
| $eol | Whether to add a newline at the last comment line |
| $whitespaceHandling | Whether to remove trailing whitespace from each line |
| $parameters | Optional parameters, can be any of the following:
|
Example:
$php->addComment( "This file is auto generated\nDo not edit!" );
Would result in the PHP code.
// This file is auto generated // Do not edit!
Definition at line 408 of file ezphpcreator.php.
| eZPHPCreator::addDefine | ( | $ | name, |
| $ | value, | ||
| $ | caseSensitive = true, |
||
| $ | parameters = array() |
||
| ) |
Adds a new define statement to the code with the name $name and value $value. The parameter $caseSensitive determines if the define should be made case sensitive or not.
Example:
$php->addDefine( 'MY_CONSTANT', 5 );
Would result in the PHP code.
define( 'MY_CONSTANT', 5 );
| $parameters | Optional parameters, can be any of the following:
|
Definition at line 156 of file ezphpcreator.php.
| eZPHPCreator::addInclude | ( | $ | file, |
| $ | type = eZPHPCreator::INCLUDE_ONCE_STATEMENT, |
||
| $ | parameters = array() |
||
| ) |
Adds an include statement to the code, the file to include is $file.
| $type | What type of include statement to use, can be one of the following:
|
| $parameters | Optional parameters, can be any of the following:
|
Example:
$php->addInclude( 'lib/ezutils/classes/ezphpcreator.php' );
Would result in the PHP code.
//include_once( 'lib/ezutils/classes/ezphpcreator.php' );
Definition at line 439 of file ezphpcreator.php.
| eZPHPCreator::addMethodCall | ( | $ | objectName, |
| $ | methodName, | ||
| $ | methodParameters, | ||
| $ | returnValue = false, |
||
| $ | parameters = array() |
||
| ) |
Adds code to call the method $methodName on the object named $objectName, $methodParameters should be an array with parameter entries where each entry contains:
Optionally the $returnValue parameter can be used to decide what should be done with the return value of the method call. It can either be false which means to do nothing or an array with the following entries.
| $parameters | Optional parameters, can be any of the following:
|
Example:
$php->addMethodCall( 'node', 'name', array(), array( 'name' ) ); $php->addMethodCall( 'php', 'addMethodCall', array( array( 'node' ), array( 'name' ) ) );
Would result in the PHP code.
$name = $node->name(); $php->addMethodCall( 'node', 'name' );
Definition at line 343 of file ezphpcreator.php.
| eZPHPCreator::addRawVariable | ( | $ | name, |
| $ | value | ||
| ) |
Adds a new raw variable tothe code with the name $name and value $value.
Example:
$php->addVariable( 'TransLationRoot', $cache['root'] );
This function makes use of PHP's var_export() function which is optimized for this task.
Definition at line 177 of file ezphpcreator.php.
| eZPHPCreator::addSpace | ( | $ | lines = 1 | ) |
Adds some space to the code in form of newlines. The number of lines is controlled with $lines which is 1 by default. You can use this to get more readable PHP code.
Example:
$php->addSpace( 1 );
Definition at line 288 of file ezphpcreator.php.
| eZPHPCreator::addText | ( | $ | text | ) |
Adds some plain text to the code. The text will be placed outside of PHP start and end markers and will in principle work as printing the text.
Example:
$php->addText( 'Print me!' );
Definition at line 305 of file ezphpcreator.php.
| eZPHPCreator::addVariable | ( | $ | name, |
| $ | value, | ||
| $ | assignmentType = eZPHPCreator::VARIABLE_ASSIGNMENT, |
||
| $ | parameters = array() |
||
| ) |
Adds a new variable to the code with the name $name and value $value.
Example:
$php->addVariable( 'offset', 5 ); $php->addVariable( 'text', 'some more text', eZPHPCreator::VARIABLE_APPEND_TEXT ); $php->addVariable( 'array', 42, eZPHPCreator::VARIABLE_APPEND_ELEMENT );
Would result in the PHP code.
| $assignmentType | Controls the way the value is assigned, choose one of the following:
|
| $parameters | Optional parameters, can be any of the following:
|
Definition at line 211 of file ezphpcreator.php.
| eZPHPCreator::addVariableUnset | ( | $ | name, |
| $ | parameters = array() |
||
| ) |
Adds code to unset a variable with the name $name.
Example:
$php->addVariableUnset( 'offset' );
Would result in the PHP code.
unset( $offset );
| $parameters | Optional parameters, can be any of the following:
|
Definition at line 241 of file ezphpcreator.php.
| eZPHPCreator::addVariableUnsetList | ( | $ | list, |
| $ | parameters = array() |
||
| ) |
Adds code to unset a list of variables with name from $list.
Example:
$php->addVariableUnsetList( array ( 'var1', 'var2' ) );
Would result in the PHP code.
unset( $var1, $var2 );
| $parameters | Optional parameters, can be any of the following:
|
Definition at line 269 of file ezphpcreator.php.
| eZPHPCreator::canRestore | ( | $ | timestamp = false | ) |
true if file exists and can be restored. | $timestamp | The timestamp to check the modification time of the file against, if the modification time is larger or equal to $timestamp the file can be restored. Otherwise the file is considered too old. |
Definition at line 859 of file ezphpcreator.php.
| eZPHPCreator::close | ( | ) |
Closes the currently open file if any.
Definition at line 815 of file ezphpcreator.php.
Referenced by store().
| eZPHPCreator::exists | ( | ) |
true if the file and path already exists. Definition at line 839 of file ezphpcreator.php.
| eZPHPCreator::eZPHPCreator | ( | $ | dir, |
| $ | file, | ||
| $ | prefix = '', |
||
| $ | options = array() |
||
| ) |
Initializes the creator with the directory path $dir and filename $file.
Definition at line 102 of file ezphpcreator.php.
| eZPHPCreator::fetch | ( | $ | addPHPMarkers = true | ) |
Creates a text string out of all elements and returns it.
Definition at line 998 of file ezphpcreator.php.
| eZPHPCreator::flushChunks | ( | ) | [private] |
Definition at line 1049 of file ezphpcreator.php.
| eZPHPCreator::open | ( | $ | atomic = false | ) |
Opens the file for writing and sets correct file permissions.
false if it failed to open the file. Definition at line 777 of file ezphpcreator.php.
Referenced by store().
| static eZPHPCreator::prependSpacing | ( | $ | text, |
| $ | spacing, | ||
| $ | skipEmptyLines = true, |
||
| $ | spacingString = " ", |
||
| $ | splitString = "\n" |
||
| ) | [static] |
Splits $text into multiple lines using $splitString for splitting. For each line it will prepend the string $spacingString n times as specified by $spacing.
It will try to be smart and not do anything when $spacing is set to 0.
| $skipEmptyLines | If true it will not prepend the string for empty lines. |
| $spacing | Must be a positive number, 0 means to not prepend anything. |
Definition at line 752 of file ezphpcreator.php.
Referenced by writeCodePiece(), writeMethodCall(), writeVariable(), writeVariableUnset(), and writeVariableUnsetList().
| eZPHPCreator::restore | ( | $ | variableDefinitions | ) |
Tries to restore the PHP file and fetch the defined variables in $variableDefinitions. This basically means including the file using include().
| $variableDefinitions | Associative array with the return variable name being the key matched variable as value. |
Example:
$values = $php->restore( array( 'MyValue' => 'node' ) ); print( $values['MyValue'] );
Definition at line 902 of file ezphpcreator.php.
| eZPHPCreator::store | ( | $ | atomic = false | ) |
Stores the PHP cache, returns false if the cache file could not be created.
Definition at line 962 of file ezphpcreator.php.
| eZPHPCreator::temporaryVariableName | ( | $ | prefix | ) | [private] |
Definition at line 1345 of file ezphpcreator.php.
Referenced by thisVariableText().
| eZPHPCreator::thisVariableText | ( | $ | value, |
| $ | column = 0, |
||
| $ | iteration = 0, |
||
| $ | maxIterations = 2 |
||
| ) |
Creates a text representation of the value $value which can be placed in files and be read back by a PHP parser as it was. The type of the values determines the output, it can be one of the following.
true or false null serializeData implemented.| $column | Determines the starting column in which the text will be placed. This is used for expanding arrays and objects which can span multiple lines. |
| $iteration | The current iteration, starts at 0 and increases with 1 for each recursive call |
| $maxIterations | The maximum number of iterations to allow, if the iteration exceeds this the array or object will be split into multiple variables. Can be set to false to the array or object as-is. |
false Definition at line 499 of file ezphpcreator.php.
Referenced by writeDefine(), writeInclude(), writeMethodCall(), and writeVariable().
| static eZPHPCreator::variableNameText | ( | $ | variableName, |
| $ | assignmentType, | ||
| $ | variableParameters = array() |
||
| ) | [static] |
Creates a variable statement with an assignment type and returns it.
| $variableName | The name of the variable |
| $assignmentType | What kind of assignment to use, is one of the following;
|
| $variableParameters | Optional parameters for the statement |
Definition at line 460 of file ezphpcreator.php.
Referenced by writeMethodCall(), and writeVariable().
| static eZPHPCreator::variableText | ( | $ | value, |
| $ | column = 0, |
||
| $ | iteration = 0, |
||
| $ | maxIterations = false |
||
| ) | [static] |
Definition at line 632 of file ezphpcreator.php.
Referenced by eZTemplateArrayOperator\arrayTrans(), eZTemplateLogicOperator\chooseTransformation(), eZTemplateArrayOperator\compareTrans(), eZTemplateControlOperator\condTransform(), eZTemplateArrayOperator\extractTrans(), eZTemplateExecuteOperator\fetchTransform(), eZTemplateLogicOperator\logicalComparisonTransformation(), eZTemplateArrayOperator\mergeTrans(), eZTemplateUnitOperator\operatorTransform(), eZKernelOperator\preferencesTransformation(), eZObjectForwarder\resourceAcquisitionTransformation(), eZTemplateSwitchFunction\templateNodeCaseTransformation(), eZTemplateDesignResource\templateNodeTransformation(), eZObjectForwarder\templateNodeTransformation(), eZTemplateCacheFunction\templateNodeTransformation(), eZTemplateBlockFunction\templateNodeTransformation(), and eZTemplateSectionFunction\templateNodeTransformation().
| eZPHPCreator::write | ( | $ | text | ) | [private] |
Definition at line 1057 of file ezphpcreator.php.
Referenced by fetch(), store(), writeCodePiece(), writeComment(), writeDefine(), writeInclude(), writeMethodCall(), writeRawVariable(), writeSpace(), writeText(), writeVariable(), writeVariableUnset(), and writeVariableUnsetList().
| eZPHPCreator::writeChunks | ( | ) | [private] |
Definition at line 1018 of file ezphpcreator.php.
Referenced by store().
| eZPHPCreator::writeCodePiece | ( | $ | element | ) | [private] |
Definition at line 1208 of file ezphpcreator.php.
Referenced by writeElements().
| eZPHPCreator::writeComment | ( | $ | element | ) | [private] |
Definition at line 1169 of file ezphpcreator.php.
Referenced by writeElements().
| eZPHPCreator::writeDefine | ( | $ | element | ) | [private] |
Definition at line 1119 of file ezphpcreator.php.
Referenced by writeElements().
| eZPHPCreator::writeElements | ( | ) | [private] |
Definition at line 1065 of file ezphpcreator.php.
| eZPHPCreator::writeInclude | ( | $ | element | ) | [private] |
Definition at line 1145 of file ezphpcreator.php.
Referenced by writeElements().
| eZPHPCreator::writeMethodCall | ( | $ | element | ) | [private] |
Definition at line 1233 of file ezphpcreator.php.
Referenced by writeElements().
| eZPHPCreator::writeRawVariable | ( | $ | variableName, |
| $ | variableValue | ||
| ) | [private] |
Definition at line 1316 of file ezphpcreator.php.
Referenced by writeElements().
| eZPHPCreator::writeSpace | ( | $ | element | ) | [private] |
Definition at line 1199 of file ezphpcreator.php.
Referenced by writeElements().
| eZPHPCreator::writeText | ( | $ | element | ) | [private] |
Definition at line 1222 of file ezphpcreator.php.
Referenced by writeElements().
| eZPHPCreator::writeVariable | ( | $ | variableName, |
| $ | variableValue, | ||
| $ | assignmentType = eZPHPCreator::VARIABLE_ASSIGNMENT, |
||
| $ | variableParameters = array() |
||
| ) | [private] |
Definition at line 1324 of file ezphpcreator.php.
Referenced by thisVariableText(), and writeElements().
| eZPHPCreator::writeVariableUnset | ( | $ | element | ) | [private] |
Definition at line 1279 of file ezphpcreator.php.
Referenced by writeElements().
| eZPHPCreator::writeVariableUnsetList | ( | $ | element | ) | [private] |
Definition at line 1294 of file ezphpcreator.php.
Referenced by writeElements().
| eZPHPCreator::$ClusterFileScope = false |
Definition at line 1363 of file ezphpcreator.php.
| eZPHPCreator::$ClusteringEnabled = false |
Definition at line 1362 of file ezphpcreator.php.
| eZPHPCreator::$Elements |
Definition at line 1356 of file ezphpcreator.php.
| eZPHPCreator::$FileResource |
Definition at line 1355 of file ezphpcreator.php.
| eZPHPCreator::$isAtomic |
Definition at line 1358 of file ezphpcreator.php.
| eZPHPCreator::$PHPDir |
Definition at line 1353 of file ezphpcreator.php.
| eZPHPCreator::$PHPFile |
Definition at line 1354 of file ezphpcreator.php.
| eZPHPCreator::$requestedFilename |
Definition at line 1360 of file ezphpcreator.php.
| eZPHPCreator::$Spacing = true |
Definition at line 1361 of file ezphpcreator.php.
| eZPHPCreator::$TextChunks |
Definition at line 1357 of file ezphpcreator.php.
| eZPHPCreator::$tmpFilename |
Definition at line 1359 of file ezphpcreator.php.
| const eZPHPCreator::CODE_PIECE = 5 |
Definition at line 81 of file ezphpcreator.php.
Referenced by addCodePiece(), and writeElements().
| const eZPHPCreator::DEFINE = 9 |
Definition at line 85 of file ezphpcreator.php.
Referenced by addDefine(), and writeElements().
| const eZPHPCreator::EOL_COMMENT = 6 |
Definition at line 82 of file ezphpcreator.php.
Referenced by addComment(), and writeElements().
| const eZPHPCreator::INCLUDE_ALWAYS_STATEMENT = 2 |
Definition at line 94 of file ezphpcreator.php.
Referenced by writeInclude().
| const eZPHPCreator::INCLUDE_ONCE_STATEMENT = 1 |
Definition at line 93 of file ezphpcreator.php.
Referenced by eZTemplateCompiler\compileTemplate(), and writeInclude().
| const eZPHPCreator::INCLUDE_STATEMENT = 7 |
Definition at line 83 of file ezphpcreator.php.
Referenced by addInclude(), and writeElements().
| const eZPHPCreator::METHOD_CALL = 4 |
Definition at line 80 of file ezphpcreator.php.
Referenced by addMethodCall(), and writeElements().
Definition at line 96 of file ezphpcreator.php.
Referenced by writeMethodCall().
Definition at line 97 of file ezphpcreator.php.
Referenced by writeMethodCall().
| const eZPHPCreator::RAW_VARIABLE = 11 |
Definition at line 87 of file ezphpcreator.php.
Referenced by addRawVariable(), and writeElements().
| const eZPHPCreator::SPACE = 2 |
Definition at line 78 of file ezphpcreator.php.
Referenced by addSpace(), and writeElements().
| const eZPHPCreator::TEXT = 3 |
Definition at line 79 of file ezphpcreator.php.
Referenced by addText(), and writeElements().
| const eZPHPCreator::VARIABLE = 1 |
Definition at line 77 of file ezphpcreator.php.
Referenced by addVariable(), and writeElements().
| const eZPHPCreator::VARIABLE_APPEND_ELEMENT = 3 |
Definition at line 91 of file ezphpcreator.php.
Referenced by variableNameText().
| const eZPHPCreator::VARIABLE_APPEND_TEXT = 2 |
Definition at line 90 of file ezphpcreator.php.
Referenced by variableNameText().
| const eZPHPCreator::VARIABLE_ASSIGNMENT = 1 |
Definition at line 89 of file ezphpcreator.php.
Referenced by eZTemplateCompiler\compileTemplate(), eZTemplateCompiler\createCommonCompileTemplate(), eZTemplateCompiler\generatePHPCodeChildren(), eZPackage\storeCache(), variableNameText(), and writeMethodCall().
| const eZPHPCreator::VARIABLE_UNSET = 8 |
Definition at line 84 of file ezphpcreator.php.
Referenced by addVariableUnset(), and writeElements().
| const eZPHPCreator::VARIABLE_UNSET_LIST = 10 |
Definition at line 86 of file ezphpcreator.php.
Referenced by addVariableUnsetList(), and writeElements().