| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect1 id="zend.codegenerator.examples">
- <title>Zend_CodeGenerator Examples</title>
- <example id="zend.codegenerator.examples.class">
- <title>Generating PHP classes</title>
- <para>
- The following example generates an empty class with a class-level
- DocBlock.
- </para>
- <programlisting language="php"><![CDATA[
- $foo = new Zend_CodeGenerator_Php_Class();
- $docblock = new Zend_CodeGenerator_Php_Docblock(array(
- 'shortDescription' => 'Sample generated class',
- 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
- 'tags' => array(
- array(
- 'name' => 'version',
- 'description' => '$Rev:$',
- ),
- array(
- 'name' => 'license',
- 'description' => 'New BSD',
- ),
- ),
- ));
- $foo->setName('Foo')
- ->setDocblock($docblock);
- echo $foo->generate();
- ]]></programlisting>
- <para>
- The above code will result in the following:
- </para>
- <programlisting language="php"><![CDATA[
- /**
- * Sample generated class
- *
- * This is a class generated with Zend_CodeGenerator.
- *
- * @version $Rev:$
- * @license New BSD
- *
- */
- class Foo
- {
- }
- ]]></programlisting>
- </example>
- <example id="zend.codegenerator.examples.class-properties">
- <title>Generating PHP classes with class properties</title>
- <para>
- Building on the previous example, we now add properties to our
- generated class.
- </para>
- <programlisting language="php"><![CDATA[
- $foo = new Zend_CodeGenerator_Php_Class();
- $docblock = new Zend_CodeGenerator_Php_Docblock(array(
- 'shortDescription' => 'Sample generated class',
- 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
- 'tags' => array(
- array(
- 'name' => 'version',
- 'description' => '$Rev:$',
- ),
- array(
- 'name' => 'license',
- 'description' => 'New BSD',
- ),
- ),
- ));
- $foo->setName('Foo')
- ->setDocblock($docblock)
- ->setProperties(array(
- array(
- 'name' => '_bar',
- 'visibility' => 'protected',
- 'defaultValue' => 'baz',
- ),
- array(
- 'name' => 'baz',
- 'visibility' => 'public',
- 'defaultValue' => 'bat',
- ),
- array(
- 'name' => 'bat',
- 'const' => true,
- 'defaultValue' => 'foobarbazbat',
- ),
- ));
- echo $foo->generate();
- ]]></programlisting>
- <para>
- The above results in the following class definition:
- </para>
- <programlisting language="php"><![CDATA[
- /**
- * Sample generated class
- *
- * This is a class generated with Zend_CodeGenerator.
- *
- * @version $Rev:$
- * @license New BSD
- *
- */
- class Foo
- {
- protected $_bar = 'baz';
- public $baz = 'bat';
- const bat = 'foobarbazbat';
- }
- ]]></programlisting>
- </example>
- <example id="zend.codegenerator.examples.class-methods">
- <title>Generating PHP classes with class methods</title>
- <para>
- <classname>Zend_CodeGenerator_Php_Class</classname> allows you to attach
- methods with optional content to your classes. Methods may be
- attached as either arrays or concrete
- <classname>Zend_CodeGenerator_Php_Method</classname> instances.
- </para>
- <programlisting language="php"><![CDATA[
- $foo = new Zend_CodeGenerator_Php_Class();
- $docblock = new Zend_CodeGenerator_Php_Docblock(array(
- 'shortDescription' => 'Sample generated class',
- 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
- 'tags' => array(
- array(
- 'name' => 'version',
- 'description' => '$Rev:$',
- ),
- array(
- 'name' => 'license',
- 'description' => 'New BSD',
- ),
- ),
- ));
- $foo->setName('Foo')
- ->setDocblock($docblock)
- ->setProperties(array(
- array(
- 'name' => '_bar',
- 'visibility' => 'protected',
- 'defaultValue' => 'baz',
- ),
- array(
- 'name' => 'baz',
- 'visibility' => 'public',
- 'defaultValue' => 'bat',
- ),
- array(
- 'name' => 'bat',
- 'const' => true,
- 'defaultValue' => 'foobarbazbat',
- ),
- ))
- ->setMethods(array(
- // Method passed as array
- array(
- 'name' => 'setBar',
- 'parameters' => array(
- array('name' => 'bar'),
- ),
- 'body' => '$this->_bar = $bar;' . "\n" . 'return $this;',
- 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
- 'shortDescription' => 'Set the bar property',
- 'tags' => array(
- new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
- 'paramName' => 'bar',
- 'datatype' => 'string'
- )),
- new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
- 'datatype' => 'string',
- )),
- ),
- )),
- ),
- // Method passed as concrete instance
- new Zend_CodeGenerator_Php_Method(array(
- 'name' => 'getBar',
- 'body' => 'return $this->_bar;',
- 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
- 'shortDescription' => 'Retrieve the bar property',
- 'tags' => array(
- new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
- 'datatype' => 'string|null',
- )),
- ),
- )),
- )),
- ));
- echo $foo->generate();
- ]]></programlisting>
- <para>
- The above generates the following output:
- </para>
- <programlisting language="php"><![CDATA[
- /**
- * Sample generated class
- *
- * This is a class generated with Zend_CodeGenerator.
- *
- * @version $Rev:$
- * @license New BSD
- */
- class Foo
- {
- protected $_bar = 'baz';
- public $baz = 'bat';
- const bat = 'foobarbazbat';
- /**
- * Set the bar property
- *
- * @param string bar
- * @return string
- */
- public function setBar($bar)
- {
- $this->_bar = $bar;
- return $this;
- }
- /**
- * Retrieve the bar property
- *
- * @return string|null
- */
- public function getBar()
- {
- return $this->_bar;
- }
- }
- ]]></programlisting>
- </example>
- <example id="zend.codegenerator.examples.file">
- <title>Generating PHP files</title>
- <para>
- <classname>Zend_CodeGenerator_Php_File</classname> can be used to generate the
- contents of a <acronym>PHP</acronym> file. You can include classes as well as arbitrary
- content body. When attaching classes, you should attach either
- concrete <classname>Zend_CodeGenerator_Php_Class</classname> instances or an
- array defining the class.
- </para>
- <para>
- In the example below, we will assume you've defined
- <varname>$foo</varname> per one of the class definitions in a previous
- example.
- </para>
- <programlisting language="php"><![CDATA[
- $file = new Zend_CodeGenerator_Php_File(array(
- 'classes' => array($foo);
- 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
- 'shortDescription' => 'Foo class file',
- 'tags' => array(
- array(
- 'name' => 'license',
- 'description' => 'New BSD',
- ),
- ),
- )),
- 'body' => 'define(\'APPLICATION_ENV\', \'testing\');',
- ));
- ]]></programlisting>
- <para>
- Calling <methodname>generate()</methodname> 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:
- </para>
- <programlisting language="php"><![CDATA[
- $code = $file->generate();
- file_put_contents('Foo.php', $code);
- ]]></programlisting>
- <para>
- The above will generate the following file:
- </para>
- <programlisting language="php"><![CDATA[
- <?php
- /**
- * Foo class file
- *
- * @license New BSD
- */
- /**
- * Sample generated class
- *
- * This is a class generated with Zend_CodeGenerator.
- *
- * @version $Rev:$
- * @license New BSD
- */
- class Foo
- {
- protected $_bar = 'baz';
- public $baz = 'bat';
- const bat = 'foobarbazbat';
- /**
- * Set the bar property
- *
- * @param string bar
- * @return string
- */
- public function setBar($bar)
- {
- $this->_bar = $bar;
- return $this;
- }
- /**
- * Retrieve the bar property
- *
- * @return string|null
- */
- public function getBar()
- {
- return $this->_bar;
- }
- }
- define('APPLICATION_ENV', 'testing');
- ]]></programlisting>
- </example>
- <example id="zend.codegenerator.examples.reflection-file">
- <title>Seeding PHP file code generation via reflection</title>
- <para>
- You can add <acronym>PHP</acronym> code to an existing <acronym>PHP</acronym> file
- using the code generator. To do so, you need to first do reflection on it. The
- static method <methodname>fromReflectedFileName()</methodname> allows you to do
- this.
- </para>
- <programlisting language="php"><![CDATA[
- $generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path);
- $body = $generator->getBody();
- $body .= "\n\$foo->bar();";
- file_put_contents($path, $generator->generate());
- ]]></programlisting>
- </example>
- <example id="zend.codegenerator.examples.reflection-class">
- <title>Seeding PHP class generation via reflection</title>
- <para>
- You may add code to an existing class. To do so, first use the
- static <methodname>fromReflection()</methodname> method to map the class into a
- generator object. From there, you may add additional properties or
- methods, and then regenerate the class.
- </para>
- <programlisting language="php"><![CDATA[
- $generator = Zend_CodeGenerator_Php_Class::fromReflection(
- new Zend_Reflection_Class($class)
- );
- $generator->setMethod(array(
- 'name' => 'setBaz',
- 'parameters' => array(
- array('name' => 'baz'),
- ),
- 'body' => '$this->_baz = $baz;' . "\n" . 'return $this;',
- 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
- 'shortDescription' => 'Set the baz property',
- 'tags' => array(
- new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
- 'paramName' => 'baz',
- 'datatype' => 'string'
- )),
- new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
- 'datatype' => 'string',
- )),
- ),
- )),
- ));
- $code = $generator->generate();
- ]]></programlisting>
- </example>
- </sect1>
|