1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.codegenerator.reference">
4 <title>Zend_CodeGenerator Reference</title>
6 <sect2 id="zend.codegenerator.reference.abstracts">
7 <title>Abstract Classes and Interfaces</title>
9 <sect3 id="zend.codegenerator.reference.abstracts.abstract">
10 <title>Zend_CodeGenerator_Abstract</title>
13 The base class from which all CodeGenerator classes inherit
14 provides the minimal functionality necessary. It's <acronym>API</acronym> is as
18 <programlisting language="php"><![CDATA[
19 abstract class Zend_CodeGenerator_Abstract
21 final public function __construct(Array $options = array())
22 public function setOptions(Array $options)
23 public function setSourceContent($sourceContent)
24 public function getSourceContent()
25 protected function _init()
26 protected function _prepare()
27 abstract public function generate();
28 final public function __toString()
33 The constructor first calls <methodname>_init()</methodname> (which is left
34 empty for the concrete extending class to implement), then
35 passes the <varname>$options</varname> parameter to
36 <methodname>setOptions()</methodname>, and finally calls
37 <methodname>_prepare()</methodname> (again, to be implemented by an
42 Like most classes in Zend Framework, <methodname>setOptions()</methodname>
43 compares an option key to existing setters in the class, and
44 passes the value on to that method if found.
48 <methodname>__toString()</methodname> is marked as final, and proxies to
49 <methodname>generate()</methodname>.
53 <methodname>setSourceContent()</methodname> and
54 <methodname>getSourceContent()</methodname> are intended to either set the
55 default content for the code being generated, or to replace said
56 content once all generation tasks are complete.
60 <sect3 id="zend.codegenerator.reference.abstracts.php-abstract">
61 <title>Zend_CodeGenerator_Php_Abstract</title>
64 <classname>Zend_CodeGenerator_Php_Abstract</classname> extends
65 <classname>Zend_CodeGenerator_Abstract</classname>, and adds some
66 properties for tracking whether content has changed as well as
67 the amount of indentation that should appear before generated
68 content. Its <acronym>API</acronym> is as follows:
71 <programlisting language="php"><![CDATA[
72 abstract class Zend_CodeGenerator_Php_Abstract
73 extends Zend_CodeGenerator_Abstract
75 public function setSourceDirty($isSourceDirty = true)
76 public function isSourceDirty()
77 public function setIndentation($indentation)
78 public function getIndentation()
83 <sect3 id="zend.codegenerator.reference.abstracts.php-member-abstract">
84 <title>Zend_CodeGenerator_Php_Member_Abstract</title>
87 <classname>Zend_CodeGenerator_Php_Member_Abstract</classname> is a base
88 class for generating class members -- properties and methods --
89 and provides accessors and mutators for establishing visibility;
90 whether or not the member is abstract, static, or final; and the
91 name of the member. Its <acronym>API</acronym> is as follows:
94 <programlisting language="php"><![CDATA[
95 abstract class Zend_CodeGenerator_Php_Member_Abstract
96 extends Zend_CodeGenerator_Php_Abstract
98 public function setAbstract($isAbstract)
99 public function isAbstract()
100 public function setStatic($isStatic)
101 public function isStatic()
102 public function setVisibility($visibility)
103 public function getVisibility()
104 public function setName($name)
105 public function getName()
111 <sect2 id="zend.codegenerator.reference.concrete">
112 <title>Concrete CodeGenerator Classes</title>
114 <sect3 id="zend.codegenerator.reference.concrete.php-body">
115 <title>Zend_CodeGenerator_Php_Body</title>
118 <classname>Zend_CodeGenerator_Php_Body</classname> is intended for
119 generating arbitrary procedural code to include within a file.
120 As such, you simply set content for the object, and it will
121 return that content when you invoke <methodname>generate()</methodname>.
125 The <acronym>API</acronym> of the class is as follows:
128 <programlisting language="php"><![CDATA[
129 class Zend_CodeGenerator_Php_Body extends Zend_CodeGenerator_Php_Abstract
131 public function setContent($content)
132 public function getContent()
133 public function generate()
138 <sect3 id="zend.codegenerator.reference.concrete.php-class">
139 <title>Zend_CodeGenerator_Php_Class</title>
142 <classname>Zend_CodeGenerator_Php_Class</classname> is intended for
143 generating <acronym>PHP</acronym> classes. The basic functionality just generates
144 the <acronym>PHP</acronym> class itself, as well as optionally the related
145 <acronym>PHP</acronym> DocBlock. Classes may implement or inherit from other
146 classes, and may be marked as abstract. Utilizing other code generator
147 classes, you can also attach class constants, properties, and
152 The <acronym>API</acronym> is as follows:
155 <programlisting language="php"><![CDATA[
156 class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
158 public static function fromReflection(
159 Zend_Reflection_Class $reflectionClass
161 public function setDocblock(Zend_CodeGenerator_Php_Docblock $docblock)
162 public function getDocblock()
163 public function setName($name)
164 public function getName()
165 public function setAbstract($isAbstract)
166 public function isAbstract()
167 public function setExtendedClass($extendedClass)
168 public function getExtendedClass()
169 public function setImplementedInterfaces(Array $implementedInterfaces)
170 public function getImplementedInterfaces()
171 public function setProperties(Array $properties)
172 public function setProperty($property)
173 public function getProperties()
174 public function getProperty($propertyName)
175 public function setMethods(Array $methods)
176 public function setMethod($method)
177 public function getMethods()
178 public function getMethod($methodName)
179 public function hasMethod($methodName)
180 public function isSourceDirty()
181 public function generate()
186 The <methodname>setProperty()</methodname> method accepts an array of
187 information that may be used to generate a
188 <classname>Zend_CodeGenerator_Php_Property</classname> instance -- or
189 simply an instance of
190 <classname>Zend_CodeGenerator_Php_Property</classname>.
191 Likewise, <methodname>setMethod()</methodname> accepts either an array of
192 information for generating a
193 <classname>Zend_CodeGenerator_Php_Method</classname> instance or a
194 concrete instance of that class.
198 Note that <methodname>setDocBlock()</methodname> expects an instance of
199 <classname>Zend_CodeGenerator_Php_DocBlock</classname>.
203 <sect3 id="zend.codegenerator.reference.concrete.php-docblock">
204 <title>Zend_CodeGenerator_Php_Docblock</title>
207 <classname>Zend_CodeGenerator_Php_Docblock</classname> can be used to
208 generate arbitrary <acronym>PHP</acronym> docblocks, including all the standard
209 docblock features: short and long descriptions and annotation
214 Annotation tags may be set using the <methodname>setTag()</methodname> and
215 <methodname>setTags()</methodname> methods; these each take either an array
216 describing the tag that may be passed to the
217 <classname>Zend_CodeGenerator_Php_Docblock_Tag</classname> constructor, or
218 an instance of that class.
222 The <acronym>API</acronym> is as follows:
225 <programlisting language="php"><![CDATA[
226 class Zend_CodeGenerator_Php_Docblock extends Zend_CodeGenerator_Php_Abstract
228 public static function fromReflection(
229 Zend_Reflection_Docblock $reflectionDocblock
231 public function setShortDescription($shortDescription)
232 public function getShortDescription()
233 public function setLongDescription($longDescription)
234 public function getLongDescription()
235 public function setTags(Array $tags)
236 public function setTag($tag)
237 public function getTags()
238 public function generate()
243 <sect3 id="zend.codegenerator.reference.concrete.php-docblock-tag">
244 <title>Zend_CodeGenerator_Php_Docblock_Tag</title>
247 <classname>Zend_CodeGenerator_Php_Docblock_Tag</classname> is intended for
248 creating arbitrary annotation tags for inclusion in <acronym>PHP</acronym>
249 docblocks. Tags are expected to contain a name (the portion
250 immediately following the '@' symbol) and a description
251 (everything following the tag name).
255 The class <acronym>API</acronym> is as follows:
258 <programlisting language="php"><![CDATA[
259 class Zend_CodeGenerator_Php_Docblock_Tag
260 extends Zend_CodeGenerator_Php_Abstract
262 public static function fromReflection(
263 Zend_Reflection_Docblock_Tag $reflectionTag
265 public function setName($name)
266 public function getName()
267 public function setDescription($description)
268 public function getDescription()
269 public function generate()
274 <sect3 id="zend.codegenerator.reference.concrete.php-docblock-tag-param">
275 <title>Zend_CodeGenerator_Php_DocBlock_Tag_Param</title>
278 <classname>Zend_CodeGenerator_Php_DocBlock_Tag_Param</classname> is a
279 specialized version of
280 <classname>Zend_CodeGenerator_Php_DocBlock_Tag</classname>, and represents
281 a method parameter. The tag name is therefor known ("param"),
282 but due to the format of this annotation tag, additional
283 information is required in order to generate it: the parameter
284 name and data type it represents.
288 The class <acronym>API</acronym> is as follows:
291 <programlisting language="php"><![CDATA[
292 class Zend_CodeGenerator_Php_Docblock_Tag_Param
293 extends Zend_CodeGenerator_Php_Docblock_Tag
295 public static function fromReflection(
296 Zend_Reflection_Docblock_Tag $reflectionTagParam
298 public function setDatatype($datatype)
299 public function getDatatype()
300 public function setParamName($paramName)
301 public function getParamName()
302 public function generate()
307 <sect3 id="zend.codegenerator.reference.concrete.php-docblock-tag-return">
308 <title>Zend_CodeGenerator_Php_DocBlock_Tag_Return</title>
311 Like the param docblock tag variant,
312 <classname>Zend_CodeGenerator_Php_Docblock_Tab_Return</classname> is an
313 annotation tag variant for representing a method return value.
314 In this case, the annotation tag name is known ("return"), but
315 requires a return type.
319 The class <acronym>API</acronym> is as follows:
322 <programlisting language="php"><![CDATA[
323 class Zend_CodeGenerator_Php_Docblock_Tag_Param
324 extends Zend_CodeGenerator_Php_Docblock_Tag
326 public static function fromReflection(
327 Zend_Reflection_Docblock_Tag $reflectionTagReturn
329 public function setDatatype($datatype)
330 public function getDatatype()
331 public function generate()
336 <sect3 id="zend.codegenerator.reference.concrete.php-file">
337 <title>Zend_CodeGenerator_Php_File</title>
340 <classname>Zend_CodeGenerator_Php_File</classname> is used to generate
341 the full contents of a file that will contain <acronym>PHP</acronym> code. The file
342 may contain classes or arbitrary <acronym>PHP</acronym> code, as well as a
343 file-level docblock if desired.
347 When adding classes to the file, you will need to pass either an
348 array of information to pass to the
349 <classname>Zend_CodeGenerator_Php_Class</classname> constructor, or an
350 instance of that class. Similarly, with docblocks, you will need
351 to pass information for the
352 <classname>Zend_CodeGenerator_Php_Docblock</classname> constructor to
353 consume or an instance of the class.
357 The <acronym>API</acronym> of the class is as follows:
360 <programlisting language="php"><![CDATA[
361 class Zend_CodeGenerator_Php_File extends Zend_CodeGenerator_Php_Abstract
363 public static function fromReflectedFilePath(
365 $usePreviousCodeGeneratorIfItExists = true,
366 $includeIfNotAlreadyIncluded = true)
367 public static function fromReflection(Zend_Reflection_File $reflectionFile)
368 public function setDocblock(Zend_CodeGenerator_Php_Docblock $docblock)
369 public function getDocblock()
370 public function setRequiredFiles($requiredFiles)
371 public function getRequiredFiles()
372 public function setClasses(Array $classes)
373 public function getClass($name = null)
374 public function setClass($class)
375 public function setFilename($filename)
376 public function getFilename()
377 public function getClasses()
378 public function setBody($body)
379 public function getBody()
380 public function isSourceDirty()
381 public function generate()
386 <sect3 id="zend.codegenerator.reference.concrete.php-member-container">
387 <title>Zend_CodeGenerator_Php_Member_Container</title>
390 <classname>Zend_CodeGenerator_Php_Member_Container</classname> is used
391 internally by <classname>Zend_CodeGenerator_Php_Class</classname> to keep
392 track of class members -- properties and methods alike. These
393 are indexed by name, using the concrete instances of the members
398 The <acronym>API</acronym> of the class is as follows:
401 <programlisting language="php"><![CDATA[
402 class Zend_CodeGenerator_Php_Member_Container extends ArrayObject
404 public function __construct($type = self::TYPE_PROPERTY)
409 <sect3 id="zend.codegenerator.reference.concrete.php-method">
410 <title>Zend_CodeGenerator_Php_Method</title>
413 <classname>Zend_CodeGenerator_Php_Method</classname> describes a class
414 method, and can generate both the code and the docblock for the
415 method. The visibility and status as static,
416 abstract, or final may be indicated, per its parent class,
417 <classname>Zend_CodeGenerator_Php_Member_Abstract</classname>. Finally,
418 the parameters and return value for the method may be specified.
422 Parameters may be set using <methodname>setParameter()</methodname> or
423 <methodname>setParameters()</methodname>. In each case, a parameter should
424 either be an array of information to pass to the
425 <classname>Zend_CodeGenerator_Php_Parameter</classname> constructor or an
426 instance of that class.
430 The <acronym>API</acronym> of the class is as follows:
433 <programlisting language="php"><![CDATA[
434 class Zend_CodeGenerator_Php_Method
435 extends Zend_CodeGenerator_Php_Member_Abstract
437 public static function fromReflection(
438 Zend_Reflection_Method $reflectionMethod
440 public function setDocblock(Zend_CodeGenerator_Php_Docblock $docblock)
441 public function getDocblock()
442 public function setFinal($isFinal)
443 public function setParameters(Array $parameters)
444 public function setParameter($parameter)
445 public function getParameters()
446 public function setBody($body)
447 public function getBody()
448 public function generate()
453 <sect3 id="zend.codegenerator.reference.concrete.php-parameter">
454 <title>Zend_CodeGenerator_Php_Parameter</title>
457 <classname>Zend_CodeGenerator_Php_Parameter</classname> may be used to
458 specify method parameters. Each parameter may have a position
459 (if unspecified, the order in which they are registered with the
460 method will be used), a default value, and a data type; a
461 parameter name is required.
465 The <acronym>API</acronym> of the class is as follows:
468 <programlisting language="php"><![CDATA[
469 class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
471 public static function fromReflection(
472 Zend_Reflection_Parameter $reflectionParameter
474 public function setType($type)
475 public function getType()
476 public function setName($name)
477 public function getName()
478 public function setDefaultValue($defaultValue)
479 public function getDefaultValue()
480 public function setPosition($position)
481 public function getPosition()
482 public function getPassedByReference()
483 public function setPassedByReference($passedByReference)
484 public function generate()
489 There are several problems that might occur when trying to set
490 <constant>NULL</constant>, booleans or arrays as default values. For this the value
491 holder object <classname>Zend_CodeGenerator_Php_ParameterDefaultValue</classname>
492 can be used, for example:
495 <programlisting language="php"><![CDATA[
496 $parameter = new Zend_CodeGenerator_Php_Parameter();
497 $parameter->setDefaultValue(
498 new Zend_CodeGenerator_Php_Parameter_DefaultValue("null")
500 $parameter->setDefaultValue(
501 new Zend_CodeGenerator_Php_Parameter_DefaultValue("array('foo', 'bar')")
506 Internally <methodname>setDefaultValue()</methodname> also converts the values
507 which can't be expressed in <acronym>PHP</acronym> into the value holder.
511 <sect3 id="zend.codegenerator.reference.concrete.php-property">
512 <title>Zend_CodeGenerator_Php_Property</title>
515 <classname>Zend_CodeGenerator_Php_Property</classname> describes a class
516 property, which may be either a constant or a variable. In each
517 case, the property may have an optional default value associated
518 with it. Additionally, the visibility of variable properties may
519 be set, per the parent class,
520 <classname>Zend_CodeGenerator_Php_Member_Abstract</classname>.
524 The <acronym>API</acronym> of the class is as follows:
527 <programlisting language="php"><![CDATA[
528 class Zend_CodeGenerator_Php_Property
529 extends Zend_CodeGenerator_Php_Member_Abstract
531 public static function fromReflection(
532 Zend_Reflection_Property $reflectionProperty
534 public function setConst($const)
535 public function isConst()
536 public function setDefaultValue($defaultValue)
537 public function getDefaultValue()
538 public function generate()