Zeta Components - high quality PHP components

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.

  1.  $generator = new ezcPhpGenerator( "~/file.php" );
  2.  $generator->appendCustomCode( 'function fibonacci( $number )' );
  3.  $generator->appendCustomCode( "{" );
  4.  
  5.  $generator->appendValueAssignment( "lo", 0 );
  6.  $generator->appendValueAssignment( "hi", 1 );
  7.  $generator->appendValueAssignment( "i", 2 );
  8.  
  9.  $generator->appendWhile( '$i < $number' );
  10.  $generator->appendCustomCode( '$hi = $lo + $hi;' );
  11.  $generator->appendCustomCode( '$lo = $hi - $lo;' );
  12.  $generator->appendCustomCode( '$i++;' );
  13.  $generator->appendEndWhile();
  14.  $generator->appendCustomCode( 'return $hi;' );
  15.  $generator->appendCustomCode( "}" );
  16.  $generator->finish();

The above code will fill the file "~/file.php" with the following contents:

  1.  <?php
  2.  function fibonacci( $number )
  3.  {
  4.  $lo = 0;
  5.  $hi = 1;
  6.  $i = 2;
  7.  while ( $i < $number )
  8.  {
  9.    $hi = $lo + $hi;
  10.    $lo = $hi - $lo;
  11.    $i++;
  12.  }
  13.  return $hi;
  14.  }
  15.  ?>

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

ezcPhpGenerator __construct( string $filename , [bool $includeStartEndTags = true] , [bool $niceIndentation = false] )

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

void __destruct( )

Destructs the object.

Removes all temporary files that are in use.

abort

void 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

void appendComment( string $comment )

Inserts a comment into the generated code.

The comment will be displayed using an end-of-line comment (//).

Example:

  1.  $php->addComment( "This file is auto generated. Do not edit!" );
  2.  <code>
  3.  
  4.  Produces the PHP code:
  5.  <code>
  6.  // 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

void appendCustomCode( string $code )

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:

  1.  $php->addCodePiece( "if ( \$value > 2 )" . $php->lineBreak()
  2.                      . '{' . $php->lineBreak() .  "\$value = 2;" );

Produces the PHP code:

  1.  if ( $value > 2 )
  2.  {
  3.    $value = 2;
  4.  }
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

void appendDefine( string $name , string $value , [bool $caseInsensitive = false] )

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:

  1.  $php->addDefine( 'MY_CONSTANT', 5 );

Produces:

  1.  define( 'MY_CONSTANT', 5 );
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

void appendDo( )

Inserts a do statement in the generated code.

The do statement must be closed properly with a call to appendEndDo().

Example:

  1.  $php->appendDo();
  2.  $php->appendEndDo( '$myVar > 0' );

Produces the PHP code:

  1.  do
  2.  {
  3.  } while ( $myVar > 0 );
Exceptions:
Type Description
ezcPhpGeneratorException if it was not possible to write to the output file.

appendElse

void appendElse( [string $condition = ''] )

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:

  1.  $php->appendIf( '$myVar === 0 ' );
  2.  $php->appendElse( '$myVar2 === 0 ' );
  3.  $php->appendElse();
  4.  $php->appendEndIf();

Produces the PHP code:

  1.  if ( $myVar === 0 )
  2.  {
  3.  }
  4.  else if ( $myVar )
  5.  {
  6.  }
  7.  else
  8.  {
  9.  }
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

void appendEmptyLines( [int $lines = 1] )

Inserts $lines number of empty lines in the generated code.

Example:

  1.  $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

void appendEndDo( string $condition )

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

void 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

void 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

void 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

void appendForeach( string $condition )

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:

  1.  $php->appendForeach( '$myArray as $item ' );
  2.  $php->appendEndForeach();

Produces the PHP code:

  1.  foreach ( $myArray as $item )
  2.  {
  3.  }
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

void appendFunctionCall( string $functionName , $parameters , [ezcPhpGeneratorReturnData $returnData = null] )

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:

  1.  $php->appendFunctionCall( 'str_repeat', array( new ezcPhpGeneratorParameter( 'repeat' ),
  2.                                                 new ezcPhpGeneratorParameter( 4, ezcPhpGenerator::VALUE ) );

Produces the PHP code:

  1.  $var = str_repeat( $repeat, 4 );
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

void appendIf( string $condition )

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:

  1.  $php->appendIf( '$myVar === 0 ' );
  2.  $php->appendEndIf();

Produces the PHP code:

  1.  if ( $myVar === 0 )
  2.  {
  3.  }
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

void appendMethodCall( string $objectName , string $methodName , [ $parameters = array()] , [ezcPhpGeneratorReturnData $returnData = null] )

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:

  1.  $php->appendMethodCall( 'node', 'name', array(), new ezcPhpGeneratorReturnType( 'result' ) );

Produces the PHP code:

  1.  $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

void appendMethodOrFunctionCall( string $functionName , $parameters , [ezcPhpGeneratorReturnData $returnData = null] , [string $objectVariable = null] )

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

void appendUnset( string $name )

Unsets the variable $name in the generated code.

Example:

  1.  $php->addVariableUnset( 'offset' );

Produces the PHP code:

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

void appendUnsetList( $list )

Unsets the variable names in $list in the generated code.

Example:

  1.  $php->addVariableUnsetList( array ( 'var1', 'var2' ) );

Produces the PHP code:

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

void appendValueAssignment( string $name , mixed $value , [int $assignmentType = ezcPhpGenerator::ASSIGN_NORMAL] )

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:

  1.  $array = array( 1, 2, 3 );
  2.  $php->appendValueAssignment( 'ProducedArray', $array );

Produces the PHP code:

  1.  $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

void appendVariableAssignment( string $name , mixed $variable , [int $assignmentType = ezcPhpGenerator::ASSIGN_NORMAL] )

Assigns the variable named $variable to the variable $name in the generated code.

You can control the assignment type with the $assignmentType parameter.

Example:

  1.  $php->addVariableAssignment( 'ProducedArray', 'otherVar' );

Produces the PHP code:

  1.  $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

void appendWhile( string $condition )

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:

  1.  $php->appendWhile( '$myVar > 0' );
  2.  $php->appendEndWhile();

Produces the PHP code:

  1.  while ( $myVar > 0 )
  2.  {
  3.  }
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

void 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

string indentCode( string $text )

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

void write( string $data )

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.
Documentation generated by phpDocumentor 1.4.3