Zeta Components Manual :: Docs For Class ezcPhpGenerator
PhpGenerator::ezcPhpGenerator
Class ezcPhpGenerator
ezcPhpGenerator provides a simple API to generate PHP code.
This class can be used to generate quick and simple PHP scripts. Typical usage would be to generate caches in the form of arrays that also require some logic.
The following example shows how you can use ezcPhpGenerator to make a method that produces a Fibonacci number.
The above code will fill the file "~/file.php" with the following contents:
- <?php
- function fibonacci( $number )
- {
- $lo = 0;
- $hi = 1;
- $i = 2;
- while ( $i < $number )
- {
- $hi = $lo + $hi;
- $lo = $hi - $lo;
- $i++;
- }
- return $hi;
- }
- ?>
Source for this file: /PhpGenerator/src/php_generator.php
Version: | //autogentag// |
Constants
ASSIGN_ADD
= 3
|
Assignment with add '+='. |
ASSIGN_APPEND_TEXT
= 2
|
Text append assignment '.='. |
ASSIGN_ARRAY_APPEND
= 5
|
Assignment with array append $var[] ='. |
ASSIGN_NORMAL
= 1
|
Normal assignment '='. |
ASSIGN_SUBTRACT
= 4
|
Assignment with subtraction '-='. |
FLOW_DO
= 'do'
|
'do' program flow structure. |
FLOW_FOR
= 'for'
|
'for' program flow structure. |
FLOW_FOREACH
= 'foreach'
|
'foreach' program flow structure. |
FLOW_IF
= 'if'
|
'if' program flow structure. |
FLOW_WHILE
= 'while'
|
'while' program flow structure. |
Properties
int | read/write |
$indentLevel
Contains the level of indentation. Increase or decrease by one if you want the indentation level to change. |
string | read/write |
$indentString
Contains the characters that are indented per indentation level. The default is ' ' (two spaces). |
string | read/write |
$lineBreak
Contains the characters to use for linebreaks. The default is "\r\n". |
bool | read/write |
$niceIndentation
Controls whether to output the PHP nicely indented or not. The default is false. |
Method Summary
public ezcPhpGenerator |
__construct(
$filename
, [ $includeStartEndTags
= true] , [ $niceIndentation
= false] )
Constructs a new ezcPhpGenerator. |
public void |
__destruct(
)
Destructs the object. |
public void |
abort(
)
Aborts the PHP generating. Cleans up the file handler and the temporary file. |
public void |
appendComment(
$comment
)
Inserts a comment into the generated code. |
public void |
appendCustomCode(
$code
)
Inserts custom code into the generated code. |
public void |
appendDefine(
$name
, $value
, [ $caseInsensitive
= false] )
Defines the variable $name with the value $value in the generated code. |
public void |
appendDo(
)
Inserts a do statement in the generated code. |
public void |
appendElse(
[ $condition
= ''] )
Inserts an else or an else if statement into the generated code. |
public void |
appendEmptyLines(
[ $lines
= 1] )
Inserts $lines number of empty lines in the generated code. |
public void |
appendEndDo(
$condition
)
Ends a do statement in the generated code. |
public void |
appendEndForeach(
)
Ends a foreach statement in the generated code. |
public void |
appendEndIf(
)
Ends an if statement in the generated code. |
public void |
appendEndWhile(
)
Ends a while statement in the generated code. |
public void |
appendForeach(
$condition
)
Inserts a foreach statement into the generated code. |
public void |
appendFunctionCall(
$functionName
, $parameters
, [ $returnData
= null] )
Inserts a function call in the generated code. |
public void |
appendIf(
$condition
)
Inserts an if statement into the generated code. |
public void |
appendMethodCall(
$objectName
, $methodName
, [ $parameters
= array()] , [ $returnData
= null] )
Inserts a method call on an object in the generated code. |
protected void |
appendMethodOrFunctionCall(
$functionName
, $parameters
, [ $returnData
= null] , [ $objectVariable
= null] )
Inserts a method or function call in the generated code. |
public void |
appendUnset(
$name
)
Unsets the variable $name in the generated code. |
public void |
appendUnsetList(
$list
)
Unsets the variable names in $list in the generated code. |
public void |
appendValueAssignment(
$name
, $value
, [ $assignmentType
= ezcPhpGenerator::ASSIGN_NORMAL] )
Assigns $value to the variable $name in the generated code. |
public void |
appendVariableAssignment(
$name
, $variable
, [ $assignmentType
= ezcPhpGenerator::ASSIGN_NORMAL] )
Assigns the variable named $variable to the variable $name in the generated code. |
public void |
appendWhile(
$condition
)
Inserts a while statement in the generated code. |
public void |
finish(
)
Completes the code generation |
protected string |
indentCode(
$text
)
Returns each line in $text indented correctly if indenting is turned on. |
protected void |
write(
$data
)
Writes $data to $this->fileResource |
Methods
__construct
Constructs a new ezcPhpGenerator.
Constructs a new ezcPhpGenerator that writes to the file $fileName. If $includeStartEndTags is set the start and end PHP tags will be included. It is useful to omit these if you want to run the generated code using eval() later. $niceIndentation controls if the PHP output should be indented correctly. This option is useful if you want to debug the generated code.
Parameters:
Name | Type | Description |
---|---|---|
$filename |
string | |
$includeStartEndTags |
bool | |
$niceIndentation |
bool |
Exceptions:
Type | Description |
---|---|
ezcBaseFilePermissionException |
if the path specified by $filename is not writeable. |
ezcBaseFileNotFoundException |
if $filename does not contain a valid path. |
__destruct
Destructs the object.
Removes all temporary files that are in use.
abort
Aborts the PHP generating. Cleans up the file handler and the temporary file.
Subsequent calls to any methods that generate code will fail.
appendComment
Inserts a comment into the generated code.
The comment will be displayed using an end-of-line comment (//).
Example:
- $php->addComment( "This file is auto generated. Do not edit!" );
- <code>
- Produces the PHP code:
- <code>
- // This file is auto generated. Do not edit!
Parameters:
Name | Type | Description |
---|---|---|
$comment |
string |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the comment to the output file. |
appendCustomCode
Inserts custom code into the generated code.
Inserts the $code directly into the generated code. Correct indenting and a linebreak at the end of your code will be inserted automatically.
Example:
- $php->addCodePiece( "if ( \$value > 2 )" . $php->lineBreak()
- . '{' . $php->lineBreak() . "\$value = 2;" );
Produces the PHP code:
- if ( $value > 2 )
- {
- $value = 2;
- }
Parameters:
Name | Type | Description |
---|---|---|
$code |
string |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the custom code to the output file. |
appendDefine
Defines the variable $name with the value $value in the generated code.
The parameter $caseSensitive determines if the defined variable is case sensitive or not. Note that $name must start with a letter or underscore, followed by any number of letters, numbers, or underscores. http://php.net/manual/en/language.constants.php for more information. http://php.net/manual/en/function.define.php
Example:
- $php->addDefine( 'MY_CONSTANT', 5 );
Produces:
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | |
$value |
string | |
$caseInsensitive |
bool |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the define to the output file. |
appendDo
Inserts a do statement in the generated code.
The do statement must be closed properly with a call to appendEndDo().
Example:
Produces the PHP code:
- do
- {
- } while ( $myVar > 0 );
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write to the output file. |
appendElse
Inserts an else or an else if statement into the generated code.
If a $condition is provided an else if statement is generated. If not, an else statement is generated. You can only call this method after calling appendIf first.
Example:
Produces the PHP code:
- if ( $myVar === 0 )
- {
- }
- else if ( $myVar )
- {
- }
- else
- {
- }
Parameters:
Name | Type | Description |
---|---|---|
$condition |
string |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the if statement to the output file. |
appendEmptyLines
Inserts $lines number of empty lines in the generated code.
Example:
- $php->addSpace( 1 );
Parameters:
Name | Type | Description |
---|---|---|
$lines |
int |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the empty lines to the output file. |
appendEndDo
Ends a do statement in the generated code.
The complete condition of the do statement is provided through $condition.
Parameters:
Name | Type | Description |
---|---|---|
$condition |
string |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the do statement to the output file or if the method was not properly nested with an appendDo. |
appendEndForeach
Ends a foreach statement in the generated code.
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write to the output file or if the method was not properly nested with an appendForeach. |
appendEndIf
Ends an if statement in the generated code.
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write to the output file or if the method was not properly nested with an appendIf. |
appendEndWhile
Ends a while statement in the generated code.
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write to the output file or if the method was not properly nested with an appendWhile. |
appendForeach
Inserts a foreach statement into the generated code.
The complete condition of the foreach statement is provided through $condition. The foreach statement must be closed properly with a call to appendEndForeach().
Example:
Produces the PHP code:
- foreach ( $myArray as $item )
- {
- }
Parameters:
Name | Type | Description |
---|---|---|
$condition |
string |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the foreach statement to the output file. |
appendFunctionCall
Inserts a function call in the generated code.
Inserts a call to $functionName with the parameters $parameters. Set the $returnData parameter if you want to catch the return value.
Example:
Produces the PHP code:
Parameters:
Name | Type | Description |
---|---|---|
$functionName |
string | |
$parameters |
array(ezcPhpGeneratorParameter) | |
$returnData |
ezcPhpGeneratorReturnData |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the method call to the output file. |
appendIf
Inserts an if statement into the generated code.
The complete condition of the if statement is provided through $condition. The if statement must be closed properly with a call to appendEndIf().
Example:
Produces the PHP code:
- if ( $myVar === 0 )
- {
- }
Parameters:
Name | Type | Description |
---|---|---|
$condition |
string |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the if statement to the output file. |
appendMethodCall
Inserts a method call on an object in the generated code.
Inserts a call to the method $methodName on the object $objectName with parameters $parameters. Set the $returnData parameter if you want to catch the return value.
Example:
Produces the PHP code:
- $result = $node->name();
Parameters:
Name | Type | Description |
---|---|---|
$objectName |
string | |
$methodName |
string | |
$parameters |
array(ezcPhpGeneratorParameter) | |
$returnData |
ezcPhpGeneratorReturnData |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the method call to the output file. |
appendMethodOrFunctionCall
Inserts a method or function call in the generated code.
A method call is inserted if $objectName is provided. If not a function call is inserted. This method is a helper method for appendFunctionCall and appendMethodCall. See their description for further description of the parameters and examples.
Parameters:
Name | Type | Description |
---|---|---|
$functionName |
string | |
$parameters |
array(ezcPhpGeneratorParameter) | |
$returnData |
ezcPhpGeneratorReturnData | |
$objectVariable |
string | The variable name containing the object. |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the method call to the output file. |
appendUnset
Unsets the variable $name in the generated code.
Example:
- $php->addVariableUnset( 'offset' );
Produces the PHP code:
- unset( $offset );
Parameters:
Name | Type | Description |
---|---|---|
$name |
string |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the unset to the output file. |
appendUnsetList
Unsets the variable names in $list in the generated code.
Example:
- $php->addVariableUnsetList( array ( 'var1', 'var2' ) );
Produces the PHP code:
- unset( $var1, $var2 );
Parameters:
Name | Type | Description |
---|---|---|
$list |
array | Array of variable names. |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the unset to the output file. |
appendValueAssignment
Assigns $value to the variable $name in the generated code.
$value is exported using var_export(). This allows you to use complex structures for $value. If you want to append an assignment to a variable in the generated code use appendVariableAssignment.
You can control the assignment type with the $assignmentType parameter.
Example:
- $array = array( 1, 2, 3 );
Produces the PHP code:
- $ProducedArray = array( 1, 2, 3 );
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | |
$value |
mixed | |
$assignmentType |
int | ezcPhpGenerator:: ASSIGN_NORMAL, ASSIGN_APPEND_TEXT, ASSIGN_ADD, ASSIGN_SUBTRACT or ASSIGN_ARRAY_APPEND. |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the assignment to the output file. |
appendVariableAssignment
Assigns the variable named $variable to the variable $name in the generated code.
You can control the assignment type with the $assignmentType parameter.
Example:
- $php->addVariableAssignment( 'ProducedArray', 'otherVar' );
Produces the PHP code:
- $ProducedArray = $otherVar;
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | |
$variable |
mixed | |
$assignmentType |
int | ezcPhpGenerator:: ASSIGN_NORMAL, ASSIGN_APPEND_TEXT, ASSIGN_ADD, ASSIGN_SUBTRACT or ASSIGN_ARRAY_APPEND. |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the assignment to the output file. |
appendWhile
Inserts a while statement in the generated code.
The complete condition of the while statement is provided through $condition. The while statement must be closed properly with a call to appendEndWhile().
Example:
Produces the PHP code:
- while ( $myVar > 0 )
- {
- }
Parameters:
Name | Type | Description |
---|---|---|
$condition |
string |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write the while statement to the output file. |
finish
Completes the code generation
This method must be called when you have finished generating a file. It moves the temporary file that was used for writing to the end result file and releases used resources. Subsequent calls to any methods generating code will fail.
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write to the output file or if there are any control structures (if/foreach etc.) still open. |
indentCode
Returns each line in $text indented correctly if indenting is turned on.
If indenting is turned off it will return $text unmodified.
Parameters:
Name | Type | Description |
---|---|---|
$text |
string |
write
Writes $data to $this->fileResource
Parameters:
Name | Type | Description |
---|---|---|
$data |
string |
Exceptions:
Type | Description |
---|---|
ezcPhpGeneratorException |
if it was not possible to write to the file. |