Zend_CodeGenerator Examples
Example #1 Generating PHP classes
The following example generates an empty class with a class-level
DocBlock.
span style="color: #ff0000;">'shortDescription' => 'Sample generated class',
'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
'tags''name' => 'version',
'description' => '$Rev:$''name' => 'license',
'description' => 'New BSD',
),
),
));
$foo->setName('Foo'
The above code will result in the following:
/**
* Sample generated class
*
* This is a class generated with Zend_CodeGenerator.
*
* @version $Rev:$
* @license New BSD
*
*/
Example #2 Generating PHP classes with class properties
Building on the previous example, we now add properties to our
generated class.
span style="color: #ff0000;">'shortDescription' => 'Sample generated class',
'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
'tags''name' => 'version',
'description' => '$Rev:$''name' => 'license',
'description' => 'New BSD',
),
),
));
$foo->setName('Foo''name' => '_bar',
'visibility' => 'protected',
'defaultValue' => 'baz''name' => 'baz',
'visibility' => 'public',
'defaultValue' => 'bat''name' => 'bat',
'const''defaultValue' => 'foobarbazbat'
The above results in the following class definition:
/**
* Sample generated class
*
* This is a class generated with Zend_CodeGenerator.
*
* @version $Rev:$
* @license New BSD
*
*/'baz''bat';
const bat = 'foobarbazbat';
}
Example #3 Generating PHP classes with class methods
Zend_CodeGenerator_Php_Class allows you to attach
methods with optional content to your classes. Methods may be
attached as either arrays or concrete
Zend_CodeGenerator_Php_Method instances.
span style="color: #ff0000;">'shortDescription' => 'Sample generated class',
'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
'tags''name' => 'version',
'description' => '$Rev:$''name' => 'license',
'description' => 'New BSD',
),
),
));
$foo->setName('Foo''name' => '_bar',
'visibility' => 'protected',
'defaultValue' => 'baz''name' => 'baz',
'visibility' => 'public',
'defaultValue' => 'bat''name' => 'bat',
'const''defaultValue' => 'foobarbazbat'// Method passed as array
'name' => 'setBar',
'parameters''name' => 'bar'),
),
'body' => '$this->_bar = $bar;' . "\n" . 'return $this;',
'docblock''shortDescription' => 'Set the bar property',
'tags''paramName' => 'bar',
'datatype' => 'string''datatype' => 'string',
)),
),
)),
),
// Method passed as concrete instance
'name' => 'getBar',
'body' => 'return $this->_bar;',
'docblock''shortDescription' => 'Retrieve the bar property',
'tags''datatype' => 'string|null'
The above generates the following output:
/**
* Sample generated class
*
* This is a class generated with Zend_CodeGenerator.
*
* @version $Rev:$
* @license New BSD
*/'baz''bat';
const bat = 'foobarbazbat';
/**
* Set the bar property
*
* @param string bar
* @return string
*//**
* Retrieve the bar property
*
* @return string|null
*/
Example #4 Generating PHP files
Zend_CodeGenerator_Php_File can be used to generate the
contents of a PHP file. You can include classes as well as arbitrary
content body. When attaching classes, you should attach either
concrete Zend_CodeGenerator_Php_Class instances or an
array defining the class.
In the example below, we will assume you've defined
$foo per one of the class definitions in a previous
example.
span style="color: #ff0000;">'classes''docblock''shortDescription' => 'Foo class file',
'tags''name' => 'license',
'description' => 'New BSD',
),
),
)),
'body' => 'define(\'APPLICATION_ENV\', \'testing\');',
));
Calling generate() will generate the code -- but not
write it to a file. You will need to capture the contents and write
them to a file yourself. As an example:
span style="color: #ff0000;">'Foo.php', $code);
The above will generate the following file:
span style="color: #808080; font-style: italic;">/**
* Foo class file
*
* @license New BSD
*/
/**
* Sample generated class
*
* This is a class generated with Zend_CodeGenerator.
*
* @version $Rev:$
* @license New BSD
*/'baz''bat';
const bat = 'foobarbazbat';
/**
* Set the bar property
*
* @param string bar
* @return string
*//**
* Retrieve the bar property
*
* @return string|null
*/'APPLICATION_ENV', 'testing');
Example #5 Seeding PHP file code generation via reflection
You can add PHP code to an existing PHP file
using the code generator. To do so, you need to first do reflection on it. The
static method fromReflectedFileName() allows you to do
this.
span style="color: #ff0000;">"\n\$foo->bar();"
Example #6 Seeding PHP class generation via reflection
You may add code to an existing class. To do so, first use the
static fromReflection() method to map the class into a
generator object. From there, you may add additional properties or
methods, and then regenerate the class.
span style="color: #ff0000;">'name' => 'setBaz',
'parameters''name' => 'baz'),
),
'body' => '$this->_baz = $baz;' . "\n" . 'return $this;',
'docblock''shortDescription' => 'Set the baz property',
'tags''paramName' => 'baz',
'datatype' => 'string''datatype' => 'string',
)),
),
)),
));
$code = $generator->generate();
|
|