Documentation

Zend_CodeGenerator Reference - Zend_CodeGenerator

Zend_CodeGenerator Reference

Abstract Classes and Interfaces

Zend_CodeGenerator_Abstract

The base class from which all CodeGenerator classes inherit provides the minimal functionality necessary. It's API is as follows:

  1. abstract class Zend_CodeGenerator_Abstract
  2. {
  3.     final public function __construct(Array $options = array())
  4.     public function setOptions(Array $options)
  5.     public function setSourceContent($sourceContent)
  6.     public function getSourceContent()
  7.     protected function _init()
  8.     protected function _prepare()
  9.     abstract public function generate();
  10.     final public function __toString()
  11. }

The constructor first calls _init() (which is left empty for the concrete extending class to implement), then passes the $options parameter to setOptions(), and finally calls _prepare() (again, to be implemented by an extending class).

Like most classes in Zend Framework, setOptions() compares an option key to existing setters in the class, and passes the value on to that method if found.

__toString() is marked as final, and proxies to generate().

setSourceContent() and getSourceContent() are intended to either set the default content for the code being generated, or to replace said content once all generation tasks are complete.

Zend_CodeGenerator_Php_Abstract

Zend_CodeGenerator_Php_Abstract extends Zend_CodeGenerator_Abstract, and adds some properties for tracking whether content has changed as well as the amount of indentation that should appear before generated content. Its API is as follows:

  1. abstract class Zend_CodeGenerator_Php_Abstract
  2.     extends Zend_CodeGenerator_Abstract
  3. {
  4.     public function setSourceDirty($isSourceDirty = true)
  5.     public function isSourceDirty()
  6.     public function setIndentation($indentation)
  7.     public function getIndentation()
  8. }

Zend_CodeGenerator_Php_Member_Abstract

Zend_CodeGenerator_Php_Member_Abstract is a base class for generating class members -- properties and methods -- and provides accessors and mutators for establishing visibility; whether or not the member is abstract, static, or final; and the name of the member. Its API is as follows:

  1. abstract class Zend_CodeGenerator_Php_Member_Abstract
  2.     extends Zend_CodeGenerator_Php_Abstract
  3. {
  4.     public function setAbstract($isAbstract)
  5.     public function isAbstract()
  6.     public function setStatic($isStatic)
  7.     public function isStatic()
  8.     public function setVisibility($visibility)
  9.     public function getVisibility()
  10.     public function setName($name)
  11.     public function getName()
  12. }

Concrete CodeGenerator Classes

Zend_CodeGenerator_Php_Body

Zend_CodeGenerator_Php_Body is intended for generating arbitrary procedural code to include within a file. As such, you simply set content for the object, and it will return that content when you invoke generate().

The API of the class is as follows:

  1. class Zend_CodeGenerator_Php_Body extends Zend_CodeGenerator_Php_Abstract
  2. {
  3.     public function setContent($content)
  4.     public function getContent()
  5.     public function generate()
  6. }

Zend_CodeGenerator_Php_Class

Zend_CodeGenerator_Php_Class is intended for generating PHP classes. The basic functionality just generates the PHP class itself, as well as optionally the related PHP DocBlock. Classes may implement or inherit from other classes, and may be marked as abstract. Utilizing other code generator classes, you can also attach class constants, properties, and methods.

The API is as follows:

  1. class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
  2. {
  3.     public static function fromReflection(
  4.         Zend_Reflection_Class $reflectionClass
  5.     )
  6.     public function setDocblock(Zend_CodeGenerator_Php_Docblock $docblock)
  7.     public function getDocblock()
  8.     public function setName($name)
  9.     public function getName()
  10.     public function setAbstract($isAbstract)
  11.     public function isAbstract()
  12.     public function setExtendedClass($extendedClass)
  13.     public function getExtendedClass()
  14.     public function setImplementedInterfaces(Array $implementedInterfaces)
  15.     public function getImplementedInterfaces()
  16.     public function setProperties(Array $properties)
  17.     public function setProperty($property)
  18.     public function getProperties()
  19.     public function getProperty($propertyName)
  20.     public function setMethods(Array $methods)
  21.     public function setMethod($method)
  22.     public function getMethods()
  23.     public function getMethod($methodName)
  24.     public function hasMethod($methodName)
  25.     public function isSourceDirty()
  26.     public function generate()
  27. }

