| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <!-- EN-Revision: 24249 -->
- <sect1 id="zend.codegenerator.examples">
- <title>Zend_CodeGeneratorサンプル</title>
- <example id="zend.codegenerator.examples.class">
- <title>PHPクラスを生成</title>
- <para>
- 下記の例ではクラスレベルのDocBlock付きで空のクラスを生成します。
- </para>
- <programlisting language="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 language="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 language="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 language="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 language="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 language="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>は<acronym>PHP</acronym>ファイルのコンテンツ生成でも使えます。
- あなたは、任意のコンテンツ本体だけでなくクラスを含めることができます。
- クラスを付与するとき、具体的な<classname>Zend_CodeGenerator_Php_Class</classname>インスタンスか、
- またはクラスを定めている配列を添付しなければなりません。
- </para>
- <para>
- 下記の例では、前述の例のクラス定義の1つにつき<varname>$foo</varname>を定義したと仮定します。
- </para>
- <programlisting language="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>
- <methodname>generate()</methodname>を呼び出すとコードを生成します。
- しかし、ファイルに書き出しません。
- コンテンツを捕まえて、自分自身で書き出す必要があります。
- その例です。:
- </para>
- <programlisting language="php"><![CDATA[
- $code = $file->generate();
- file_put_contents('Foo.php', $code);
- ]]></programlisting>
- <para>
- 上記は下記のファイルを生成します:
- </para>
- <programlisting language="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>
- コード・ジェネレーターを使って、
- 既存の<acronym>PHP</acronym>ファイルに<acronym>PHP</acronym>コードを加えることができます。
- そうするためには、まずそれにたいしてreflectionを実行する必要があります。
- 静的メソッド<methodname>fromReflectedFileName()</methodname>によりこれを実行できます。
- </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>reflection経由のPHPクラス生成の種まき</title>
- <para>
- コード・ジェネレーターを使って、既存のPHPファイルにPHPコードを加えることができます。
- そうするために、最初にクラスをジェネレーター・オブジェクトにマップするために、
- 静的メソッド<methodname>fromReflection()</methodname>を使ってください。
- そこから追加のプロパティまたはメソッドを加えて、そしてクラスを再生成するでしょう。
- </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' => '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>
|