<?xml version="1.0" encoding="UTF-8"?>
<!-- EN-Revision: 24249 -->
<!-- Reviewed: no -->
<sect1 id="zend.codegenerator.examples">
    <title>Exemples Zend_CodeGenerator</title>

    <example id="zend.codegenerator.examples.class">
        <title>Génération de classes PHP</title>

        <para>
            L'exemple suivant génère le code d'une classe avec son bloc de commentaires PHPDoc.
        </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>Le résultat est&#160;:</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>Générer des classes PHP avec des attributs de classe</title>

        <para>
            Suivant l'exemple précédant, nous ajoutons maintenant des attributs à la
            classe.
        </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>Le résultat sera&#160;:</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>Générer des classes PHP avec des méthodes</title>

        <para>
            <classname>Zend_CodeGenerator_Php_Class</classname> vous permet d'attacher des
            méthodes à vos classes générées. L'attachement se fait soit par des tableaux, soit
            directement des objets <classname>Zend_CodeGenerator_Php_Method</classname>.
        </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>Le résultat sera&#160;:</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>Générer des fichiers PHP</title>

        <para>
            <classname>Zend_CodeGenerator_Php_File</classname> sert à générer le contenu de
            fichiers <acronym>PHP</acronym>. Il est possible d'insérer du code de classes,
            ou n'importe quel code. Si
            vous attachez des classes, vous pouvez les passer sous forme de tableaux ou directement
            d'objets <classname>Zend_CodeGenerator_Php_Class</classname>.
        </para>

        <para>
            Dans l'exemple suivant, nous supposons que vous avez défini <varname>$foo</varname>
            comme étant le code d'une des classes des exemples précédents.
        </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>
            L'appel à <methodname>generate()</methodname> va générer le code, mais pas l'écrire
            dans un fichier. Pour ce faire, il faudra d'abord capturer le contenu:
        </para>

        <programlisting language="php"><![CDATA[
$code = $file->generate();
file_put_contents('Foo.php', $code);
]]></programlisting>

        <para>Le résultat sera&#160;:</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>Ajouter du code à un fichier PHP existant en utilisant la réflexion</title>

        <para>
            Vous pouvez ajouter du code <acronym>PHP</acronym> à n'importe quel fichier
            <acronym>PHP</acronym> existant à condition d'utiliser la réflexion sur celui-ci
            afin de l'analyser. La méthode <methodname>fromReflectedFileName()</methodname>
            va vous y aider.
        </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>Ajouter du code à une classe PHP existante en utilisant la réflexion</title>

        <para>
            Vous pouvez aussi ajouter du code à une classe existante.
            Utilisez <methodname>fromReflection()</methodname> pour transformer la classe en objet
            Reflection. Ajoutez ensuite des méthodes, des attributs, puis régénérez le code de la
            classe modifiée&#160;:
        </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>
