Zend_CodeGenerator-Introduction.xml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.codegenerator.introduction">
  4. <title>Introduction</title>
  5. <para>
  6. <classname>Zend_CodeGenerator</classname> provides facilities to generate
  7. arbitrary code using an object oriented interface, both to create new
  8. code as well as to update existing code. While the current
  9. implementation is limited to generating <acronym>PHP</acronym> code, you can easily extend
  10. the base class in order to provide code generation for other tasks:
  11. JavaScript, configuration files, apache vhosts, etc.
  12. </para>
  13. <sect2 id="zend.codegenerator.introduction.theory">
  14. <title>Theory of Operation</title>
  15. <para>
  16. In the most typical use case, you will simply instantiate a code
  17. generator class and either pass it the appropriate configuration or
  18. configure it after instantiation. To generate the code, you will
  19. simply echo the object or call its <methodname>generate()</methodname> method.
  20. </para>
  21. <programlisting language="php"><![CDATA[
  22. // Passing configuration to the constructor:
  23. $file = new Zend_CodeGenerator_Php_File(array(
  24. 'classes' => array(
  25. new Zend_CodeGenerator_Php_Class(array(
  26. 'name' => 'World',
  27. 'methods' => array(
  28. new Zend_CodeGenerator_Php_Method(array(
  29. 'name' => 'hello',
  30. 'body' => 'echo \'Hello world!\';',
  31. )),
  32. ),
  33. )),
  34. )
  35. ));
  36. // Configuring after instantiation
  37. $method = new Zend_CodeGenerator_Php_Method();
  38. $method->setName('hello')
  39. ->setBody('echo \'Hello world!\';');
  40. $class = new Zend_CodeGenerator_Php_Class();
  41. $class->setName('World')
  42. ->setMethod($method);
  43. $file = new Zend_CodeGenerator_Php_File();
  44. $file->setClass($class);
  45. // Render the generated file
  46. echo $file;
  47. // or write it to a file:
  48. file_put_contents('World.php', $file->generate());
  49. ]]></programlisting>
  50. <para>
  51. Both of the above samples will render the same result:
  52. </para>
  53. <programlisting language="php"><![CDATA[
  54. <?php
  55. class World
  56. {
  57. public function hello()
  58. {
  59. echo 'Hello world!';
  60. }
  61. }
  62. ]]></programlisting>
  63. <para>
  64. Another common use case is to update existing code -- for instance,
  65. to add a method to a class. In such a case, you must first inspect
  66. the existing code using reflection, and then add your new method.
  67. <classname>Zend_CodeGenerator</classname> makes this trivially simple, by
  68. leveraging <link linkend="zend.reflection">Zend_Reflection</link>.
  69. </para>
  70. <para>
  71. As an example, let's say we've saved the above to the file
  72. "<filename>World.php</filename>", and have already included it. We could then do the
  73. following:
  74. </para>
  75. <programlisting language="php"><![CDATA[
  76. $class = Zend_CodeGenerator_Php_Class::fromReflection(
  77. new Zend_Reflection_Class('World')
  78. );
  79. $method = new Zend_CodeGenerator_Php_Method();
  80. $method->setName('mrMcFeeley')
  81. ->setBody('echo \'Hello, Mr. McFeeley!\';');
  82. $class->setMethod($method);
  83. $file = new Zend_CodeGenerator_Php_File();
  84. $file->setClass($class);
  85. // Render the generated file
  86. echo $file;
  87. // Or, better yet, write it back to the original file:
  88. file_put_contents('World.php', $file->generate());
  89. ]]></programlisting>
  90. <para>
  91. The resulting class file will now look like this:
  92. </para>
  93. <programlisting language="php"><![CDATA[
  94. <?php
  95. class World
  96. {
  97. public function hello()
  98. {
  99. echo 'Hello world!';
  100. }
  101. public function mrMcFeeley()
  102. {
  103. echo 'Hellow Mr. McFeeley!';
  104. }
  105. }
  106. ]]></programlisting>
  107. </sect2>
  108. </sect1>