eZPHPCreator Class Reference
[Utility classes]

eZPHPCreator provides a simple interface for creating and executing PHP code. More...

List of all members.

Public Member Functions

 eZPHPCreator ($dir, $file, $prefix= '', $options=array())
 variableText ($value, $column=0, $iteration=0, $maxIterations=2)

 addCodePiece ($code, $parameters=array())
 addComment ($comment, $eol=true, $whitespaceHandling=true, $parameters=array())
 addDefine ($name, $value, $caseSensitive=true, $parameters=array())
 addInclude ($file, $type=EZ_PHPCREATOR_INCLUDE_ONCE, $parameters=array())
 addMethodCall ($objectName, $methodName, $methodParameters, $returnValue=false, $parameters=array())
 addRawVariable ($name, $value)
 addSpace ($lines=1)
 addText ($text)
 addVariable ($name, $value, $assignmentType=EZ_PHPCREATOR_VARIABLE_ASSIGNMENT, $parameters=array())
 addVariableUnset ($name, $parameters=array())
 addVariableUnsetList ($list, $parameters=array())

 canRestore ($timestamp=false)
 close ()
 exists ()
 fetch ($addPHPMarkers=true)
 open ($atomic=false)
 restore ($variableDefinitions)
 store ($atomic=false)

Static Public Member Functions

 prependSpacing ($text, $spacing, $skipEmptyLines=true, $spacingString=" ", $splitString="\n")
 variableNameText ($variableName, $assignmentType, $variableParameters=array())

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=EZ_PHPCREATOR_VARIABLE_ASSIGNMENT, $variableParameters=array())
 writeVariableUnset ($element)
 writeVariableUnsetList ($element)

Private Attributes

 $ClusterFileScope = false
 $ClusteringEnabled = false
 $Elements
 $FileResource
 $isAtomic
 $PHPDir
 $PHPFile
 $requestedFilename
 $Spacing = true
 $TextChunks
 $tmpFilename

Detailed Description

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 97 of file ezphpcreator.php.


Member Function Documentation

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.

Parameters:
$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:

  • spacing, The number of spaces to place before each code line, default is 0.

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 377 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.

Parameters:
$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:

  • spacing, The number of spaces to place before each code line, default is 0.

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 407 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:
$parameters Optional parameters, can be any of the following:

  • spacing, The number of spaces to place before each code line, default is 0.
Note:
$name must start with a letter or underscore, followed by any number of letters, numbers, or underscores. See http://php.net/manual/en/language.constants.php for more information.
See also:
http://php.net/manual/en/function.define.php

Definition at line 155 of file ezphpcreator.php.

eZPHPCreator::addInclude ( file,
type = EZ_PHPCREATOR_INCLUDE_ONCE,
parameters = array() 
)

Adds an include statement to the code, the file to include is $file.

Parameters:
$type What type of include statement to use, can be one of the following:

  • EZ_PHPCREATOR_INCLUDE_ONCE, use include_once()
  • EZ_PHPCREATOR_INCLUDE_ALWAYS, use include()
$parameters Optional parameters, can be any of the following:

  • spacing, The number of spaces to place before each code line, default is 0.

Example:

$php->addInclude( 'lib/ezutils/classes/ezphpcreator.php' );

Would result in the PHP code.

include_once( 'lib/ezutils/classes/ezphpcreator.php' );

Definition at line 438 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:

  • 0, The parameter value
  • 1 (optional), The type of parameter, is one of:
    • EZ_PHPCREATOR_METHOD_CALL_PARAMETER_VALUE, Use value directly (default if this entry is missing)
    • EZ_PHPCREATOR_METHOD_CALL_PARAMETER_VARIABLE, Use value as the name of the variable.

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.

  • 0, The name of the variable to assign the value to
  • 1 (optional), The type of assignment, uses the same value as addVariable().
Parameters:
$parameters Optional parameters, can be any of the following:

  • spacing, The number of spaces to place before each code line, default is 0.

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 342 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 176 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 287 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 304 of file ezphpcreator.php.

