Zend_CodeGenerator-Reference.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.codegenerator.reference">
  4. <title>Zend_CodeGenerator Reference</title>
  5. <sect2 id="zend.codegenerator.reference.abstracts">
  6. <title>Abstract Classes and Interfaces</title>
  7. <sect3 id="zend.codegenerator.reference.abstracts.abstract">
  8. <title>Zend_CodeGenerator_Abstract</title>
  9. <para>
  10. The base class from which all CodeGenerator classes inherit
  11. provides the minimal functionality necessary. It's <acronym>API</acronym> is as
  12. follows:
  13. </para>
  14. <programlisting language="php"><![CDATA[
  15. abstract class Zend_CodeGenerator_Abstract
  16. {
  17. final public function __construct(Array $options = array())
  18. public function setOptions(Array $options)
  19. public function setSourceContent($sourceContent)
  20. public function getSourceContent()
  21. protected function _init()
  22. protected function _prepare()
  23. abstract public function generate();
  24. final public function __toString()
  25. }
  26. ]]></programlisting>
  27. <para>
  28. The constructor first calls <methodname>_init()</methodname> (which is left
  29. empty for the concrete extending class to implement), then
  30. passes the <varname>$options</varname> parameter to
  31. <methodname>setOptions()</methodname>, and finally calls
  32. <methodname>_prepare()</methodname> (again, to be implemented by an
  33. extending class).
  34. </para>
  35. <para>
  36. Like most classes in Zend Framework, <methodname>setOptions()</methodname>
  37. compares an option key to existing setters in the class, and
  38. passes the value on to that method if found.
  39. </para>
  40. <para>
  41. <methodname>__toString()</methodname> is marked as final, and proxies to
  42. <methodname>generate()</methodname>.
  43. </para>
  44. <para>
  45. <methodname>setSourceContent()</methodname> and
  46. <methodname>getSourceContent()</methodname> are intended to either set the
  47. default content for the code being generated, or to replace said
  48. content once all generation tasks are complete.
  49. </para>
  50. </sect3>
  51. <sect3 id="zend.codegenerator.reference.abstracts.php-abstract">
  52. <title>Zend_CodeGenerator_Php_Abstract</title>
  53. <para>
  54. <classname>Zend_CodeGenerator_Php_Abstract</classname> extends
  55. <classname>Zend_CodeGenerator_Abstract</classname>, and adds some
  56. properties for tracking whether content has changed as well as
  57. the amount of indentation that should appear before generated
  58. content. Its <acronym>API</acronym> is as follows:
  59. </para>
  60. <programlisting language="php"><![CDATA[
  61. abstract class Zend_CodeGenerator_Php_Abstract
  62. extends Zend_CodeGenerator_Abstract
  63. {
  64. public function setSourceDirty($isSourceDirty = true)
  65. public function isSourceDirty()
  66. public function setIndentation($indentation)
  67. public function getIndentation()
  68. }
  69. ]]></programlisting>
  70. </sect3>
  71. <sect3 id="zend.codegenerator.reference.abstracts.php-member-abstract">
  72. <title>Zend_CodeGenerator_Php_Member_Abstract</title>
  73. <para>
  74. <classname>Zend_CodeGenerator_Php_Member_Abstract</classname> is a base
  75. class for generating class members -- properties and methods --
  76. and provides accessors and mutators for establishing visibility;
  77. whether or not the member is abstract, static, or final; and the
  78. name of the member. Its <acronym>API</acronym> is as follows:
  79. </para>
  80. <programlisting language="php"><![CDATA[
  81. abstract class Zend_CodeGenerator_Php_Member_Abstract
  82. extends Zend_CodeGenerator_Php_Abstract
  83. {
  84. public function setAbstract($isAbstract)
  85. public function isAbstract()
  86. public function setStatic($isStatic)
  87. public function isStatic()
  88. public function setVisibility($visibility)
  89. public function getVisibility()
  90. public function setName($name)
  91. public function getName()
  92. }
  93. ]]></programlisting>
  94. </sect3>
  95. </sect2>
  96. <sect2 id="zend.codegenerator.reference.concrete">
  97. <title>Concrete CodeGenerator Classes</title>
  98. <sect3 id="zend.codegenerator.reference.concrete.php-body">
  99. <title>Zend_CodeGenerator_Php_Body</title>
  100. <para>
  101. <classname>Zend_CodeGenerator_Php_Body</classname> is intended for
  102. generating arbitrary procedural code to include within a file.
  103. As such, you simply set content for the object, and it will
  104. return that content when you invoke <methodname>generate()</methodname>.
  105. </para>
  106. <para>
  107. The <acronym>API</acronym> of the class is as follows:
  108. </para>
  109. <programlisting language="php"><![CDATA[
  110. class Zend_CodeGenerator_Php_Body extends Zend_CodeGenerator_Php_Abstract
  111. {
  112. public function setContent($content)
  113. public function getContent()
  114. public function generate()
  115. }
  116. ]]></programlisting>
  117. </sect3>
  118. <sect3 id="zend.codegenerator.reference.concrete.php-class">
  119. <title>Zend_CodeGenerator_Php_Class</title>
  120. <para>
  121. <classname>Zend_CodeGenerator_Php_Class</classname> is intended for
  122. generating <acronym>PHP</acronym> classes. The basic functionality just generates
  123. the <acronym>PHP</acronym> class itself, as well as optionally the related
  124. <acronym>PHP</acronym> DocBlock. Classes may implement or inherit from other
  125. classes, and may be marked as abstract. Utilizing other code generator
  126. classes, you can also attach class constants, properties, and
  127. methods.
  128. </para>
  129. <para>
  130. The <acronym>API</acronym> is as follows:
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
  134. {
  135. public static function fromReflection(
  136. Zend_Reflection_Class $reflectionClass
  137. )
  138. public function setDocblock(Zend_CodeGenerator_Php_Docblock $docblock)
  139. public function getDocblock()
  140. public function setName($name)
  141. public function getName()
  142. public function setAbstract($isAbstract)
  143. public function isAbstract()
  144. public function setExtendedClass($extendedClass)
  145. public function getExtendedClass()
  146. public function setImplementedInterfaces(Array $implementedInterfaces)
  147. public function getImplementedInterfaces()
  148. public function setProperties(Array $properties)
  149. public function setProperty($property)
  150. public function getProperties()
  151. public function getProperty($propertyName)
  152. public function setMethods(Array $methods)
  153. public function setMethod($method)
  154. public function getMethods()
  155. public function getMethod($methodName)
  156. public function hasMethod($methodName)
  157. public function isSourceDirty()
  158. public function generate()
  159. }
  160. ]]></programlisting>
  161. <para>
  162. The <methodname>setProperty()</methodname> method accepts an array of
  163. information that may be used to generate a
  164. <classname>Zend_CodeGenerator_Php_Property</classname> instance -- or
  165. simply an instance of
  166. <classname>Zend_CodeGenerator_Php_Property</classname>.
  167. Likewise, <methodname>setMethod()</methodname> accepts either an array of
  168. information for generating a
  169. <classname>Zend_CodeGenerator_Php_Method</classname> instance or a
  170. concrete instance of that class.
  171. </para>
  172. <para>
  173. Note that <methodname>setDocBlock()</methodname> expects an instance of
  174. <classname>Zend_CodeGenerator_Php_DocBlock</classname>.
  175. </para>
  176. </sect3>
  177. <sect3 id="zend.codegenerator.reference.concrete.php-docblock">
  178. <title>Zend_CodeGenerator_Php_Docblock</title>
  179. <para>
  180. <classname>Zend_CodeGenerator_Php_Docblock</classname> can be used to
  181. generate arbitrary <acronym>PHP</acronym> docblocks, including all the standard
  182. docblock features: short and long descriptions and annotation
  183. tags.
  184. </para>
  185. <para>
  186. Annotation tags may be set using the <methodname>setTag()</methodname> and
  187. <methodname>setTags()</methodname> methods; these each take either an array
  188. describing the tag that may be passed to the
  189. <classname>Zend_CodeGenerator_Php_Docblock_Tag</classname> constructor, or
  190. an instance of that class.
  191. </para>
  192. <para>
  193. The <acronym>API</acronym> is as follows:
  194. </para>
  195. <programlisting language="php"><![CDATA[
  196. class Zend_CodeGenerator_Php_Docblock extends Zend_CodeGenerator_Php_Abstract
  197. {
  198. public static function fromReflection(
  199. Zend_Reflection_Docblock $reflectionDocblock
  200. )
  201. public function setShortDescription($shortDescription)
  202. public function getShortDescription()
  203. public function setLongDescription($longDescription)
  204. public function getLongDescription()
  205. public function setTags(Array $tags)
  206. public function setTag($tag)
  207. public function getTags()
  208. public function generate()
  209. }
  210. ]]></programlisting>
  211. </sect3>
  212. <sect3 id="zend.codegenerator.reference.concrete.php-docblock-tag">
  213. <title>Zend_CodeGenerator_Php_Docblock_Tag</title>
  214. <para>
  215. <classname>Zend_CodeGenerator_Php_Docblock_Tag</classname> is intended for
  216. creating arbitrary annotation tags for inclusion in <acronym>PHP</acronym>
  217. docblocks. Tags are expected to contain a name (the portion
  218. immediately following the '@' symbol) and a description
  219. (everything following the tag name).
  220. </para>
  221. <para>
  222. The class <acronym>API</acronym> is as follows:
  223. </para>
  224. <programlisting language="php"><![CDATA[
  225. class Zend_CodeGenerator_Php_Docblock_Tag
  226. extends Zend_CodeGenerator_Php_Abstract
  227. {
  228. public static function fromReflection(
  229. Zend_Reflection_Docblock_Tag $reflectionTag
  230. )
  231. public function setName($name)
  232. public function getName()
  233. public function setDescription($description)
  234. public function getDescription()
  235. public function generate()
  236. }
  237. ]]></programlisting>
  238. </sect3>
  239. <sect3 id="zend.codegenerator.reference.concrete.php-docblock-tag-param">
  240. <title>Zend_CodeGenerator_Php_DocBlock_Tag_Param</title>
  241. <para>
  242. <classname>Zend_CodeGenerator_Php_DocBlock_Tag_Param</classname> is a
  243. specialized version of
  244. <classname>Zend_CodeGenerator_Php_DocBlock_Tag</classname>, and represents
  245. a method parameter. The tag name is therefor known ("param"),
  246. but due to the format of this annotation tag, additional
  247. information is required in order to generate it: the parameter
  248. name and data type it represents.
  249. </para>
  250. <para>
  251. The class <acronym>API</acronym> is as follows:
  252. </para>
  253. <programlisting language="php"><![CDATA[
  254. class Zend_CodeGenerator_Php_Docblock_Tag_Param
  255. extends Zend_CodeGenerator_Php_Docblock_Tag
  256. {
  257. public static function fromReflection(
  258. Zend_Reflection_Docblock_Tag $reflectionTagParam
  259. )
  260. public function setDatatype($datatype)
  261. public function getDatatype()
  262. public function setParamName($paramName)
  263. public function getParamName()
  264. public function generate()
  265. }
  266. ]]></programlisting>
  267. </sect3>
  268. <sect3 id="zend.codegenerator.reference.concrete.php-docblock-tag-return">
  269. <title>Zend_CodeGenerator_Php_DocBlock_Tag_Return</title>
  270. <para>
  271. Like the param docblock tag variant,
  272. <classname>Zend_CodeGenerator_Php_Docblock_Tab_Return</classname> is an
  273. annotation tag variant for representing a method return value.
  274. In this case, the annotation tag name is known ("return"), but
  275. requires a return type.
  276. </para>
  277. <para>
  278. The class <acronym>API</acronym> is as follows:
  279. </para>
  280. <programlisting language="php"><![CDATA[
  281. class Zend_CodeGenerator_Php_Docblock_Tag_Param
  282. extends Zend_CodeGenerator_Php_Docblock_Tag
  283. {
  284. public static function fromReflection(
  285. Zend_Reflection_Docblock_Tag $reflectionTagReturn
  286. )
  287. public function setDatatype($datatype)
  288. public function getDatatype()
  289. public function generate()
  290. }
  291. ]]></programlisting>
  292. </sect3>
  293. <sect3 id="zend.codegenerator.reference.concrete.php-file">
  294. <title>Zend_CodeGenerator_Php_File</title>
  295. <para>
  296. <classname>Zend_CodeGenerator_Php_File</classname> is used to generate
  297. the full contents of a file that will contain <acronym>PHP</acronym> code. The file
  298. may contain classes or arbitrary <acronym>PHP</acronym> code, as well as a
  299. file-level docblock if desired.
  300. </para>
  301. <para>
  302. When adding classes to the file, you will need to pass either an
  303. array of information to pass to the
  304. <classname>Zend_CodeGenerator_Php_Class</classname> constructor, or an
  305. instance of that class. Similarly, with docblocks, you will need
  306. to pass information for the
  307. <classname>Zend_CodeGenerator_Php_Docblock</classname> constructor to
  308. consume or an instance of the class.
  309. </para>
  310. <para>
  311. The <acronym>API</acronym> of the class is as follows:
  312. </para>
  313. <programlisting language="php"><![CDATA[
  314. class Zend_CodeGenerator_Php_File extends Zend_CodeGenerator_Php_Abstract
  315. {
  316. public static function fromReflectedFilePath(
  317. $filePath,
  318. $usePreviousCodeGeneratorIfItExists = true,
  319. $includeIfNotAlreadyIncluded = true)
  320. public static function fromReflection(Zend_Reflection_File $reflectionFile)
  321. public function setDocblock(Zend_CodeGenerator_Php_Docblock $docblock)
  322. public function getDocblock()
  323. public function setRequiredFiles($requiredFiles)
  324. public function getRequiredFiles()
  325. public function setClasses(Array $classes)
  326. public function getClass($name = null)
  327. public function setClass($class)
  328. public function setFilename($filename)
  329. public function getFilename()
  330. public function getClasses()
  331. public function setBody($body)
  332. public function getBody()
  333. public function isSourceDirty()
  334. public function generate()
  335. }
  336. ]]></programlisting>
  337. </sect3>
  338. <sect3 id="zend.codegenerator.reference.concrete.php-member-container">
  339. <title>Zend_CodeGenerator_Php_Member_Container</title>
  340. <para>
  341. <classname>Zend_CodeGenerator_Php_Member_Container</classname> is used
  342. internally by <classname>Zend_CodeGenerator_Php_Class</classname> to keep
  343. track of class members -- properties and methods alike. These
  344. are indexed by name, using the concrete instances of the members
  345. as values.
  346. </para>
  347. <para>
  348. The <acronym>API</acronym> of the class is as follows:
  349. </para>
  350. <programlisting language="php"><![CDATA[
  351. class Zend_CodeGenerator_Php_Member_Container extends ArrayObject
  352. {
  353. public function __construct($type = self::TYPE_PROPERTY)
  354. }
  355. ]]></programlisting>
  356. </sect3>
  357. <sect3 id="zend.codegenerator.reference.concrete.php-method">
  358. <title>Zend_CodeGenerator_Php_Method</title>
  359. <para>
  360. <classname>Zend_CodeGenerator_Php_Method</classname> describes a class
  361. method, and can generate both the code and the docblock for the
  362. method. The visibility and status as static,
  363. abstract, or final may be indicated, per its parent class,
  364. <classname>Zend_CodeGenerator_Php_Member_Abstract</classname>. Finally,
  365. the parameters and return value for the method may be specified.
  366. </para>
  367. <para>
  368. Parameters may be set using <methodname>setParameter()</methodname> or
  369. <methodname>setParameters()</methodname>. In each case, a parameter should
  370. either be an array of information to pass to the
  371. <classname>Zend_CodeGenerator_Php_Parameter</classname> constructor or an
  372. instance of that class.
  373. </para>
  374. <para>
  375. The <acronym>API</acronym> of the class is as follows:
  376. </para>
  377. <programlisting language="php"><![CDATA[
  378. class Zend_CodeGenerator_Php_Method
  379. extends Zend_CodeGenerator_Php_Member_Abstract
  380. {
  381. public static function fromReflection(
  382. Zend_Reflection_Method $reflectionMethod
  383. )
  384. public function setDocblock(Zend_CodeGenerator_Php_Docblock $docblock)
  385. public function getDocblock()
  386. public function setFinal($isFinal)
  387. public function setParameters(Array $parameters)
  388. public function setParameter($parameter)
  389. public function getParameters()
  390. public function setBody($body)
  391. public function getBody()
  392. public function generate()
  393. }
  394. ]]></programlisting>
  395. </sect3>
  396. <sect3 id="zend.codegenerator.reference.concrete.php-parameter">
  397. <title>Zend_CodeGenerator_Php_Parameter</title>
  398. <para>
  399. <classname>Zend_CodeGenerator_Php_Parameter</classname> may be used to
  400. specify method parameters. Each parameter may have a position
  401. (if unspecified, the order in which they are registered with the
  402. method will be used), a default value, and a data type; a
  403. parameter name is required.
  404. </para>
  405. <para>
  406. The <acronym>API</acronym> of the class is as follows:
  407. </para>
  408. <programlisting language="php"><![CDATA[
  409. class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
  410. {
  411. public static function fromReflection(
  412. Zend_Reflection_Parameter $reflectionParameter
  413. )
  414. public function setType($type)
  415. public function getType()
  416. public function setName($name)
  417. public function getName()
  418. public function setDefaultValue($defaultValue)
  419. public function getDefaultValue()
  420. public function setPosition($position)
  421. public function getPosition()
  422. public function getPassedByReference()
  423. public function setPassedByReference($passedByReference)
  424. public function generate()
  425. }
  426. ]]></programlisting>
  427. <para>
  428. There are several problems that might occur when trying to set
  429. <constant>NULL</constant>, booleans or arrays as default values. For this the value
  430. holder object <classname>Zend_CodeGenerator_Php_ParameterDefaultValue</classname>
  431. can be used, for example:
  432. </para>
  433. <programlisting language="php"><![CDATA[
  434. $parameter = new Zend_CodeGenerator_Php_Parameter();
  435. $parameter->setDefaultValue(
  436. new Zend_CodeGenerator_Php_Parameter_DefaultValue("null")
  437. );
  438. $parameter->setDefaultValue(
  439. new Zend_CodeGenerator_Php_Parameter_DefaultValue("array('foo', 'bar')")
  440. );
  441. ]]></programlisting>
  442. <para>
  443. Internally <methodname>setDefaultValue()</methodname> also converts the values
  444. which can't be expressed in <acronym>PHP</acronym> into the value holder.
  445. </para>
  446. </sect3>
  447. <sect3 id="zend.codegenerator.reference.concrete.php-property">
  448. <title>Zend_CodeGenerator_Php_Property</title>
  449. <para>
  450. <classname>Zend_CodeGenerator_Php_Property</classname> describes a class
  451. property, which may be either a constant or a variable. In each
  452. case, the property may have an optional default value associated
  453. with it. Additionally, the visibility of variable properties may
  454. be set, per the parent class,
  455. <classname>Zend_CodeGenerator_Php_Member_Abstract</classname>.
  456. </para>
  457. <para>
  458. The <acronym>API</acronym> of the class is as follows:
  459. </para>
  460. <programlisting language="php"><![CDATA[
  461. class Zend_CodeGenerator_Php_Property
  462. extends Zend_CodeGenerator_Php_Member_Abstract
  463. {
  464. public static function fromReflection(
  465. Zend_Reflection_Property $reflectionProperty
  466. )
  467. public function setConst($const)
  468. public function isConst()
  469. public function setDefaultValue($defaultValue)
  470. public function getDefaultValue()
  471. public function generate()
  472. }
  473. ]]></programlisting>
  474. </sect3>
  475. </sect2>
  476. </sect1>