<?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>
