Zend_CodeGenerator-Examples.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.codegenerator.examples">
  4. <title>Exemples Zend_CodeGenerator</title>
  5. <example id="zend.codegenerator.examples.class">
  6. <title>Génération de classes PHP</title>
  7. <para>
  8. L'exemple suivant génère le code d'une classe avec son bloc de commentaires PHPDoc.
  9. </para>
  10. <programlisting role="php"><![CDATA[
  11. $foo = new Zend_CodeGenerator_Php_Class();
  12. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  13. 'shortDescription' => 'Sample generated class',
  14. 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
  15. 'tags' => array(
  16. array(
  17. 'name' => 'version',
  18. 'description' => '$Rev:$',
  19. ),
  20. array(
  21. 'name' => 'license',
  22. 'description' => 'New BSD',
  23. ),
  24. ),
  25. ));
  26. $foo->setName('Foo')
  27. ->setDocblock($docblock);
  28. echo $foo->generate();
  29. ]]></programlisting>
  30. <para>
  31. Le résultat est:
  32. </para>
  33. <programlisting role="php"><![CDATA[
  34. /**
  35. * Sample generated class
  36. *
  37. * This is a class generated with Zend_CodeGenerator.
  38. *
  39. * @version $Rev:$
  40. * @license New BSD
  41. *
  42. */
  43. class Foo
  44. {
  45. }
  46. ]]></programlisting>
  47. </example>
  48. <example id="zend.codegenerator.examples.class-properties">
  49. <title>Generer des classes PHP avec des attributs de classe</title>
  50. <para>
  51. Suivant l'exemple précédant, nous ajoutons maintenant des attributs à la classe.
  52. </para>
  53. <programlisting role="php"><![CDATA[
  54. $foo = new Zend_CodeGenerator_Php_Class();
  55. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  56. 'shortDescription' => 'Sample generated class',
  57. 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
  58. 'tags' => array(
  59. array(
  60. 'name' => 'version',
  61. 'description' => '$Rev:$',
  62. ),
  63. array(
  64. 'name' => 'license',
  65. 'description' => 'New BSD',
  66. ),
  67. ),
  68. ));
  69. $foo->setName('Foo')
  70. ->setDocblock($docblock)
  71. ->setProperties(array(
  72. array(
  73. 'name' => '_bar',
  74. 'visibility' => 'protected',
  75. 'defaultValue' => 'baz',
  76. ),
  77. array(
  78. 'name' => 'baz',
  79. 'visibility' => 'public',
  80. 'defaultValue' => 'bat',
  81. ),
  82. array(
  83. 'name' => 'bat',
  84. 'const' => true,
  85. 'defaultValue' => 'foobarbazbat',
  86. ),
  87. ));
  88. echo $foo->generate();
  89. ]]></programlisting>
  90. <para>
  91. Le résultat sera:
  92. </para>
  93. <programlisting role="php"><![CDATA[
  94. /**
  95. * Sample generated class
  96. *
  97. * This is a class generated with Zend_CodeGenerator.
  98. *
  99. * @version $Rev:$
  100. * @license New BSD
  101. *
  102. */
  103. class Foo
  104. {
  105. protected $_bar = 'baz';
  106. public $baz = 'bat';
  107. const bat = 'foobarbazbat';
  108. }
  109. ]]></programlisting>
  110. </example>
  111. <example id="zend.codegenerator.examples.class-methods">
  112. <title>Générer des classes PHP avec des méthodes</title>
  113. <para>
  114. <classname>Zend_CodeGenerator_Php_Class</classname> vous permet d'attacher des méthodes
  115. à vos classes générées. L'attachement se fait soit par des tableaux, soit directement des
  116. objets <classname>Zend_CodeGenerator_Php_Method</classname>.
  117. </para>
  118. <programlisting role="php"><![CDATA[
  119. $foo = new Zend_CodeGenerator_Php_Class();
  120. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  121. 'shortDescription' => 'Sample generated class',
  122. 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
  123. 'tags' => array(
  124. array(
  125. 'name' => 'version',
  126. 'description' => '$Rev:$',
  127. ),
  128. array(
  129. 'name' => 'license',
  130. 'description' => 'New BSD',
  131. ),
  132. ),
  133. ));
  134. $foo->setName('Foo')
  135. ->setDocblock($docblock)
  136. ->setProperties(array(
  137. array(
  138. 'name' => '_bar',
  139. 'visibility' => 'protected',
  140. 'defaultValue' => 'baz',
  141. ),
  142. array(
  143. 'name' => 'baz',
  144. 'visibility' => 'public',
  145. 'defaultValue' => 'bat',
  146. ),
  147. array(
  148. 'name' => 'bat',
  149. 'const' => true,
  150. 'defaultValue' => 'foobarbazbat',
  151. ),
  152. ))
  153. ->setMethods(array(
  154. // Method passed as array
  155. array(
  156. 'name' => 'setBar',
  157. 'parameters' => array(
  158. array('name' => 'bar'),
  159. ),
  160. 'body' => '$this->_bar = $bar;' . "\n" . 'return $this;',
  161. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  162. 'shortDescription' => 'Set the bar property',
  163. 'tags' => array(
  164. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  165. 'paramName' => 'bar',
  166. 'datatype' => 'string'
  167. )),
  168. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  169. 'datatype' => 'string',
  170. )),
  171. ),
  172. )),
  173. ),
  174. // Method passed as concrete instance
  175. new Zend_CodeGenerator_Php_Method(array(
  176. 'name' => 'getBar',
  177. 'body' => 'return $this->_bar;',
  178. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  179. 'shortDescription' => 'Retrieve the bar property',
  180. 'tags' => array(
  181. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  182. 'datatype' => 'string|null',
  183. )),
  184. ),
  185. )),
  186. )),
  187. ));
  188. echo $foo->generate();
  189. ]]></programlisting>
  190. <para>
  191. Le résultat sera:
  192. </para>
  193. <programlisting role="php"><![CDATA[
  194. /**
  195. * Sample generated class
  196. *
  197. * This is a class generated with Zend_CodeGenerator.
  198. *
  199. * @version $Rev:$
  200. * @license New BSD
  201. */
  202. class Foo
  203. {
  204. protected $_bar = 'baz';
  205. public $baz = 'bat';
  206. const bat = 'foobarbazbat';
  207. /**
  208. * Set the bar property
  209. *
  210. * @param string bar
  211. * @return string
  212. */
  213. public function setBar($bar)
  214. {
  215. $this->_bar = $bar;
  216. return $this;
  217. }
  218. /**
  219. * Retrieve the bar property
  220. *
  221. * @return string|null
  222. */
  223. public function getBar()
  224. {
  225. return $this->_bar;
  226. }
  227. }
  228. ]]></programlisting>
  229. </example>
  230. <example id="zend.codegenerator.examples.file">
  231. <title>Générer des fichiers PHP</title>
  232. <para>
  233. <classname>Zend_CodeGenerator_Php_File</classname> sert à générer le contenu de fichiers PHP.
  234. Il est possible d'insérer du code de classes, ou du code PHP banal. Si vous attachez des classes,
  235. vous pouvez les passer sous forme de tableaux ou directement d'objets
  236. <classname>Zend_CodeGenerator_Php_Class</classname>.
  237. </para>
  238. <para>
  239. Dans l'exemple suivant, nous supposonsque vous avez défini
  240. <code>$foo</code> comme étant le code d'une des classes des exemples précédants.
  241. </para>
  242. <programlisting role="php"><![CDATA[
  243. $file = new Zend_CodeGenerator_Php_File(array(
  244. 'classes' => array($foo);
  245. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  246. 'shortDescription' => 'Foo class file',
  247. 'tags' => array(
  248. array(
  249. 'name' => 'license',
  250. 'description' => 'New BSD',
  251. ),
  252. ),
  253. )),
  254. 'body' => 'define(\'APPLICATION_ENV\', \'testing\');',
  255. ));
  256. ]]></programlisting>
  257. <para>
  258. L'appel à <code>generate()</code> va générer le code, mais pas l'écrire dans un fichier.
  259. Pour ce faire, il faudra d'abord capturer le contenu:
  260. </para>
  261. <programlisting role="php"><![CDATA[
  262. $code = $file->generate();
  263. file_put_contents('Foo.php', $code);
  264. ]]></programlisting>
  265. <para>
  266. Le résultat sera:
  267. </para>
  268. <programlisting role="php"><![CDATA[
  269. <?php
  270. /**
  271. * Foo class file
  272. *
  273. * @license New BSD
  274. */
  275. /**
  276. * Sample generated class
  277. *
  278. * This is a class generated with Zend_CodeGenerator.
  279. *
  280. * @version $Rev:$
  281. * @license New BSD
  282. */
  283. class Foo
  284. {
  285. protected $_bar = 'baz';
  286. public $baz = 'bat';
  287. const bat = 'foobarbazbat';
  288. /**
  289. * Set the bar property
  290. *
  291. * @param string bar
  292. * @return string
  293. */
  294. public function setBar($bar)
  295. {
  296. $this->_bar = $bar;
  297. return $this;
  298. }
  299. /**
  300. * Retrieve the bar property
  301. *
  302. * @return string|null
  303. */
  304. public function getBar()
  305. {
  306. return $this->_bar;
  307. }
  308. }
  309. define('APPLICATION_ENV', 'testing');
  310. ]]></programlisting>
  311. </example>
  312. <example id="zend.codegenerator.examples.reflection-file">
  313. <title>Ajouter du code à un fichier PHP existant en utilisant la réflexion</title>
  314. <para>
  315. Vous pouvez ajouter du code à n'importe quel fichier PHP existant à condition
  316. d'utiliser la réflexion sur celui-ci afin de l'analyser. La méthode
  317. <code>fromReflectedFileName()</code> va vous y aider
  318. </para>
  319. <programlisting role="php"><![CDATA[
  320. $generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path);
  321. $body = $generator->getBody();
  322. $body .= "\n\$foo->bar();";
  323. file_put_contents($path, $generator->generate());
  324. ]]></programlisting>
  325. </example>
  326. <example id="zend.codegenerator.examples.reflection-class">
  327. <title>Ajouter du code à une classe PHP existante en utilisant la réflexion</title>
  328. <para>
  329. Vous pouvez aussi ajouter du code à une classe PHP existante. Utilisez
  330. <code>fromReflection()</code> pour transformer la classe en objet Reflection.
  331. Ajoutez ensuite des méthodes, des attributs, puis régénérez le code de la classe modifiée:
  332. </para>
  333. <programlisting role="php"><![CDATA[
  334. $generator = Zend_CodeGenerator_Php_Class::fromReflection(
  335. new Zend_Reflection_Class($class)
  336. );
  337. $generator->setMethod(array(
  338. 'name' => 'setBaz',
  339. 'parameters' => array(
  340. array('name' => 'baz'),
  341. ),
  342. 'body' => '$this->_baz = $baz;' . "\n" . 'return $this;',
  343. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  344. 'shortDescription' => 'Set the baz property',
  345. 'tags' => array(
  346. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  347. 'paramName' => 'baz',
  348. 'datatype' => 'string'
  349. )),
  350. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  351. 'datatype' => 'string',
  352. )),
  353. ),
  354. )),
  355. ));
  356. $code = $generator->generate();
  357. ]]></programlisting>
  358. </example>
  359. </sect1>