Introduction

Zend_CodeGenerator provides facilities to generate arbitrary code using an object oriented interface, both to create new code as well as to update existing code. While the current implementation is limited to generating PHP code, you can easily extend the base class in order to provide code generation for other tasks: JavaScript, configuration files, apache vhosts, etc.

Theory of Operation

In the most typical use case, you will simply instantiate a code generator class and either pass it the appropriate configuration or configure it after instantiation. To generate the code, you will simply echo the object or call its generate() method.

  1. // Passing configuration to the constructor:
  2. $file = new Zend_CodeGenerator_Php_File(array(
  3.     'classes' => array(
  4.         new Zend_CodeGenerator_Php_Class(array(
  5.             'name'    => 'World',
  6.             'methods' => array(
  7.                 new Zend_CodeGenerator_Php_Method(array(
  8.                     'name' => 'hello',
  9.                     'body' => 'echo \'Hello world!\';',
  10.                 )),
  11.             ),
  12.         )),
  13.     )
  14. ));
  15.  
  16. // Configuring after instantiation
  17. $method = new Zend_CodeGenerator_Php_Method();
  18. $method->setName('hello')
  19.        ->setBody('echo \'Hello world!\';');
  20.  
  21. $class = new Zend_CodeGenerator_Php_Class();
  22. $class->setName('World')
  23.       ->setMethod($method);
  24.  
  25. $file = new Zend_CodeGenerator_Php_File();
  26. $file->setClass($class);
  27.  
  28. // Render the generated file
  29. echo $file;
  30.  
  31. // or write it to a file:
  32. file_put_contents('World.php', $file->generate());

Both of the above samples will render the same result:

  1. <?php
  2.  
  3. class World
  4. {
  5.  
  6.     public function hello()
  7.     {
  8.         echo 'Hello world!';
  9.     }
  10.  
  11. }

Another common use case is to update existing code -- for instance, to add a method to a class. In such a case, you must first inspect the existing code using reflection, and then add your new method. Zend_CodeGenerator makes this trivially simple, by leveraging Zend_Reflection.

As an example, let's say we've saved the above to the file "World.php", and have already included it. We could then do the following:

  1. $class = Zend_CodeGenerator_Php_Class::fromReflection(
  2.     new Zend_Reflection_Class('World')
  3. );
  4.  
  5. $method = new Zend_CodeGenerator_Php_Method();
  6. $method->setName('mrMcFeeley')
  7.        ->setBody('echo \'Hello, Mr. McFeeley!\';');
  8. $class->setMethod($method);
  9.  
  10. $file = new Zend_CodeGenerator_Php_File();
  11. $file->setClass($class);
  12.  
  13. // Render the generated file
  14. echo $file;
  15.  
  16. // Or, better yet, write it back to the original file:
  17. file_put_contents('World.php', $file->generate());

The resulting class file will now look like this:

  1. <?php
  2.  
  3. class World
  4. {
  5.  
  6.     public function hello()
  7.     {
  8.         echo 'Hello world!';
  9.     }
  10.  
  11.     public function mrMcFeeley()
  12.     {
  13.         echo 'Hellow Mr. McFeeley!';
  14.     }
  15.  
  16. }
blog comments powered by Disqus