The setProperty() method accepts an array of information that may be used to generate a Zend_CodeGenerator_Php_Property instance -- or simply an instance of Zend_CodeGenerator_Php_Property. Likewise, setMethod() accepts either an array of information for generating a Zend_CodeGenerator_Php_Method instance or a concrete instance of that class.

Note that setDocBlock() expects an instance of Zend_CodeGenerator_Php_DocBlock.

Zend_CodeGenerator_Php_Docblock

Zend_CodeGenerator_Php_Docblock can be used to generate arbitrary PHP docblocks, including all the standard docblock features: short and long descriptions and annotation tags.

Annotation tags may be set using the setTag() and setTags() methods; these each take either an array describing the tag that may be passed to the Zend_CodeGenerator_Php_Docblock_Tag constructor, or an instance of that class.

The API is as follows:

  1. class Zend_CodeGenerator_Php_Docblock extends Zend_CodeGenerator_Php_Abstract
  2. {
  3.     public static function fromReflection(
  4.         Zend_Reflection_Docblock $reflectionDocblock
  5.     )
  6.     public function setShortDescription($shortDescription)
  7.     public function getShortDescription()
  8.     public function setLongDescription($longDescription)
  9.     public function getLongDescription()
  10.     public function setTags(Array $tags)
  11.     public function setTag($tag)
  12.     public function getTags()
  13.     public function generate()
  14. }

Zend_CodeGenerator_Php_Docblock_Tag

Zend_CodeGenerator_Php_Docblock_Tag is intended for creating arbitrary annotation tags for inclusion in PHP docblocks. Tags are expected to contain a name (the portion immediately following the '@' symbol) and a description (everything following the tag name).

The class API is as follows:

  1. class Zend_CodeGenerator_Php_Docblock_Tag
  2.     extends Zend_CodeGenerator_Php_Abstract
  3. {
  4.     public static function fromReflection(
  5.         Zend_Reflection_Docblock_Tag $reflectionTag
  6.     )
  7.     public function setName($name)
  8.     public function getName()
  9.     public function setDescription($description)
  10.     public function getDescription()
  11.     public function generate()
  12. }

Zend_CodeGenerator_Php_DocBlock_Tag_Param

Zend_CodeGenerator_Php_DocBlock_Tag_Param is a specialized version of Zend_CodeGenerator_Php_DocBlock_Tag, and represents a method parameter. The tag name is therefor known ("param"), but due to the format of this annotation tag, additional information is required in order to generate it: the parameter name and data type it represents.

The class API is as follows:

  1. class Zend_CodeGenerator_Php_Docblock_Tag_Param
  2.     extends Zend_CodeGenerator_Php_Docblock_Tag
  3. {
  4.     public static function fromReflection(
  5.         Zend_Reflection_Docblock_Tag $reflectionTagParam
  6.     )
  7.     public function setDatatype($datatype)
  8.     public function getDatatype()
  9.     public function setParamName($paramName)
  10.     public function getParamName()
  11.     public function generate()
  12. }

Zend_CodeGenerator_Php_DocBlock_Tag_Return

Like the param docblock tag variant, Zend_CodeGenerator_Php_Docblock_Tab_Return is an annotation tag variant for representing a method return value. In this case, the annotation tag name is known ("return"), but requires a return type.

The class API is as follows:

  1. class Zend_CodeGenerator_Php_Docblock_Tag_Param
  2.     extends Zend_CodeGenerator_Php_Docblock_Tag
  3. {
  4.     public static function fromReflection(
  5.         Zend_Reflection_Docblock_Tag $reflectionTagReturn
  6.     )
  7.     public function setDatatype($datatype)
  8.     public function getDatatype()
  9.     public function generate()
  10. }

Zend_CodeGenerator_Php_File

Zend_CodeGenerator_Php_File is used to generate the full contents of a file that will contain PHP code. The file may contain classes or arbitrary PHP code, as well as a file-level docblock if desired.

When adding classes to the file, you will need to pass either an array of information to pass to the Zend_CodeGenerator_Php_Class constructor, or an instance of that class. Similarly, with docblocks, you will need to pass information for the Zend_CodeGenerator_Php_Docblock constructor to consume or an instance of the class.

