私は Symfony のコンソール コンポーネントを Zend_CodeGenerator コンポーネントと一緒に使用しています。例はほとんどありませんでしたが、うまくいきました - http://framework.zend.com/manual/1.12/en/zend.codegenerator.html。もちろん、名前空間とuse
ステートメントを処理しませんでした。今日、私はZend/Code/Generatorを試してみました。ここで古い投稿を見つけました http://mwop.net/blog/261-Code-Generation-with-ZendCodeGenerator.html。
そのため、Composer を使用してコンポーネントをインストールし、コードを更新しました (以下を参照)。doc ブロックが生成されないという問題があります (新しいバージョンを参照してください)。パラメータが間違っていることを示す結果のエラーはありません。docblock はありません。ソースコード、API、さらにはいくつかの単体テストを簡単に確認しました。したがって、これをあきらめて ZF1 CodeGenerator に固執する前に、docblock を使用した例があるかどうか疑問に思いました。
古いバージョン
<?php
$class = new Zend_CodeGenerator_Php_Class();
$docblock = new Zend_CodeGenerator_Php_Docblock(array(
'shortDescription' => 'Index controller',
'tags' => array(
array(
'name' => 'category',
'description' => 'Controller',
),
array(
'name' => 'package',
'description' => 'Application\Controller',
),
),
));
$methods = array(
array(
'name' => 'indexAction',
'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
'shortDescription' => 'Index action',
'tags' => array(
new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
'datatype' => 'void',
)),
),
)),
),
);
$class->setName("Admin_IndexController")
// Additional step needed to add namespace and use statement
->setExtendedClass('Action')
->setDocblock($docblock)
->setMethods($methods);
$file = new Zend_CodeGenerator_Php_File(array(
'classes' => array($class),
));
$code = $file->generate();
// Remove all that extra blank lines
$code = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $code);
if (file_put_contents('application/modules/admin/controllers/IndexController.php', $code)) {
$this->output->writeln(
'Controller generated ' . realpath('application/modules/admin/controllers/IndexController.php')
);
}
新しいバージョン
<?php
$classGenerator = new ClassGenerator();
$classGenerator->setName('Admin_IndexController')
->setExtendedClass('Action')
->setDocBlock(
new DocBlockGenerator(
'Index controller',
'',
array(
new Tag(
array(
'name' => 'category',
'description' => 'Controller'
)
),
new Tag(
array(
'name' => 'package',
'description' => 'Application\Controller'
)
)
)
)
)
->addMethods(
array(
new MethodGenerator(
'indexAction',
array(),
null,
null,
new DocBlockGenerator(
'Index action'
)
)
)
);
$fileGenerator = new FileGenerator();
$fileGenerator->setUse('Application\Controller\Action')
->setClass($classGenerator);
$code = $fileGenerator->generate();
// ...