eZPHPCreator::addVariable ( name,
value,
assignmentType = EZ_PHPCREATOR_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', EZ_PHPCREATOR_VARIABLE_APPEND_TEXT );
$php->addVariable( 'array', 42, EZ_PHPCREATOR_VARIABLE_APPEND_ELEMENT );

Would result in the PHP code.

$offset = 5;
$text .= 'some more text';
$array[] = 42;
Parameters:
$assignmentType Controls the way the value is assigned, choose one of the following:

  • EZ_PHPCREATOR_VARIABLE_ASSIGNMENT, assign using = (default)
  • EZ_PHPCREATOR_VARIABLE_APPEND_TEXT, append using text concat operator .
  • EZ_PHPCREATOR_VARIABLE_APPEND_ELEMENT, append element to array using append operator []
$parameters Optional parameters, can be any of the following:

  • spacing, The number of spaces to place before each code line, default is 0.
  • full-tree, Whether to displays array values as one large expression (true) or split it up into multiple variables (false)

Definition at line 210 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:
$parameters Optional parameters, can be any of the following:

  • spacing, The number of spaces to place before each code line, default is 0.
See also:
http://php.net/manual/en/function.unset.php

Definition at line 240 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:
$parameters Optional parameters, can be any of the following:

  • spacing, The number of spaces to place before each code line, default is 0.
See also:
http://php.net/manual/en/function.unset.php

Definition at line 268 of file ezphpcreator.php.

eZPHPCreator::canRestore ( timestamp = false  ) 
Returns:
true if file exists and can be restored.
Parameters:
$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.
Note:
The file name and path is supplied to the constructor of this class.

Definition at line 775 of file ezphpcreator.php.

eZPHPCreator::close (  ) 

Closes the currently open file if any.

Definition at line 731 of file ezphpcreator.php.

Referenced by store().

eZPHPCreator::exists (  ) 
Returns:
true if the file and path already exists.
Note:
The file name and path is supplied to the constructor of this class.

Definition at line 755 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.

Note:
Calling this multiple times will resulting text processing each time.

Definition at line 907 of file ezphpcreator.php.

eZPHPCreator::flushChunks (  )  [private]

Definition at line 958 of file ezphpcreator.php.

Referenced by fetch(), and store().

eZPHPCreator::open ( atomic = false  ) 

Opens the file for writing and sets correct file permissions.

Returns:
The current file resource or false if it failed to open the file.
Note:
The file name and path is supplied to the constructor of this class.
Multiple calls to this method will only open the file once.

Definition at line 688 of file ezphpcreator.php.

Referenced by store().

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.

Parameters:
$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 663 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().

Parameters:
$variableDefinitions Associative array with the return variable name being the key matched variable as value.
Returns:
An associatve array with the variables that were found according to $variableDefinitions.

Example:

$values = $php->restore( array( 'MyValue' => 'node' ) );
print( $values['MyValue'] );
Note:
The file name and path is supplied to the constructor of this class.

Definition at line 824 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 876 of file ezphpcreator.php.

eZPHPCreator::temporaryVariableName ( prefix  )  [private]

Definition at line 1256 of file ezphpcreator.php.

Referenced by variableText().

eZPHPCreator::variableNameText ( variableName,
assignmentType,
variableParameters = array() 
) [static]

Creates a variable statement with an assignment type and returns it.

Parameters:
$variableName The name of the variable
$assignmentType What kind of assignment to use, is one of the following;

  • EZ_PHPCREATOR_VARIABLE_ASSIGNMENT, assign using =
  • EZ_PHPCREATOR_VARIABLE_APPEND_TEXT, append to text using .
  • EZ_PHPCREATOR_VARIABLE_APPEND_ELEMENT, append to array using []
$variableParameters Optional parameters for the statement

  • is-reference, whether to do the assignment with reference or not (default is not)

Definition at line 460 of file ezphpcreator.php.

Referenced by writeMethodCall(), and writeVariable().

eZPHPCreator::variableText ( 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.

  • boolean, becomes true or false
  • null, becomes null
  • string, adds \ (backslash) to backslashes, double quotes, dollar signs and newlines. Then wraps the whole string in " (double quotes).
  • numeric, displays the value as-is.
  • array, expands all value recursively using this function
  • object, creates a representation of an object creation if the object has serializeData implemented.
Parameters:
$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.
Note:
This function can be called statically if $maxIterations is set to false

Definition at line 508 of file ezphpcreator.php.

Referenced by eZTemplateArrayOperator::arrayTrans(), eZTemplateLogicOperator::chooseTransformation(), eZTemplateArrayOperator::compareTrans(), eZTemplateControlOperator::condTransform(), eZTemplateArrayOperator::extractTrans(), eZTemplateLogicOperator::logicalComparisonTransformation(), eZTemplateArrayOperator::mergeTrans(), eZTemplateUnitOperator::operatorTransform(), eZKernelOperator::preferencesTransformation(), eZObjectForwarder::resourceAcquisitionTransformation(), eZTemplateSwitchFunction::templateNodeCaseTransformation(), eZTemplateSectionFunction::templateNodeTransformation(), eZTemplateDesignResource::templateNodeTransformation(), eZTemplateCacheFunction::templateNodeTransformation(), eZTemplateBlockFunction::templateNodeTransformation(), eZObjectForwarder::templateNodeTransformation(), writeDefine(), writeInclude(), writeMethodCall(), and writeVariable().

eZPHPCreator::write ( text  )  [private]
eZPHPCreator::writeChunks (  )  [private]

Definition at line 927 of file ezphpcreator.php.

Referenced by store().

eZPHPCreator::writeCodePiece ( element  )  [private]

Definition at line 1119 of file ezphpcreator.php.

Referenced by writeElements().

eZPHPCreator::writeComment ( element  )  [private]

Definition at line 1080 of file ezphpcreator.php.

Referenced by writeElements().

eZPHPCreator::writeDefine ( element  )  [private]

Definition at line 1030 of file ezphpcreator.php.

Referenced by writeElements().

eZPHPCreator::writeElements (  )  [private]

Definition at line 974 of file ezphpcreator.php.

Referenced by fetch(), and store().

eZPHPCreator::writeInclude ( element  )  [private]

Definition at line 1056 of file ezphpcreator.php.

Referenced by writeElements().

eZPHPCreator::writeMethodCall ( element  )  [private]

Definition at line 1144 of file ezphpcreator.php.

Referenced by writeElements().

eZPHPCreator::writeRawVariable ( variableName,
variableValue 
) [private]

Definition at line 1227 of file ezphpcreator.php.

Referenced by writeElements().

eZPHPCreator::writeSpace ( element  )  [private]

Definition at line 1110 of file ezphpcreator.php.

Referenced by writeElements().

eZPHPCreator::writeText ( element  )  [private]

Definition at line 1133 of file ezphpcreator.php.

Referenced by writeElements().

eZPHPCreator::writeVariable ( variableName,
variableValue,
assignmentType = EZ_PHPCREATOR_VARIABLE_ASSIGNMENT,
variableParameters = array() 
) [private]

Definition at line 1235 of file ezphpcreator.php.

Referenced by variableText(), and writeElements().

eZPHPCreator::writeVariableUnset ( element  )  [private]

Definition at line 1190 of file ezphpcreator.php.

Referenced by writeElements().

eZPHPCreator::writeVariableUnsetList ( element  )  [private]

Definition at line 1205 of file ezphpcreator.php.

Referenced by writeElements().


Member Data Documentation

eZPHPCreator::$ClusterFileScope = false [private]

Definition at line 1275 of file ezphpcreator.php.

eZPHPCreator::$ClusteringEnabled = false [private]

Definition at line 1274 of file ezphpcreator.php.

eZPHPCreator::$Elements [private]

Definition at line 1268 of file ezphpcreator.php.

eZPHPCreator::$FileResource [private]

Definition at line 1267 of file ezphpcreator.php.

eZPHPCreator::$isAtomic [private]

Definition at line 1270 of file ezphpcreator.php.

eZPHPCreator::$PHPDir [private]

Definition at line 1265 of file ezphpcreator.php.

eZPHPCreator::$PHPFile [private]

Definition at line 1266 of file ezphpcreator.php.

eZPHPCreator::$requestedFilename [private]

Definition at line 1272 of file ezphpcreator.php.

eZPHPCreator::$Spacing = true [private]

Definition at line 1273 of file ezphpcreator.php.

eZPHPCreator::$TextChunks [private]

Definition at line 1269 of file ezphpcreator.php.

eZPHPCreator::$tmpFilename [private]

Definition at line 1271 of file ezphpcreator.php.


The documentation for this class was generated from the following file:
Generated on Mon Jul 12 07:10:30 2010 for eZ publish by  doxygen 1.6.3