Introduction

Zend_CodeGenerator Examples

Example #1 Generating PHP classes

The following example generates an empty class with a class-level DocBlock.

  1. $foo      = new Zend_CodeGenerator_Php_Class();
  2. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  3.     'shortDescription' => 'Sample generated class',
  4.     'longDescription'  => 'This is a class generated with Zend_CodeGenerator.',
  5.     'tags'             => array(
  6.         array(
  7.             'name'        => 'version',
  8.             'description' => '$Rev:$',
  9.         ),
  10.         array(
  11.             'name'        => 'license',
  12.             'description' => 'New BSD',
  13.         ),
  14.     ),
  15. ));
  16. $foo->setName('Foo')
  17.     ->setDocblock($docblock);
  18. echo $foo->generate();

The above code will result in the following:

  1. /**
  2. * Sample generated class
  3. *
  4. * This is a class generated with Zend_CodeGenerator.
  5. *
  6. * @version $Rev:$
  7. * @license New BSD
  8. *
  9. */
  10. class Foo
  11. {
  12.  
  13. }

Example #2 Generating PHP classes with class properties

Building on the previous example, we now add properties to our generated class.

  1. $foo      = new Zend_CodeGenerator_Php_Class();
  2. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  3.     'shortDescription' => 'Sample generated class',
  4.     'longDescription'  => 'This is a class generated with Zend_CodeGenerator.',
  5.     'tags'             => array(
  6.         array(
  7.             'name'        => 'version',
  8.             'description' => '$Rev:$',
  9.         ),
  10.         array(
  11.             'name'        => 'license',
  12.             'description' => 'New BSD',
  13.         ),
  14.     ),
  15. ));
  16. $foo->setName('Foo')
  17.     ->setDocblock($docblock)
  18.     ->setProperties(array(
  19.         array(
  20.             'name'         => '_bar',
  21.             'visibility'   => 'protected',
  22.             'defaultValue' => 'baz',
  23.         ),
  24.         array(
  25.             'name'         => 'baz',
  26.             'visibility'   => 'public',
  27.             'defaultValue' => 'bat',
  28.         ),
  29.         array(
  30.             'name'         => 'bat',
  31.             'const'        => true,
  32.             'defaultValue' => 'foobarbazbat',
  33.         ),
  34.     ));
  35. echo $foo->generate();

The above results in the following class definition:

  1. /**
  2. * Sample generated class
  3. *
  4. * This is a class generated with Zend_CodeGenerator.
  5. *
  6. * @version $Rev:$
  7. * @license New BSD
  8. *
  9. */
  10. class Foo
  11. {
  12.  
  13.     protected $_bar = 'baz';
  14.  
  15.     public $baz = 'bat';
  16.  
  17.     const bat = 'foobarbazbat';
  18.  
  19. }

Example #3 Generating PHP classes with class methods

Zend_CodeGenerator_Php_Class allows you to attach methods with optional content to your classes. Methods may be attached as either arrays or concrete Zend_CodeGenerator_Php_Method instances.

  1. $foo      = new Zend_CodeGenerator_Php_Class();
  2. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  3.     'shortDescription' => 'Sample generated class',
  4.     'longDescription'  => 'This is a class generated with Zend_CodeGenerator.',
  5.     'tags'             => array(
  6.         array(
  7.             'name'        => 'version',
  8.             'description' => '$Rev:$',
  9.         ),
  10.         array(
  11.             'name'        => 'license',
  12.             'description' => 'New BSD',
  13.         ),
  14.     ),
  15. ));
  16. $foo->setName('Foo')
  17.     ->setDocblock($docblock)
  18.     ->setProperties(array(
  19.         array(
  20.             'name'         => '_bar',
  21.             'visibility'   => 'protected',
  22.             'defaultValue' => 'baz',
  23.         ),
  24.         array(
  25.             'name'         => 'baz',
  26.             'visibility'   => 'public',
  27.             'defaultValue' => 'bat',
  28.         ),
  29.         array(
  30.             'name'         => 'bat',
  31.             'const'        => true,
  32.             'defaultValue' => 'foobarbazbat',
  33.         ),
  34.     ))
  35.     ->setMethods(array(
  36.         // Method passed as array
  37.         array(
  38.             'name'       => 'setBar',
  39.             'parameters' => array(
  40.                 array('name' => 'bar'),
  41.             ),
  42.             'body'       => '$this->_bar = $bar;' . "\n" . 'return $this;',
  43.             'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
  44.                 'shortDescription' => 'Set the bar property',
  45.                 'tags'             => array(
  46.                     new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  47.                         'paramName' => 'bar',
  48.                         'datatype'  => 'string'
  49.                     )),
  50.                     new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  51.                         'datatype'  => 'string',
  52.                     )),
  53.                 ),
  54.             )),
  55.         ),
  56.         // Method passed as concrete instance
  57.         new Zend_CodeGenerator_Php_Method(array(
  58.             'name' => 'getBar',
  59.             'body'       => 'return $this->_bar;',
  60.             'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
  61.                 'shortDescription' => 'Retrieve the bar property',
  62.                 'tags'             => array(
  63.                     new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  64.                         'datatype'  => 'string|null',
  65.                     )),
  66.                 ),
  67.             )),
  68.         )),
  69.     ));
  70.  
  71. echo $foo->generate();

