Zend_CodeGenerator-Examples.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.codegenerator.examples">
  4. <title>Zend_CodeGenerator Examples</title>
  5. <example id="zend.codegenerator.examples.class">
  6. <title>Generating PHP classes</title>
  7. <para>
  8. The following example generates an empty class with a class-level
  9. DocBlock.
  10. </para>
  11. <programlisting language="php"><![CDATA[
  12. $foo = new Zend_CodeGenerator_Php_Class();
  13. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  14. 'shortDescription' => 'Sample generated class',
  15. 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
  16. 'tags' => array(
  17. array(
  18. 'name' => 'version',
  19. 'description' => '$Rev:$',
  20. ),
  21. array(
  22. 'name' => 'license',
  23. 'description' => 'New BSD',
  24. ),
  25. ),
  26. ));
  27. $foo->setName('Foo')
  28. ->setDocblock($docblock);
  29. echo $foo->generate();
  30. ]]></programlisting>
  31. <para>
  32. The above code will result in the following:
  33. </para>
  34. <programlisting language="php"><![CDATA[
  35. /**
  36. * Sample generated class
  37. *
  38. * This is a class generated with Zend_CodeGenerator.
  39. *
  40. * @version $Rev:$
  41. * @license New BSD
  42. *
  43. */
  44. class Foo
  45. {
  46. }
  47. ]]></programlisting>
  48. </example>
  49. <example id="zend.codegenerator.examples.class-properties">
  50. <title>Generating PHP classes with class properties</title>
  51. <para>
  52. Building on the previous example, we now add properties to our
  53. generated class.
  54. </para>
  55. <programlisting language="php"><![CDATA[
  56. $foo = new Zend_CodeGenerator_Php_Class();
  57. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  58. 'shortDescription' => 'Sample generated class',
  59. 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
  60. 'tags' => array(
  61. array(
  62. 'name' => 'version',
  63. 'description' => '$Rev:$',
  64. ),
  65. array(
  66. 'name' => 'license',
  67. 'description' => 'New BSD',
  68. ),
  69. ),
  70. ));
  71. $foo->setName('Foo')
  72. ->setDocblock($docblock)
  73. ->setProperties(array(
  74. array(
  75. 'name' => '_bar',
  76. 'visibility' => 'protected',
  77. 'defaultValue' => 'baz',
  78. ),
  79. array(
  80. 'name' => 'baz',
  81. 'visibility' => 'public',
  82. 'defaultValue' => 'bat',
  83. ),
  84. array(
  85. 'name' => 'bat',
  86. 'const' => true,
  87. 'defaultValue' => 'foobarbazbat',
  88. ),
  89. ));
  90. echo $foo->generate();
  91. ]]></programlisting>
  92. <para>
  93. The above results in the following class definition:
  94. </para>
  95. <programlisting language="php"><![CDATA[
  96. /**
  97. * Sample generated class
  98. *
  99. * This is a class generated with Zend_CodeGenerator.
  100. *
  101. * @version $Rev:$
  102. * @license New BSD
  103. *
  104. */
  105. class Foo
  106. {
  107. protected $_bar = 'baz';
  108. public $baz = 'bat';
  109. const bat = 'foobarbazbat';
  110. }
  111. ]]></programlisting>
  112. </example>
  113. <example id="zend.codegenerator.examples.class-methods">
  114. <title>Generating PHP classes with class methods</title>
  115. <para>
  116. <classname>Zend_CodeGenerator_Php_Class</classname> allows you to attach
  117. methods with optional content to your classes. Methods may be
  118. attached as either arrays or concrete
  119. <classname>Zend_CodeGenerator_Php_Method</classname> instances.
  120. </para>
  121. <programlisting language="php"><![CDATA[
  122. $foo = new Zend_CodeGenerator_Php_Class();
  123. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  124. 'shortDescription' => 'Sample generated class',
  125. 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
  126. 'tags' => array(
  127. array(
  128. 'name' => 'version',
  129. 'description' => '$Rev:$',
  130. ),
  131. array(
  132. 'name' => 'license',
  133. 'description' => 'New BSD',
  134. ),
  135. ),
  136. ));
  137. $foo->setName('Foo')
  138. ->setDocblock($docblock)
  139. ->setProperties(array(
  140. array(
  141. 'name' => '_bar',
  142. 'visibility' => 'protected',
  143. 'defaultValue' => 'baz',
  144. ),
  145. array(
  146. 'name' => 'baz',
  147. 'visibility' => 'public',
  148. 'defaultValue' => 'bat',
  149. ),
  150. array(
  151. 'name' => 'bat',
  152. 'const' => true,
  153. 'defaultValue' => 'foobarbazbat',
  154. ),
  155. ))
  156. ->setMethods(array(
  157. // Method passed as array
  158. array(
  159. 'name' => 'setBar',
  160. 'parameters' => array(
  161. array('name' => 'bar'),
  162. ),
  163. 'body' => '$this->_bar = $bar;' . "\n" . 'return $this;',
  164. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  165. 'shortDescription' => 'Set the bar property',
  166. 'tags' => array(
  167. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  168. 'paramName' => 'bar',
  169. 'datatype' => 'string'
  170. )),
  171. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  172. 'datatype' => 'string',
  173. )),
  174. ),
  175. )),
  176. ),
  177. // Method passed as concrete instance
  178. new Zend_CodeGenerator_Php_Method(array(
  179. 'name' => 'getBar',
  180. 'body' => 'return $this->_bar;',
  181. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  182. 'shortDescription' => 'Retrieve the bar property',
  183. 'tags' => array(
  184. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  185. 'datatype' => 'string|null',
  186. )),
  187. ),
  188. )),
  189. )),
  190. ));
  191. echo $foo->generate();
  192. ]]></programlisting>
  193. <para>
  194. The above generates the following output:
  195. </para>
  196. <programlisting language="php"><![CDATA[
  197. /**
  198. * Sample generated class
  199. *
  200. * This is a class generated with Zend_CodeGenerator.
  201. *
  202. * @version $Rev:$
  203. * @license New BSD
  204. */
  205. class Foo
  206. {
  207. protected $_bar = 'baz';
  208. public $baz = 'bat';
  209. const bat = 'foobarbazbat';
  210. /**
  211. * Set the bar property
  212. *
  213. * @param string bar
  214. * @return string
  215. */
  216. public function setBar($bar)
  217. {
  218. $this->_bar = $bar;
  219. return $this;
  220. }
  221. /**
  222. * Retrieve the bar property
  223. *
  224. * @return string|null
  225. */
  226. public function getBar()
  227. {
  228. return $this->_bar;
  229. }
  230. }
  231. ]]></programlisting>
  232. </example>
  233. <example id="zend.codegenerator.examples.file">
  234. <title>Generating PHP files</title>
  235. <para>
  236. <classname>Zend_CodeGenerator_Php_File</classname> can be used to generate the
  237. contents of a <acronym>PHP</acronym> file. You can include classes as well as arbitrary
  238. content body. When attaching classes, you should attach either
  239. concrete <classname>Zend_CodeGenerator_Php_Class</classname> instances or an
  240. array defining the class.
  241. </para>
  242. <para>
  243. In the example below, we will assume you've defined
  244. <varname>$foo</varname> per one of the class definitions in a previous
  245. example.
  246. </para>
  247. <programlisting language="php"><![CDATA[
  248. $file = new Zend_CodeGenerator_Php_File(array(
  249. 'classes' => array($foo);
  250. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  251. 'shortDescription' => 'Foo class file',
  252. 'tags' => array(
  253. array(
  254. 'name' => 'license',
  255. 'description' => 'New BSD',
  256. ),
  257. ),
  258. )),
  259. 'body' => 'define(\'APPLICATION_ENV\', \'testing\');',
  260. ));
  261. ]]></programlisting>
  262. <para>
  263. Calling <methodname>generate()</methodname> will generate the code -- but not
  264. write it to a file. You will need to capture the contents and write
  265. them to a file yourself. As an example:
  266. </para>
  267. <programlisting language="php"><![CDATA[
  268. $code = $file->generate();
  269. file_put_contents('Foo.php', $code);
  270. ]]></programlisting>
  271. <para>
  272. The above will generate the following file:
  273. </para>
  274. <programlisting language="php"><![CDATA[
  275. <?php
  276. /**
  277. * Foo class file
  278. *
  279. * @license New BSD
  280. */
  281. /**
  282. * Sample generated class
  283. *
  284. * This is a class generated with Zend_CodeGenerator.
  285. *
  286. * @version $Rev:$
  287. * @license New BSD
  288. */
  289. class Foo
  290. {
  291. protected $_bar = 'baz';
  292. public $baz = 'bat';
  293. const bat = 'foobarbazbat';
  294. /**
  295. * Set the bar property
  296. *
  297. * @param string bar
  298. * @return string
  299. */
  300. public function setBar($bar)
  301. {
  302. $this->_bar = $bar;
  303. return $this;
  304. }
  305. /**
  306. * Retrieve the bar property
  307. *
  308. * @return string|null
  309. */
  310. public function getBar()
  311. {
  312. return $this->_bar;
  313. }
  314. }
  315. define('APPLICATION_ENV', 'testing');
  316. ]]></programlisting>
  317. </example>
  318. <example id="zend.codegenerator.examples.reflection-file">
  319. <title>Seeding PHP file code generation via reflection</title>
  320. <para>
  321. You can add <acronym>PHP</acronym> code to an existing <acronym>PHP</acronym> file
  322. using the code generator. To do so, you need to first do reflection on it. The
  323. static method <methodname>fromReflectedFileName()</methodname> allows you to do
  324. this.
  325. </para>
  326. <programlisting language="php"><![CDATA[
  327. $generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path);
  328. $body = $generator->getBody();
  329. $body .= "\n\$foo->bar();";
  330. file_put_contents($path, $generator->generate());
  331. ]]></programlisting>
  332. </example>
  333. <example id="zend.codegenerator.examples.reflection-class">
  334. <title>Seeding PHP class generation via reflection</title>
  335. <para>
  336. You may add code to an existing class. To do so, first use the
  337. static <methodname>fromReflection()</methodname> method to map the class into a
  338. generator object. From there, you may add additional properties or
  339. methods, and then regenerate the class.
  340. </para>
  341. <programlisting language="php"><![CDATA[
  342. $generator = Zend_CodeGenerator_Php_Class::fromReflection(
  343. new Zend_Reflection_Class($class)
  344. );
  345. $generator->setMethod(array(
  346. 'name' => 'setBaz',
  347. 'parameters' => array(
  348. array('name' => 'baz'),
  349. ),
  350. 'body' => '$this->_baz = $baz;' . "\n" . 'return $this;',
  351. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  352. 'shortDescription' => 'Set the baz property',
  353. 'tags' => array(
  354. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  355. 'paramName' => 'baz',
  356. 'datatype' => 'string'
  357. )),
  358. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  359. 'datatype' => 'string',
  360. )),
  361. ),
  362. )),
  363. ));
  364. $code = $generator->generate();
  365. ]]></programlisting>
  366. </example>
  367. </sect1>