例112 PHPクラスを生成
下記の例ではクラスレベルのDocBlock付きで空のクラスを生成します。
$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();
上記のコードは下記の結果になります。:
/** * 生成されたクラスサンプル * * これはZend_CodeGeneratorで生成されたクラスです。 * * @version $Rev:$ * @license New BSD * */ class Foo { }
例113 クラスのプロパティ付でPHPクラスを生成
では、前の例を基にして、生成したクラスにプロパティを加えます。
$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();
上記の結果は下記のクラス定義になります。:
/** * 生成されたクラスサンプル * * これはZend_CodeGeneratorで生成されたクラスです。 * * @version $Rev:$ * @license New BSD * */ class Foo { protected $_bar = 'baz'; public $baz = 'bat'; const bat = 'foobarbazbat'; }
例114 クラスのメソッド付でPHPクラスを生成
Zend_CodeGenerator_Php_Class
のおかげで、
クラスにオプションのコンテンツと一緒にメソッドを付与できます。
メソッドは、配列かまたは具体的なZend_CodeGenerator_Php_Method
インスタンスとして付与されるかもしれません。
$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();
上記のコードは下記の出力になります。:
/** * 生成されたクラスサンプル * * これは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; } }
例115 PHPファイルの生成
Zend_CodeGenerator_Php_File
はPHPファイルのコンテンツ生成でも使えます。
あなたは、任意のコンテンツ本体だけでなくクラスを含めることができます。
クラスを付与するとき、具体的なZend_CodeGenerator_Php_Class
インスタンスか、
またはクラスを定めている配列を添付しなければなりません。
下記の例では、前述の例のクラス定義の1つにつき$foo
を定義したと仮定します。
$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\');', ));
generate()
を呼び出すとコードを生成します。
しかし、ファイルに書き出しません。
コンテンツを捕まえて、自分自身で書き出す必要があります。
その例です。:
$code = $file->generate(); file_put_contents('Foo.php', $code);
上記は下記のファイルを生成します:
<?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');
例116 reflection経由のPHPファイルのコード生成の種まき
コード・ジェネレーターを使って、
既存のPHPファイルにPHPコードを加えることができます。
そうするためには、まずそれにたいしてreflectionを実行する必要があります。
静的メソッドfromReflectedFileName()
によりこれを実行できます。
$generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path); $body = $generator->getBody(); $body .= "\n\$foo->bar();"; file_put_contents($path, $generator->generate());
例117 reflection経由のPHPクラス生成の種まき
コード・ジェネレーターを使って、既存のPHPファイルにPHPコードを加えることができます。
そうするために、最初にクラスをジェネレーター・オブジェクトにマップするために、
静的メソッドfromReflection()
を使ってください。
そこから追加のプロパティまたはメソッドを加えて、そしてクラスを再生成するでしょう。
$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();