View Source

<h1>Zend_Tool_CodeGenerator</h1>

<h2>Goals</h2>

<p>It is the goal of the Zend_Tool_CodeGenerator is to provide an object oriented set of abstracts and interfaces to facilitate the tasks related to and around the task of generating structured code. Currently in PHP, developers are able to (through Reflection) disassemble object oriented code into finite objects. Zend_Tool_CodeGenerator will attempt to take finite objects and construct and modify existing files (at the top most level) to produce completely working PHP files, classes, methods, properties, and docblocks.</p>

<p>Zend_Tool_Codegenerator will supply a set of interfaces and abstracts that will be able to be used to produce other flavors of &quot;code&quot;, such as apache config files, php ini files, and potentially other languages such as javascript.</p>

<h2>Zend_Tool_CodeGenerator_CodeGeneratorAbstract</h2>

<p>Zend_Tool_CodeGenerator_CodeGeneratorAbstract is the simplest interface. Essentially, the only methods that are required are toString() and fromString(). This will ensure that any generators that use this component can read existing code and break it down into a finite object representation of the code, as well as take that object representation and write it out as code.</p>

<p>This interface also enforces the passing of options to the constructor that will allow for the generation of </p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php

abstract class Zend_Tool_CodeGenerator_CodeGeneratorAbstract
{

public function __construct(Array $options = array())
{
if ($options) {
$this->setOptions($options);
}
}

public function setOptions(Array $options)
{
foreach ($options as $optionName => $optionValue) {
$methodName = 'set' . $optionName;
if (method_exists($this, $methodName)) {
call_user_func(array($this, $methodName), $optionValue);
}
}
}

abstract public function fromString();

abstract public function toString();

final public function __toString()
{
return $this->toString();
}

}

]]></ac:plain-text-body></ac:macro>



<h2>Zend_Tool_CodeGenerator_Php</h2>

<p>The Zend_Tool_CodeGenerator_Php sub-component will be responsible for generating PHP based files. It will take cue from Reflection in what parts and bits of can become represented in object oriented notation. At current the following language constructs that can be retrieved through reflection will be implemented</p>

<ul>
<li>Classes</li>
<li>Methods/Functions</li>
<li>Properties</li>
<li>Parameters (for methods and functions)</li>
</ul>


<p>The following elements will be able to be generated by the PHP CodeGenerator, but are not explicitly modeled in the PHP Reflection API:</p>

<ul>
<li>Docblocks
<ul>
<li>File</li>
<li>Class</li>
<li>Method</li>
<li>Property</li>
<li>Required Files</li>
</ul>
</li>
<li>Method Bodies</li>
<li>File</li>
</ul>


<p>There exist the possiblity that in the future, &quot;body&quot; elements/language constructs can be implemented such as: foreach loops, switch statements, for loops, etc.</p>



<h3>Sample Usage</h3>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[

$className = 'IndexController';

$codeGenFile = new Zend_Tool_CodeGenerator_Php_File(array(
'classes' => array(
new Zend_Tool_CodeGenerator_Php_Class(array(
'className' => $className,
'extendedClassName' => 'Zend_Controller_Action',
'methods' => array(
new Zend_Tool_CodeGenerator_Php_Method(array(
'name' => 'init',
'body' => ' /* Initialize action controller here */'
)),
new Zend_Tool_CodeGenerator_Php_Method(array(
'name' => 'indexAction',
'body' => ' /* Default action for action controller */'
))
)
))
)
));

echo $codeGenFile;

Will produce:

<?php

class IndexController extends Zend_Controller_Action
{

public function init()
{
/* Initialize action controller here */
}

public function indexAction()
{
/* Default action for action controller */
}

}

]]></ac:plain-text-body></ac:macro>