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