Zend_CodeGenerator-Examples.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect1 id="zend.codegenerator.examples">
  5. <title>Zend_CodeGeneratorサンプル</title>
  6. <example id="zend.codegenerator.examples.class">
  7. <title>PHPクラスを生成</title>
  8. <para>
  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' => '生成されたクラスサンプル',
  15. 'longDescription' => 'これは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. 上記のコードは下記の結果になります。:
  33. </para>
  34. <programlisting language="php"><![CDATA[
  35. /**
  36. * 生成されたクラスサンプル
  37. *
  38. * これは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>クラスのプロパティ付でPHPクラスを生成</title>
  51. <para>
  52. では、前の例を基にして、生成したクラスにプロパティを加えます。
  53. </para>
  54. <programlisting language="php"><![CDATA[
  55. $foo = new Zend_CodeGenerator_Php_Class();
  56. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  57. 'shortDescription' => '生成されたクラスサンプル',
  58. 'longDescription' => 'これはZend_CodeGeneratorで生成されたクラスです。',
  59. 'tags' => array(
  60. array(
  61. 'name' => 'version',
  62. 'description' => '$Rev:$',
  63. ),
  64. array(
  65. 'name' => 'license',
  66. 'description' => 'New BSD',
  67. ),
  68. ),
  69. ));
  70. $foo->setName('Foo')
  71. ->setDocblock($docblock)
  72. ->setProperties(array(
  73. array(
  74. 'name' => '_bar',
  75. 'visibility' => 'protected',
  76. 'defaultValue' => 'baz',
  77. ),
  78. array(
  79. 'name' => 'baz',
  80. 'visibility' => 'public',
  81. 'defaultValue' => 'bat',
  82. ),
  83. array(
  84. 'name' => 'bat',
  85. 'const' => true,
  86. 'defaultValue' => 'foobarbazbat',
  87. ),
  88. ));
  89. echo $foo->generate();
  90. ]]></programlisting>
  91. <para>
  92. 上記の結果は下記のクラス定義になります。:
  93. </para>
  94. <programlisting language="php"><![CDATA[
  95. /**
  96. * 生成されたクラスサンプル
  97. *
  98. * これはZend_CodeGeneratorで生成されたクラスです。
  99. *
  100. * @version $Rev:$
  101. * @license New BSD
  102. *
  103. */
  104. class Foo
  105. {
  106. protected $_bar = 'baz';
  107. public $baz = 'bat';
  108. const bat = 'foobarbazbat';
  109. }
  110. ]]></programlisting>
  111. </example>
  112. <example id="zend.codegenerator.examples.class-methods">
  113. <title>クラスのメソッド付でPHPクラスを生成</title>
  114. <para>
  115. <classname>Zend_CodeGenerator_Php_Class</classname>のおかげで、
  116. クラスにオプションのコンテンツと一緒にメソッドを付与できます。
  117. メソッドは、配列かまたは具体的な<classname>Zend_CodeGenerator_Php_Method</classname>インスタンスとして付与されるかもしれません。
  118. </para>
  119. <programlisting language="php"><![CDATA[
  120. $foo = new Zend_CodeGenerator_Php_Class();
  121. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  122. 'shortDescription' => '生成されたクラスサンプル',
  123. 'longDescription' => 'これはZend_CodeGeneratorで生成されたクラスです。',
  124. 'tags' => array(
  125. array(
  126. 'name' => 'version',
  127. 'description' => '$Rev:$',
  128. ),
  129. array(
  130. 'name' => 'license',
  131. 'description' => 'New BSD',
  132. ),
  133. ),
  134. ));
  135. $foo->setName('Foo')
  136. ->setDocblock($docblock)
  137. ->setProperties(array(
  138. array(
  139. 'name' => '_bar',
  140. 'visibility' => 'protected',
  141. 'defaultValue' => 'baz',
  142. ),
  143. array(
  144. 'name' => 'baz',
  145. 'visibility' => 'public',
  146. 'defaultValue' => 'bat',
  147. ),
  148. array(
  149. 'name' => 'bat',
  150. 'const' => true,
  151. 'defaultValue' => 'foobarbazbat',
  152. ),
  153. ))
  154. ->setMethods(array(
  155. // メソッドは配列として渡されます
  156. array(
  157. 'name' => 'setBar',
  158. 'parameters' => array(
  159. array('name' => 'bar'),
  160. ),
  161. 'body' => '$this->_bar = $bar;' . "\n" . 'return $this;',
  162. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  163. 'shortDescription' => 'barプロパティーを設定',
  164. 'tags' => array(
  165. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  166. 'paramName' => 'bar',
  167. 'datatype' => 'string'
  168. )),
  169. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  170. 'datatype' => 'string',
  171. )),
  172. ),
  173. )),
  174. ),
  175. // メソッドは具体的なインスタンスとして渡されます
  176. new Zend_CodeGenerator_Php_Method(array(
  177. 'name' => 'getBar',
  178. 'body' => 'return $this->_bar;',
  179. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  180. 'shortDescription' => 'barプロパティーを取得',
  181. 'tags' => array(
  182. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  183. 'datatype' => 'string|null',
  184. )),
  185. ),
  186. )),
  187. )),
  188. ));
  189. echo $foo->generate();
  190. ]]></programlisting>
  191. <para>
  192. 上記のコードは下記の出力になります。:
  193. </para>
  194. <programlisting language="php"><![CDATA[
  195. /**
  196. * 生成されたクラスサンプル
  197. *
  198. * これはZend_CodeGeneratorで生成されたクラスです。
  199. *
  200. * @version $Rev:$
  201. * @license New BSD
  202. */
  203. class Foo
  204. {
  205. protected $_bar = 'baz';
  206. public $baz = 'bat';
  207. const bat = 'foobarbazbat';
  208. /**
  209. * barプロパティーを設定
  210. *
  211. * @param string bar
  212. * @return string
  213. */
  214. public function setBar($bar)
  215. {
  216. $this->_bar = $bar;
  217. return $this;
  218. }
  219. /**
  220. * barプロパティーを取得
  221. *
  222. * @return string|null
  223. */
  224. public function getBar()
  225. {
  226. return $this->_bar;
  227. }
  228. }
  229. ]]></programlisting>
  230. </example>
  231. <example id="zend.codegenerator.examples.file">
  232. <title>PHPファイルの生成</title>
  233. <para>
  234. <classname>Zend_CodeGenerator_Php_File</classname>は<acronym>PHP</acronym>ファイルのコンテンツ生成でも使えます。
  235. あなたは、任意のコンテンツ本体だけでなくクラスを含めることができます。
  236. クラスを付与するとき、具体的な<classname>Zend_CodeGenerator_Php_Class</classname>インスタンスか、
  237. またはクラスを定めている配列を添付しなければなりません。
  238. </para>
  239. <para>
  240. 下記の例では、前述の例のクラス定義の1つにつき<varname>$foo</varname>を定義したと仮定します。
  241. </para>
  242. <programlisting language="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クラスファイル',
  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. <methodname>generate()</methodname>を呼び出すとコードを生成します。
  259. しかし、ファイルに書き出しません。
  260. コンテンツを捕まえて、自分自身で書き出す必要があります。
  261. その例です。:
  262. </para>
  263. <programlisting language="php"><![CDATA[
  264. $code = $file->generate();
  265. file_put_contents('Foo.php', $code);
  266. ]]></programlisting>
  267. <para>
  268. 上記は下記のファイルを生成します:
  269. </para>
  270. <programlisting language="php"><![CDATA[
  271. <?php
  272. /**
  273. * Fooクラスファイル
  274. *
  275. * @license New BSD
  276. */
  277. /**
  278. * 生成されたクラスサンプル
  279. *
  280. * これはZend_CodeGeneratorで生成されたクラスです。
  281. *
  282. * @version $Rev:$
  283. * @license New BSD
  284. */
  285. class Foo
  286. {
  287. protected $_bar = 'baz';
  288. public $baz = 'bat';
  289. const bat = 'foobarbazbat';
  290. /**
  291. * barプロパティーを設定
  292. *
  293. * @param string bar
  294. * @return string
  295. */
  296. public function setBar($bar)
  297. {
  298. $this->_bar = $bar;
  299. return $this;
  300. }
  301. /**
  302. * barプロパティーを取得
  303. *
  304. * @return string|null
  305. */
  306. public function getBar()
  307. {
  308. return $this->_bar;
  309. }
  310. }
  311. define('APPLICATION_ENV', 'testing');
  312. ]]></programlisting>
  313. </example>
  314. <example id="zend.codegenerator.examples.reflection-file">
  315. <title>reflection経由のPHPファイルのコード生成の種まき</title>
  316. <para>
  317. コード・ジェネレーターを使って、
  318. 既存の<acronym>PHP</acronym>ファイルに<acronym>PHP</acronym>コードを加えることができます。
  319. そうするためには、まずそれにたいしてreflectionを実行する必要があります。
  320. 静的メソッド<methodname>fromReflectedFileName()</methodname>によりこれを実行できます。
  321. </para>
  322. <programlisting language="php"><![CDATA[
  323. $generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path);
  324. $body = $generator->getBody();
  325. $body .= "\n\$foo->bar();";
  326. file_put_contents($path, $generator->generate());
  327. ]]></programlisting>
  328. </example>
  329. <example id="zend.codegenerator.examples.reflection-class">
  330. <title>reflection経由のPHPクラス生成の種まき</title>
  331. <para>
  332. コード・ジェネレーターを使って、既存のPHPファイルにPHPコードを加えることができます。
  333. そうするために、最初にクラスをジェネレーター・オブジェクトにマップするために、
  334. 静的メソッド<methodname>fromReflection()</methodname>を使ってください。
  335. そこから追加のプロパティまたはメソッドを加えて、そしてクラスを再生成するでしょう。
  336. </para>
  337. <programlisting language="php"><![CDATA[
  338. $generator = Zend_CodeGenerator_Php_Class::fromReflection(
  339. new Zend_Reflection_Class($class)
  340. );
  341. $generator->setMethod(array(
  342. 'name' => 'setBaz',
  343. 'parameters' => array(
  344. array('name' => 'baz'),
  345. ),
  346. 'body' => '$this->_baz = $baz;' . "\n" . 'return $this;',
  347. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  348. 'shortDescription' => 'bazプロパティーを設定',
  349. 'tags' => array(
  350. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  351. 'paramName' => 'baz',
  352. 'datatype' => 'string'
  353. )),
  354. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  355. 'datatype' => 'string',
  356. )),
  357. ),
  358. )),
  359. ));
  360. $code = $generator->generate();
  361. ]]></programlisting>
  362. </example>
  363. </sect1>