Theory of Operation
最も代表的な使用例では、単にコード・ジェネレーター・クラスのインスタンスを生成して、
それに適切な設定を渡すか、またはインスタンス化のあと設定します。
コードを生成するために、単にオブジェクトをechoするか、
その generate()メソッドを呼びます。
//構成をコンストラクタに渡す
$file =
new Zend_CodeGenerator_Php_File
(array(
new Zend_CodeGenerator_Php_Class
(array(
'name' => 'World',
new Zend_CodeGenerator_Php_Method
(array(
'name' => 'hello',
'body' => 'echo \'Hello world!\';',
)),
),
)),
)
));
//インスタンス化のあと設定
$method = new Zend_CodeGenerator_Php_Method();
$method->setName('hello')
->setBody('echo \'Hello world!\';');
$class = new Zend_CodeGenerator_Php_Class();
$class->setName('World')
->setMethod($method);
$file = new Zend_CodeGenerator_Php_File();
$file->setClass($class);
//生成されたファイルのレンダリング
//またはファイルへの書き出し:
file_put_contents('World.php', $file->generate());
上記のサンプルは両方とも同じ結果でレンダリングされます:
<?php
class World
{
public function hello()
{
}
}
もう一つの一般的な使用例は、既存のコードを更新することです。
たとえばメソッドをクラスに加えるために。
この場合には、最初にreflectionを用いて既存のコードを調べて、
次に新しいメソッドを加えなければいけません。
Zend_Reflectionを導入することによって、
Zend_CodeGeneratorはつまらないほどこれを単純にします。
例えば、上記をファイル"World.php"に保存して、すでにincludeしたとしましょう。
それから下記のようにできます。
$class = Zend_CodeGenerator_Php_Class::fromReflection(
new Zend_Reflection_Class('World')
);
$method = new Zend_CodeGenerator_Php_Method();
$method->setName('mrMcFeeley')
->setBody('echo \'Hello, Mr. McFeeley!\';');
$class->setMethod($method);
$file = new Zend_CodeGenerator_Php_File();
$file->setClass($class);
//生成されたファイルのレンダリング
//または、より良いですが、元のファイルに書き戻します。:
file_put_contents('World.php', $file->generate());
クラスファイルの結果はこのようになります:
<?php
class World
{
public function hello()
{
}
public function mrMcFeeley()
{
echo 'Hellow Mr. McFeeley!';
}
}