Zend_CodeGeneratorサンプル
下記の例ではクラスレベルのDocBlock付きで空のクラスを生成します。
$foo = new Zend_CodeGenerator_Php_Class();
$docblock = new Zend_CodeGenerator_Php_Docblock (array(
'shortDescription' => '生成されたクラスサンプル',
'longDescription' => 'これはZend_CodeGeneratorで生成されたクラスです。',
'name' => 'version',
'description' => '$Rev:$',
),
'name' => 'license',
'description' => 'New BSD',
),
),
));
$foo->setName('Foo')
->setDocblock($docblock);
/**
* 生成されたクラスサンプル
*
* これはZend_CodeGeneratorで生成されたクラスです。
*
* @version $Rev:$
* @license New BSD
*
*/
class Foo
{
}
Example #2 クラスのプロパティ付でPHPクラスを生成
では、前の例を基にして、生成したクラスにプロパティを加えます。
$foo = new Zend_CodeGenerator_Php_Class();
$docblock = new Zend_CodeGenerator_Php_Docblock (array(
'shortDescription' => '生成されたクラスサンプル',
'longDescription' => 'これはZend_CodeGeneratorで生成されたクラスです。',
'name' => 'version',
'description' => '$Rev:$',
),
'name' => 'license',
'description' => 'New BSD',
),
),
));
$foo->setName('Foo')
->setDocblock($docblock)
'name' => '_bar',
'visibility' => 'protected',
'defaultValue' => 'baz',
),
'name' => 'baz',
'visibility' => 'public',
'defaultValue' => 'bat',
),
'name' => 'bat',
'const' => true,
'defaultValue' => 'foobarbazbat',
),
));
/**
* 生成されたクラスサンプル
*
* これはZend_CodeGeneratorで生成されたクラスです。
*
* @version $Rev:$
* @license New BSD
*
*/
class Foo
{
protected $_bar = 'baz';
public $baz = 'bat';
const bat = 'foobarbazbat';
}
Example #3 クラスのメソッド付で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で生成されたクラスです。',
'name' => 'version',
'description' => '$Rev:$',
),
'name' => 'license',
'description' => 'New BSD',
),
),
));
$foo->setName('Foo')
->setDocblock($docblock)
'name' => '_bar',
'visibility' => 'protected',
'defaultValue' => 'baz',
),
'name' => 'baz',
'visibility' => 'public',
'defaultValue' => 'bat',
),
'name' => 'bat',
'const' => true,
'defaultValue' => 'foobarbazbat',
),
))
// メソッドは配列として渡されます
'name' => 'setBar',
),
'body' => '$this->_bar = $bar;' . "\n" . 'return $this;',
'docblock' => new Zend_CodeGenerator_Php_Docblock (array(
'shortDescription' => 'barプロパティーを設定',
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プロパティーを取得',
new Zend_CodeGenerator_Php_Docblock_Tag_Return (array(
'datatype' => 'string|null',
)),
),
)),
)),
));
/**
* 生成されたクラスサンプル
*
* これは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;
}
}
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クラスファイル',
'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');
Example #5 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());
Example #6 reflection経由のPHPクラス生成の種まき
コード・ジェネレーターを使って、既存のPHPファイルにPHPコードを加えることができます。
そうするために、最初にクラスをジェネレーター・オブジェクトにマップするために、
静的メソッド fromReflection()を使ってください。
そこから追加のプロパティまたはメソッドを加えて、そしてクラスを再生成するでしょう。
$generator = Zend_CodeGenerator_Php_Class::fromReflection(
new Zend_Reflection_Class($class)
);
$generator-> setMethod(array(
'name' => 'setBaz',
),
'body' => '$this->_baz = $baz;' . "\n" . 'return $this;',
'docblock' => new Zend_CodeGenerator_Php_Docblock (array(
'shortDescription' => 'bazプロパティーを設定',
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();
|
|