導入

Zend_CodeGeneratorサンプル

Example #1 PHPクラスを生成

下記の例ではクラスレベルのDocBlock付きで空のクラスを生成します。

  1. $foo      = new Zend_CodeGenerator_Php_Class();
  2. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  3.     'shortDescription' => '生成されたクラスサンプル',
  4.     'longDescription'  => 'これはZend_CodeGeneratorで生成されたクラスです。',
  5.     'tags'             => array(
  6.         array(
  7.             'name'        => 'version',
  8.             'description' => '$Rev:$',
  9.         ),
  10.         array(
  11.             'name'        => 'license',
  12.             'description' => 'New BSD',
  13.         ),
  14.     ),
  15. ));
  16. $foo->setName('Foo')
  17.     ->setDocblock($docblock);
  18. echo $foo->generate();

上記のコードは下記の結果になります。:

  1. /**
  2. * 生成されたクラスサンプル
  3. *
  4. * これはZend_CodeGeneratorで生成されたクラスです。
  5. *
  6. * @version $Rev:$
  7. * @license New BSD
  8. *
  9. */
  10. class Foo
  11. {
  12.  
  13. }

Example #2 クラスのプロパティ付でPHPクラスを生成

では、前の例を基にして、生成したクラスにプロパティを加えます。

  1. $foo      = new Zend_CodeGenerator_Php_Class();
  2. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  3.     'shortDescription' => '生成されたクラスサンプル',
  4.     'longDescription'  => 'これはZend_CodeGeneratorで生成されたクラスです。',
  5.     'tags'             => array(
  6.         array(
  7.             'name'        => 'version',
  8.             'description' => '$Rev:$',
  9.         ),
  10.         array(
  11.             'name'        => 'license',
  12.             'description' => 'New BSD',
  13.         ),
  14.     ),
  15. ));
  16. $foo->setName('Foo')
  17.     ->setDocblock($docblock)
  18.     ->setProperties(array(
  19.         array(
  20.             'name'         => '_bar',
  21.             'visibility'   => 'protected',
  22.             'defaultValue' => 'baz',
  23.         ),
  24.         array(
  25.             'name'         => 'baz',
  26.             'visibility'   => 'public',
  27.             'defaultValue' => 'bat',
  28.         ),
  29.         array(
  30.             'name'         => 'bat',
  31.             'const'        => true,
  32.             'defaultValue' => 'foobarbazbat',
  33.         ),
  34.     ));
  35. echo $foo->generate();

上記の結果は下記のクラス定義になります。:

  1. /**
  2. * 生成されたクラスサンプル
  3. *
  4. * これはZend_CodeGeneratorで生成されたクラスです。
  5. *
  6. * @version $Rev:$
  7. * @license New BSD
  8. *
  9. */
  10. class Foo
  11. {
  12.  
  13.     protected $_bar = 'baz';
  14.  
  15.     public $baz = 'bat';
  16.  
  17.     const bat = 'foobarbazbat';
  18.  
  19. }

Example #3 クラスのメソッド付でPHPクラスを生成

Zend_CodeGenerator_Php_Classのおかげで、 クラスにオプションのコンテンツと一緒にメソッドを付与できます。 メソッドは、配列かまたは具体的なZend_CodeGenerator_Php_Methodインスタンスとして付与されるかもしれません。

  1. $foo      = new Zend_CodeGenerator_Php_Class();
  2. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  3.     'shortDescription' => '生成されたクラスサンプル',
  4.     'longDescription'  => 'これはZend_CodeGeneratorで生成されたクラスです。',
  5.     'tags'             => array(
  6.         array(
  7.             'name'        => 'version',
  8.             'description' => '$Rev:$',
  9.         ),
  10.         array(
  11.             'name'        => 'license',
  12.             'description' => 'New BSD',
  13.         ),
  14.     ),
  15. ));
  16. $foo->setName('Foo')
  17.     ->setDocblock($docblock)
  18.     ->setProperties(array(
  19.         array(
  20.             'name'         => '_bar',
  21.             'visibility'   => 'protected',
  22.             'defaultValue' => 'baz',
  23.         ),
  24.         array(
  25.             'name'         => 'baz',
  26.             'visibility'   => 'public',
  27.             'defaultValue' => 'bat',
  28.         ),
  29.         array(
  30.             'name'         => 'bat',
  31.             'const'        => true,
  32.             'defaultValue' => 'foobarbazbat',
  33.         ),
  34.     ))
  35.     ->setMethods(array(
  36.         // メソッドは配列として渡されます
  37.         array(
  38.             'name'       => 'setBar',
  39.             'parameters' => array(
  40.                 array('name' => 'bar'),
  41.             ),
  42.             'body'       => '$this->_bar = $bar;' . "\n" . 'return $this;',
  43.             'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
  44.                 'shortDescription' => 'barプロパティーを設定',
  45.                 'tags'             => array(
  46.                     new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  47.                         'paramName' => 'bar',
  48.                         'datatype'  => 'string'
  49.                     )),
  50.                     new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  51.                         'datatype'  => 'string',
  52.                     )),
  53.                 ),
  54.             )),
  55.         ),
  56.         // メソッドは具体的なインスタンスとして渡されます
  57.         new Zend_CodeGenerator_Php_Method(array(
  58.             'name' => 'getBar',
  59.             'body'       => 'return $this->_bar;',
  60.             'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
  61.                 'shortDescription' => 'barプロパティーを取得',
  62.                 'tags'             => array(
  63.                     new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  64.                         'datatype'  => 'string|null',
  65.                     )),
  66.                 ),
  67.             )),
  68.         )),
  69.     ));
  70.  
  71. echo $foo->generate();

