regex - PHP Code generator based on templates -
i want generate code based on templates.
suppose in /templates
have files structured as:
/templates
- vendor/plugin/config.xml
- vendor/plugin/model/plugin.php
- vendor/plugin/view/plugin.phtml
and files have following contents(variables enclosed {{ }}
needs parsed):
vendor/plugin/config.xml:
<?xml version="1.0"?> <config> <module>{{vendor}}/{{plugin}}</module> <version>{{version}}</version> {{if $hastable}} <database> <table>{{tablename}}</table> <pk>{{primarykey}}</pk> <fields> {{foreach $fields}} <field> <name>{{fields.name}}</name> <label>{{fields.label}}</label> <type>{{fields.type}}</type> </field> {{/foreach}} </fields> </database> {{/if}} </config>
vendor/plugin/model/plugin.php:
<?php /** * @category {{vendor}}_{{plugin}} * @author {{author}} */ class {{vendor}}_{{plugin}}_model_{{plugin}} extends core_model_abstract { public function __construct() { parent::__construct(); } {{if $hastable}} public function settable() { $this->_tablename = '{{tablename}}'; } public function setprimarykey() { $this->_primarykey = '{{primarykey}}'; } public function setfields() { $this->_fields = core::config('database/table/fields'); } {{/if}} }
vendor/plugin/view/plugin.phtml:
{{tablename}} rows <table> <tr> {{foreach $fields}} <th>{{$fields.label}}</th> {{/foreach}} </tr> <?php foreach ($data $_data) ?> <tr> {{foreach $fields}} <td><?php echo $_data['{{$fields.name}}'] ?></td> {{/foreach}} </tr> <?php endforeach; ?> </table>
how code generator should work?
1> gui form let users add @ least following fields
vendor: plugin: version: author:
has tables?: if selected yes, allow users add more fields table name, it's fields etc.
2> on submitting form, generates code /templates folder directory logic can be: preparing variables fed coregenerator (class developed), read template files , re-generates them parsing variables.
expected output /template
be: (suppose if have following valures user input
vendor: foo plugin: bar version: 1.0.0 author: john doe <john.doe@example.com> has tables?: yes table name: blog primary key: blog_id fields: + name: title, label: title, type: text + name: status, label: status, type:int ...
)
/generated
- foo/bar/config.xml
- foo/bar/model/bar.php
- foo/bar/view/bar.phtml <- note case sensitivty)
generated contents:
foo/bar/config.xml:
<?xml version="1.0"?> <config> <module>foo/bar</module> <version>1.0.0</version> <database> <table>blog</table> <pk>blog_id</pk> <fields> <field> <name>title</name> <label>title</label> <type>text</type> </field> <field> <name>status</name> <label>status</label> <type>int</type> </field> <!--... --> </fields> </database> </config>
foo/bar/model/bar.php:
<?php /** * @category foo_bar * @author john doe <john.doe@example.com> */ class foo_bar_model_bar extends core_model_abstract { public function __construct() { parent::__construct(); } public function settable() { $this->_tablename = 'blog'; } public function setprimarykey() { $this->_primarykey = 'blog_id'; } public function setfields() { $this->_fields = core::config('database/table/fields'); } }
foo/bar/view/bar.phtml:
blog rows <table> <tr> <th>title</th> <th>status</th> </tr> <?php foreach ($data $_data) ?> <tr> <td><?php echo $_data['title'] ?></td> <td><?php echo $_data['status'] ?></td> </tr> <?php endforeach; ?> </table>
so main concern code generator class/library collect placeholder values user input, read files /templates
folder , regenerate them after parsing variables (simple, conditional, loop etc.) /generated
folder.
any insights on this, how should start with? rough idea, solutions & references highly appreciated. in advance.
i suggest use instead gui interface cli interface. because in way more customizable.
as reference can use yeoman large scaffold tool, documented, can build generator less efforts. http://yeoman.io/
for inspiration, take @ generator demo: https://github.com/daftmonk/generator-angular-fullstack
Comments
Post a Comment