The API of the class is as follows:

  1. class Zend_CodeGenerator_Php_File extends Zend_CodeGenerator_Php_Abstract
  2. {
  3.     public static function fromReflectedFilePath(
  4.         $filePath,
  5.         $usePreviousCodeGeneratorIfItExists = true,
  6.         $includeIfNotAlreadyIncluded = true)
  7.     public static function fromReflection(Zend_Reflection_File $reflectionFile)
  8.     public function setDocblock(Zend_CodeGenerator_Php_Docblock $docblock)
  9.     public function getDocblock()
  10.     public function setRequiredFiles($requiredFiles)
  11.     public function getRequiredFiles()
  12.     public function setClasses(Array $classes)
  13.     public function getClass($name = null)
  14.     public function setClass($class)
  15.     public function setFilename($filename)
  16.     public function getFilename()
  17.     public function getClasses()
  18.     public function setBody($body)
  19.     public function getBody()
  20.     public function isSourceDirty()
  21.     public function generate()
  22. }

Zend_CodeGenerator_Php_Member_Container

Zend_CodeGenerator_Php_Member_Container is used internally by Zend_CodeGenerator_Php_Class to keep track of class members -- properties and methods alike. These are indexed by name, using the concrete instances of the members as values.

The API of the class is as follows:

  1. class Zend_CodeGenerator_Php_Member_Container extends ArrayObject
  2. {
  3.     public function __construct($type = self::TYPE_PROPERTY)
  4. }

Zend_CodeGenerator_Php_Method

Zend_CodeGenerator_Php_Method describes a class method, and can generate both the code and the docblock for the method. The visibility and status as static, abstract, or final may be indicated, per its parent class, Zend_CodeGenerator_Php_Member_Abstract. Finally, the parameters and return value for the method may be specified.

Parameters may be set using setParameter() or setParameters(). In each case, a parameter should either be an array of information to pass to the Zend_CodeGenerator_Php_Parameter constructor or an instance of that class.

The API of the class is as follows:

  1. class Zend_CodeGenerator_Php_Method
  2.     extends Zend_CodeGenerator_Php_Member_Abstract
  3. {
  4.     public static function fromReflection(
  5.         Zend_Reflection_Method $reflectionMethod
  6.     )
  7.     public function setDocblock(Zend_CodeGenerator_Php_Docblock $docblock)
  8.     public function getDocblock()
  9.     public function setFinal($isFinal)
  10.     public function setParameters(Array $parameters)
  11.     public function setParameter($parameter)
  12.     public function getParameters()
  13.     public function setBody($body)
  14.     public function getBody()
  15.     public function generate()
  16. }

Zend_CodeGenerator_Php_Parameter

Zend_CodeGenerator_Php_Parameter may be used to specify method parameters. Each parameter may have a position (if unspecified, the order in which they are registered with the method will be used), a default value, and a data type; a parameter name is required.

The API of the class is as follows:

  1. class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
  2. {
  3.     public static function fromReflection(
  4.         Zend_Reflection_Parameter $reflectionParameter
  5.     )
  6.     public function setType($type)
  7.     public function getType()
  8.     public function setName($name)
  9.     public function getName()
  10.     public function setDefaultValue($defaultValue)
  11.     public function getDefaultValue()
  12.     public function setPosition($position)
  13.     public function getPosition()
  14.     public function getPassedByReference()
  15.     public function setPassedByReference($passedByReference)
  16.     public function generate()
  17. }

There are several problems that might occur when trying to set NULL, booleans or arrays as default values. For this the value holder object Zend_CodeGenerator_Php_ParameterDefaultValue can be used, for example:

  1. $parameter = new Zend_CodeGenerator_Php_Parameter();
  2. $parameter->setDefaultValue(
  3.     new Zend_CodeGenerator_Php_Parameter_DefaultValue("null")
  4. );
  5. $parameter->setDefaultValue(
  6.     new Zend_CodeGenerator_Php_Parameter_DefaultValue("array('foo', 'bar')")
  7. );

Internally setDefaultValue() also converts the values which can't be expressed in PHP into the value holder.

Zend_CodeGenerator_Php_Property

Zend_CodeGenerator_Php_Property describes a class property, which may be either a constant or a variable. In each case, the property may have an optional default value associated with it. Additionally, the visibility of variable properties may be set, per the parent class, Zend_CodeGenerator_Php_Member_Abstract.

The API of the class is as follows:

  1. class Zend_CodeGenerator_Php_Property
  2.     extends Zend_CodeGenerator_Php_Member_Abstract
  3. {
  4.     public static function fromReflection(
  5.         Zend_Reflection_Property $reflectionProperty
  6.     )
  7.     public function setConst($const)
  8.     public function isConst()
  9.     public function setDefaultValue($defaultValue)
  10.     public function getDefaultValue()
  11.     public function generate()
  12. }

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts