我想根据模板生成代码。基于模板的PHP代码生成器
假设在/Templates
我有结构为文件:/
/Templates
- 供应商/插件/ config.xml中
- 供应商/插件/型号Plugin.php
- 供应商/插件/浏览/ 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>
供应商/插件/型号/ 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}}
}
供应商/ 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(要开发的类)的变量,它将读取所有模板文件并通过解析这些变量重新生成它们。
从/Template
预计产出将是: (假设如果我们有如下的用户输入
Vendor: Foo
Plugin: Bar
Version: 1.0.0
Author: John Doe <[email protected]>
Has Tables?: Yes
Table Name: blog
Primary Key: blog_id
Fields:
+ Name: title, Label: Title, Type: Text
+ Name: status, Label: Status, Type:Int
...
valures)
/Generated
- 富/酒吧/ config.xml中
- 富/Bar/Model/Bar.php
- Foo/Bar/View/bar.phtml < - 请注意大小写敏感性试验)
生成的内容:
富/酒吧/配置。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>
富/酒吧/型号/ Bar.php:
<?php
/**
* @category Foo_Bar
* @author John Doe <[email protected]>
*/
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');
}
}
富/酒吧/查看/ 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>
所以我主要关注的是代码生成器类/库,它将从用户输入中收集占位符值,并从0123中读取所有这些文件文件夹并在解析这些变量(简单,条件,循环等)到/Generated
文件夹后重新生成它们。
对此的任何见解,我该如何开始?任何粗略的想法,解决方案&引用是高度赞赏。 在此先感谢。
我一直在寻找到在此之前。我离开的原因是我想要一个基于GUI的,以便我可以从任何地方(从共享主机等)生成它不像Yeoman。 – MagePsycho
你可以自己生成,然后再部署它。 – gvsrepins
我正在考虑为此目的使用#twig模板。 – MagePsycho