我想根據模板生成代碼。基於模板的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