基于模板的PHP代码生成器


PHP Code generator based on templates

我想基于模板生成代码。

假设在/Templates中,我有如下结构的文件:

/Templates
  • 供应商/插件/config.xml
  • 供应商/插件/型号/Plugin.php
  • 供应商/Plugin/View/Plugin.phtml

并说这些文件有以下内容(需要解析{{ }}中的变量):

供应商/插件/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>

供应商/插件/模型/插件.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 as $_data) ?>
        <tr>
            {{foreach $Fields}}
                <td><?php echo $_data['{{$Fields.Name}}'] ?></td>
            {{/foreach}}
        </tr>
    <?php endforeach; ?>
</table>

代码生成器应该如何工作

1> 一个GUI表单,它将允许用户添加至少以下字段

供应商:插件:版本:作者:

是否有表?:如果选择是,它将允许用户添加更多的字段,如表名、它的字段等。

2> 提交表单时,它会将代码从/Templates文件夹生成到某个目录逻辑可以是:准备将变量输入到CoreGenerator(待开发的类)中,CoreGenerator将读取所有模板文件,并通过解析这些变量重新生成它们。

/Template的预期输出为:(假设我们从用户输入得到以下值

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<-注意区分大小写)

生成的内容:

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 as $_data) ?>
        <tr>
            <td><?php echo $_data['title'] ?></td>
            <td><?php echo $_data['status'] ?></td>
        </tr>
    <?php endforeach; ?>
</table>

因此,我主要关心的是代码生成器类/库,它将从用户输入中收集占位符值,从/Templates文件夹中读取所有这些文件,并在将这些变量(简单、条件、循环等)解析到/Generated文件夹后重新生成它们。

对此有什么见解吗?我应该如何开始?任何粗略的想法、解决方案&非常感谢参考资料。提前谢谢。

我建议您使用gui接口,而不是cli接口。因为这样更容易定制。

作为参考,您可以使用Yeoman,这是一个有充分文档的大型脚手架工具,可以帮助您用较少的精力构建生成器。http://yeoman.io/

要获得灵感,请查看此生成器演示:https://github.com/DaftMonk/generator-angular-fullstack