上記のコードは下記の出力になります。:

  1. /**
  2. * 生成されたクラスサンプル
  3. *
  4. * これはZend_CodeGeneratorで生成されたクラスです。
  5. *
  6. * @version $Rev:$
  7. * @license New BSD
  8. */
  9. class Foo
  10. {
  11.  
  12.     protected $_bar = 'baz';
  13.  
  14.     public $baz = 'bat';
  15.  
  16.     const bat = 'foobarbazbat';
  17.  
  18.     /**
  19.      * barプロパティーを設定
  20.      *
  21.      * @param string bar
  22.      * @return string
  23.      */
  24.     public function setBar($bar)
  25.     {
  26.         $this->_bar = $bar;
  27.         return $this;
  28.     }
  29.  
  30.     /**
  31.      * barプロパティーを取得
  32.      *
  33.      * @return string|null
  34.      */
  35.     public function getBar()
  36.     {
  37.         return $this->_bar;
  38.     }
  39.  
  40. }

Example #4 PHPファイルの生成

Zend_CodeGenerator_Php_FilePHPファイルのコンテンツ生成でも使えます。 あなたは、任意のコンテンツ本体だけでなくクラスを含めることができます。 クラスを付与するとき、具体的なZend_CodeGenerator_Php_Classインスタンスか、 またはクラスを定めている配列を添付しなければなりません。

下記の例では、前述の例のクラス定義の1つにつき$fooを定義したと仮定します。

  1. $file = new Zend_CodeGenerator_Php_File(array(
  2.     'classes'  => array($foo);
  3.     'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  4.         'shortDescription' => 'Fooクラスファイル',
  5.         'tags'             => array(
  6.             array(
  7.                 'name'        => 'license',
  8.                 'description' => 'New BSD',
  9.             ),
  10.         ),
  11.     )),
  12.     'body'     => 'define(\'APPLICATION_ENV\', \'testing\');',
  13. ));

generate()を呼び出すとコードを生成します。 しかし、ファイルに書き出しません。 コンテンツを捕まえて、自分自身で書き出す必要があります。 その例です。:

  1. $code = $file->generate();
  2. file_put_contents('Foo.php', $code);

上記は下記のファイルを生成します:

  1. <?php
  2. /**
  3. * Fooクラスファイル
  4. *
  5. * @license New BSD
  6. */
  7.  
  8. /**
  9. * 生成されたクラスサンプル
  10. *
  11. * これはZend_CodeGeneratorで生成されたクラスです。
  12. *
  13. * @version $Rev:$
  14. * @license New BSD
  15. */
  16. class Foo
  17. {
  18.  
  19.     protected $_bar = 'baz';
  20.  
  21.     public $baz = 'bat';
  22.  
  23.     const bat = 'foobarbazbat';
  24.  
  25.     /**
  26.      * barプロパティーを設定
  27.      *
  28.      * @param string bar
  29.      * @return string
  30.      */
  31.     public function setBar($bar)
  32.     {
  33.         $this->_bar = $bar;
  34.         return $this;
  35.     }
  36.  
  37.     /**
  38.      * barプロパティーを取得
  39.      *
  40.      * @return string|null
  41.      */
  42.     public function getBar()
  43.     {
  44.         return $this->_bar;
  45.     }
  46.  
  47. }
  48.  
  49. define('APPLICATION_ENV', 'testing');

Example #5 reflection経由のPHPファイルのコード生成の種まき

コード・ジェネレーターを使って、 既存のPHPファイルにPHPコードを加えることができます。 そうするためには、まずそれにたいしてreflectionを実行する必要があります。 静的メソッド fromReflectedFileName()によりこれを実行できます。

  1. $generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path);
  2. $body = $generator->getBody();
  3. $body .= "\n\$foo->bar();";
  4. file_put_contents($path, $generator->generate());

Example #6 reflection経由のPHPクラス生成の種まき

コード・ジェネレーターを使って、既存のPHPファイルにPHPコードを加えることができます。 そうするために、最初にクラスをジェネレーター・オブジェクトにマップするために、 静的メソッド fromReflection()を使ってください。 そこから追加のプロパティまたはメソッドを加えて、そしてクラスを再生成するでしょう。

  1. $generator = Zend_CodeGenerator_Php_Class::fromReflection(
  2.     new Zend_Reflection_Class($class)
  3. );
  4. $generator->setMethod(array(
  5.     'name'       => 'setBaz',
  6.     'parameters' => array(
  7.         array('name' => 'baz'),
  8.     ),
  9.     'body'       => '$this->_baz = $baz;' . "\n" . 'return $this;',
  10.     'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
  11.         'shortDescription' => 'bazプロパティーを設定',
  12.         'tags'             => array(
  13.             new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  14.                 'paramName' => 'baz',
  15.                 'datatype'  => 'string'
  16.             )),
  17.             new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  18.                 'datatype'  => 'string',
  19.             )),
  20.         ),
  21.     )),
  22. ));
  23. $code = $generator->generate();

導入