The above generates the following output:

  1. /**
  2. * Sample generated class
  3. *
  4. * This is a class generated with Zend_CodeGenerator.
  5. *
  6. * @version $Rev:$
  7. * @license New BSD
  8. */
  9. class Foo
  10. {
  11.  
  12.     protected $_bar = 'baz';
  13.  
  14.     public $baz = 'bat';
  15.  
  16.     const bat = 'foobarbazbat';
  17.  
  18.     /**
  19.      * Set the bar property
  20.      *
  21.      * @param string bar
  22.      * @return string
  23.      */
  24.     public function setBar($bar)
  25.     {
  26.         $this->_bar = $bar;
  27.         return $this;
  28.     }
  29.  
  30.     /**
  31.      * Retrieve the bar property
  32.      *
  33.      * @return string|null
  34.      */
  35.     public function getBar()
  36.     {
  37.         return $this->_bar;
  38.     }
  39.  
  40. }

Example #4 Generating PHP files

Zend_CodeGenerator_Php_File can be used to generate the contents of a PHP file. You can include classes as well as arbitrary content body. When attaching classes, you should attach either concrete Zend_CodeGenerator_Php_Class instances or an array defining the class.

In the example below, we will assume you've defined $foo per one of the class definitions in a previous example.

  1. $file = new Zend_CodeGenerator_Php_File(array(
  2.     'classes'  => array($foo);
  3.     'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  4.         'shortDescription' => 'Foo class file',
  5.         'tags'             => array(
  6.             array(
  7.                 'name'        => 'license',
  8.                 'description' => 'New BSD',
  9.             ),
  10.         ),
  11.     )),
  12.     'body'     => 'define(\'APPLICATION_ENV\', \'testing\');',
  13. ));

Calling generate() will generate the code -- but not write it to a file. You will need to capture the contents and write them to a file yourself. As an example:

  1. $code = $file->generate();
  2. file_put_contents('Foo.php', $code);

The above will generate the following file:

  1. <?php
  2. /**
  3. * Foo class file
  4. *
  5. * @license New BSD
  6. */
  7.  
  8. /**
  9. * Sample generated class
  10. *
  11. * This is a class generated with Zend_CodeGenerator.
  12. *
  13. * @version $Rev:$
  14. * @license New BSD
  15. */
  16. class Foo
  17. {
  18.  
  19.     protected $_bar = 'baz';
  20.  
  21.     public $baz = 'bat';
  22.  
  23.     const bat = 'foobarbazbat';
  24.  
  25.     /**
  26.      * Set the bar property
  27.      *
  28.      * @param string bar
  29.      * @return string
  30.      */
  31.     public function setBar($bar)
  32.     {
  33.         $this->_bar = $bar;
  34.         return $this;
  35.     }
  36.  
  37.     /**
  38.      * Retrieve the bar property
  39.      *
  40.      * @return string|null
  41.      */
  42.     public function getBar()
  43.     {
  44.         return $this->_bar;
  45.     }
  46.  
  47. }
  48.  
  49. define('APPLICATION_ENV', 'testing');

Example #5 Seeding PHP file code generation via reflection

You can add PHP code to an existing PHP file using the code generator. To do so, you need to first do reflection on it. The static method fromReflectedFileName() allows you to do this.

  1. $generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path);
  2. $body = $generator->getBody();
  3. $body .= "\n\$foo->bar();";
  4. file_put_contents($path, $generator->generate());

Example #6 Seeding PHP class generation via reflection

You may add code to an existing class. To do so, first use the static fromReflection() method to map the class into a generator object. From there, you may add additional properties or methods, and then regenerate the class.

  1. $generator = Zend_CodeGenerator_Php_Class::fromReflection(
  2.     new Zend_Reflection_Class($class)
  3. );
  4. $generator->setMethod(array(
  5.     'name'       => 'setBaz',
  6.     'parameters' => array(
  7.         array('name' => 'baz'),
  8.     ),
  9.     'body'       => '$this->_baz = $baz;' . "\n" . 'return $this;',
  10.     'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
  11.         'shortDescription' => 'Set the baz property',
  12.         'tags'             => array(
  13.             new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  14.                 'paramName' => 'baz',
  15.                 'datatype'  => 'string'
  16.             )),
  17.             new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  18.                 'datatype'  => 'string',
  19.             )),
  20.         ),
  21.     )),
  22. ));
  23. $code = $generator->generate();

Introduction