Note that there are some explanatory texts on larger screens.

plurals
  1. POAre there any examples using the Zend/Code/Generator component
    primarykey
    data
    text
    <p>I use Symfony's console component together with the Zend_CodeGenerator component. There was very few examples but I got it work -- <a href="http://framework.zend.com/manual/1.12/en/zend.codegenerator.html" rel="nofollow">http://framework.zend.com/manual/1.12/en/zend.codegenerator.html</a>. Of course it didn't handle namespaces and <code>use</code> statements. Today I figured I try <a href="http://goo.gl/CPNTx" rel="nofollow">Zend/Code/Generator</a>. I found an old post here <a href="http://mwop.net/blog/261-Code-Generation-with-ZendCodeGenerator.html" rel="nofollow">http://mwop.net/blog/261-Code-Generation-with-ZendCodeGenerator.html</a>. </p> <p>So using Composer I installed the component and went about updated my code (see below). I'm having issues with doc blocks not being generated (see new version). There's no resulting errors indicating the parameters are wrong just no docblocks. I quickly reviewed the source code, api and even some the unit tests. So before I give up on this and stick with ZF1 CodeGenerator I wondered if there are any examples out there using docblocks.</p> <p><strong>Old Version</strong></p> <pre><code>&lt;?php $class = new Zend_CodeGenerator_Php_Class(); $docblock = new Zend_CodeGenerator_Php_Docblock(array( 'shortDescription' =&gt; 'Index controller', 'tags' =&gt; array( array( 'name' =&gt; 'category', 'description' =&gt; 'Controller', ), array( 'name' =&gt; 'package', 'description' =&gt; 'Application\Controller', ), ), )); $methods = array( array( 'name' =&gt; 'indexAction', 'docblock' =&gt; new Zend_CodeGenerator_Php_Docblock(array( 'shortDescription' =&gt; 'Index action', 'tags' =&gt; array( new Zend_CodeGenerator_Php_Docblock_Tag_Return(array( 'datatype' =&gt; 'void', )), ), )), ), ); $class-&gt;setName("Admin_IndexController") // Additional step needed to add namespace and use statement -&gt;setExtendedClass('Action') -&gt;setDocblock($docblock) -&gt;setMethods($methods); $file = new Zend_CodeGenerator_Php_File(array( 'classes' =&gt; array($class), )); $code = $file-&gt;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-&gt;output-&gt;writeln( 'Controller generated ' . realpath('application/modules/admin/controllers/IndexController.php') ); } </code></pre> <p><strong>New Version</strong></p> <pre><code>&lt;?php $classGenerator = new ClassGenerator(); $classGenerator-&gt;setName('Admin_IndexController') -&gt;setExtendedClass('Action') -&gt;setDocBlock( new DocBlockGenerator( 'Index controller', '', array( new Tag( array( 'name' =&gt; 'category', 'description' =&gt; 'Controller' ) ), new Tag( array( 'name' =&gt; 'package', 'description' =&gt; 'Application\Controller' ) ) ) ) ) -&gt;addMethods( array( new MethodGenerator( 'indexAction', array(), null, null, new DocBlockGenerator( 'Index action' ) ) ) ); $fileGenerator = new FileGenerator(); $fileGenerator-&gt;setUse('Application\Controller\Action') -&gt;setClass($classGenerator); $code = $fileGenerator-&gt;generate(); // ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload