I want to generate the code based on the templates.
Suppose in /Templates
I have files structured as:
/Templates
- Vendor/Plugin/config.xml
- Vendor/Plugin/Model/Plugin.php
- Vendor/Plugin/View/plugin.phtml
And say the files have the following contents(variables enclosed with {{ }}
needs to be 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 as $_data) ?>
<tr>
{{foreach $Fields}}
<td><?php echo $_data['{{$Fields.Name}}'] ?></td>
{{/foreach}}
</tr>
<?php endforeach; ?>
</table>
How the code generator should work?
1> A GUI Form which will let users to add at least following fields
Vendor:
Plugin:
Version:
Author:
Has Tables?:
If selected to yes, it will allow users to add more fields like table name, it’s fields etc.
2> On submitting the form, it generates the code from /Templates folder to some directory
Logic can be:
Preparing the variables to fed into the CoreGenerator (class to be developed), which will read all the template files and re-generates them by parsing those variables.
Expected output from /Template
will be:
(Suppose if we have following valures from 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 the 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 as $_data) ?>
<tr>
<td><?php echo $_data['title'] ?></td>
<td><?php echo $_data['status'] ?></td>
</tr>
<?php endforeach; ?>
</table>
So my main concern will be for the Code Generator class/library which will collect placeholder values from user input, read all those files from /Templates
folder and regenerate them after parsing those variables (simple, conditional, loop etc.) to /Generated
folder.
Any insights on this, how should I start with? any rough idea, solutions & references are highly appreciated.
Thanks in advance.