Browse Source

[translation]ja:codegenerator_example

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@15306 44c647ce-9c0f-0410-b52a-842ac1e357ba
yoshida@zend.co.jp 16 years ago
parent
commit
f2f476b8c7
1 changed files with 421 additions and 0 deletions
  1. 421 0
      documentation/manual/ja/module_specs/Zend_CodeGenerator-Examples.xml

+ 421 - 0
documentation/manual/ja/module_specs/Zend_CodeGenerator-Examples.xml

@@ -0,0 +1,421 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Reviewed: no -->
+<!-- EN-Revision: 15190 -->
+<sect1 id="zend.codegenerator.examples">
+    <title>Zend_CodeGeneratorサンプル</title>
+
+    <example id="zend.codegenerator.examples.class">
+        <title>PHPクラスを生成</title>
+
+        <para>
+            下記の例ではクラスレベルのDocBlock付きで空のクラスを生成します。
+        </para>
+
+        <programlisting role="php"><![CDATA[
+$foo      = new Zend_CodeGenerator_Php_Class();
+$docblock = new Zend_CodeGenerator_Php_Docblock(array(
+    'shortDescription' => '生成されたクラスサンプル',
+    'longDescription'  => 'これは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>
+            上記のコードは下記の結果になります。:
+        </para>
+
+            <programlisting role="php"><![CDATA[
+/**
+ * 生成されたクラスサンプル
+ *
+ * これはZend_CodeGeneratorで生成されたクラスです。
+ *
+ * @version $Rev:$
+ * @license New BSD
+ *
+ */
+class Foo
+{
+
+
+}
+]]></programlisting>
+    </example>
+
+    <example id="zend.codegenerator.examples.class-properties">
+        <title>クラスのプロパティ付でPHPクラスを生成</title>
+
+        <para>
+            では、前の例を基にして、生成したクラスにプロパティを加えます。
+        </para>
+
+        <programlisting role="php"><![CDATA[
+$foo      = new Zend_CodeGenerator_Php_Class();
+$docblock = new Zend_CodeGenerator_Php_Docblock(array(
+    'shortDescription' => '生成されたクラスサンプル',
+    'longDescription'  => 'これは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>
+            上記の結果は下記のクラス定義になります。:
+        </para>
+
+        <programlisting role="php"><![CDATA[
+/**
+ * 生成されたクラスサンプル
+ *
+ * これは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>クラスのメソッド付でPHPクラスを生成</title>
+
+        <para>
+            <classname>Zend_CodeGenerator_Php_Class</classname>のおかげで、
+            クラスにオプションのコンテンツと一緒にメソッドを付与できます。
+            メソッドは、配列かまたは具体的な<classname>Zend_CodeGenerator_Php_Method</classname>インスタンスとして付与されるかもしれません。
+        </para>
+
+            <programlisting role="php"><![CDATA[
+$foo      = new Zend_CodeGenerator_Php_Class();
+$docblock = new Zend_CodeGenerator_Php_Docblock(array(
+    'shortDescription' => '生成されたクラスサンプル',
+    'longDescription'  => 'これは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(
+        // メソッドは配列として渡されます
+        array(
+            'name'       => 'setBar',
+            'parameters' => array(
+                array('name' => 'bar'),
+            ),
+            'body'       => '$this->_bar = $bar;' . "\n" . 'return $this;',
+            'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
+                'shortDescription' => 'barプロパティーを設定',
+                'tags'             => array(
+                    new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
+                        'paramName' => 'bar',
+                        'datatype'  => 'string'
+                    )),
+                    new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
+                        'datatype'  => 'string',
+                    )),
+                ),
+            )),
+        ),
+        // メソッドは具体的なインスタンスとして渡されます
+        new Zend_CodeGenerator_Php_Method(array(
+            'name' => 'getBar',
+            'body'       => 'return $this->_bar;',
+            'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
+                'shortDescription' => 'barプロパティーを取得',
+                'tags'             => array(
+                    new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
+                        'datatype'  => 'string|null',
+                    )),
+                ),
+            )),
+        )),
+    ));
+
+echo $foo->generate();
+]]></programlisting>
+
+        <para>
+            上記のコードは下記の出力になります。:
+        </para>
+
+            <programlisting role="php"><![CDATA[
+/**
+ * 生成されたクラスサンプル
+ *
+ * これはZend_CodeGeneratorで生成されたクラスです。
+ *
+ * @version $Rev:$
+ * @license New BSD
+ */
+class Foo
+{
+
+    protected $_bar = 'baz';
+
+    public $baz = 'bat';
+
+    const bat = 'foobarbazbat';
+
+    /**
+     * barプロパティーを設定
+     *
+     * @param string bar
+     * @return string
+     */
+    public function setBar($bar)
+    {
+        $this->_bar = $bar;
+        return $this;
+    }
+
+
+    /**
+     * barプロパティーを取得
+     *
+     * @return string|null
+     */
+    public function getBar()
+    {
+        return $this->_bar;
+    }
+
+
+
+}
+]]></programlisting>
+    </example>
+
+    <example id="zend.codegenerator.examples.file">
+        <title>PHPファイルの生成</title>
+
+        <para>
+            <classname>Zend_CodeGenerator_Php_File</classname>はPHPファイルのコンテンツ生成でも使えます。
+            あなたは、任意のコンテンツ本体だけでなくクラスを含めることができます。
+            クラスを付与するとき、具体的な<classname>Zend_CodeGenerator_Php_Class</classname>インスタンスか、
+            またはクラスを定めている配列を添付しなければなりません。
+        </para>
+
+        <para>
+            下記の例では、前述の例のクラス定義の1つにつき<code>$foo</code>を定義したと仮定します。
+        </para>
+
+        <programlisting role="php"><![CDATA[
+$file = new Zend_CodeGenerator_Php_File(array(
+    'classes'  => array($foo);
+    'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
+        'shortDescription' => 'Fooクラスファイル',
+        'tags'             => array(
+            array(
+                'name'        => 'license',
+                'description' => 'New BSD',
+            ),
+        ),
+    )),
+    'body'     => 'define(\'APPLICATION_ENV\', \'testing\');',
+));
+]]></programlisting>
+
+        <para>
+            <code>generate()</code>を呼び出すとコードを生成します。
+            しかし、ファイルに書き出しません。
+            コンテンツを捕まえて、自分自身で書き出す必要があります。
+            その例です。:
+        </para>
+
+        <programlisting role="php"><![CDATA[
+$code = $file->generate();
+file_put_contents('Foo.php', $code);
+]]></programlisting>
+
+        <para>
+            上記は下記のファイルを生成します:
+        </para>
+
+        <programlisting role="php"><![CDATA[
+<?php
+/**
+ * Fooクラスファイル
+ *
+ * @license New BSD
+ */
+
+
+/**
+ * 生成されたクラスサンプル
+ *
+ * これはZend_CodeGeneratorで生成されたクラスです。
+ *
+ * @version $Rev:$
+ * @license New BSD
+ */
+class Foo
+{
+
+    protected $_bar = 'baz';
+
+    public $baz = 'bat';
+
+    const bat = 'foobarbazbat';
+
+    /**
+     * barプロパティーを設定
+     *
+     * @param string bar
+     * @return string
+     */
+    public function setBar($bar)
+    {
+        $this->_bar = $bar;
+        return $this;
+    }
+
+
+    /**
+     * barプロパティーを取得
+     *
+     * @return string|null
+     */
+    public function getBar()
+    {
+        return $this->_bar;
+    }
+
+
+
+}
+
+
+define('APPLICATION_ENV', 'testing');
+]]></programlisting>
+    </example>
+
+    <example id="zend.codegenerator.examples.reflection-file">
+        <title>reflection経由のPHPファイルのコード生成の種まき</title>
+
+        <para>
+            コード・ジェネレーターを使って、
+            既存のPHPファイルにPHPコードを加えることができます。
+            そうするためには、まずそれにたいしてreflectionを実行する必要があります。
+            静的メソッド<code>fromReflectedFileName()</code>によりこれを実行できます。
+        </para>
+
+        <programlisting role="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>reflection経由のPHPクラス生成の種まき</title>
+        <para>
+            コード・ジェネレーターを使って、既存のPHPファイルにPHPコードを加えることができます。
+            そうするために、最初にクラスをジェネレーター・オブジェクトにマップするために、
+            静的メソッド<code>fromReflection()</code>を使ってください。
+            そこから追加のプロパティまたはメソッドを加えて、そしてクラスを再生成するでしょう。
+        </para>
+
+        <programlisting role="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' => 'bazプロパティーを設定',
+        '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>