title: PHP Code generator based on templates
body: <p>I want to generate the code based on the templates.</p>
  
  <p>Suppose in <code>/Templates</code> I have files structured as:</p>
  
  <pre><code>/Templates
  </code></pre>
  
  <ul>
  <li>Vendor/Plugin/config.xml</li>
  <li>Vendor/Plugin/Model/Plugin.php</li>
  <li>Vendor/Plugin/View/plugin.phtml</li>
  </ul>
  
  <p>And say the files have the following contents(variables enclosed with <code>{{ }}</code> needs to be parsed):</p>
  
  <p><em>Vendor/Plugin/config.xml:</em></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;config&gt;
      &lt;module&gt;{{Vendor}}/{{Plugin}}&lt;/module&gt;
      &lt;version&gt;{{Version}}&lt;/version&gt;
  
      {{if $HasTable}}
      &lt;database&gt;
          &lt;table&gt;{{TableName}}&lt;/table&gt;
          &lt;pk&gt;{{PrimaryKey}}&lt;/pk&gt;
          &lt;fields&gt;
              {{foreach $Fields}}
              &lt;field&gt;
                  &lt;name&gt;{{Fields.Name}}&lt;/name&gt;
                  &lt;label&gt;{{Fields.Label}}&lt;/label&gt;
                  &lt;type&gt;{{Fields.Type}}&lt;/type&gt;
              &lt;/field&gt;
              {{/foreach}}
          &lt;/fields&gt;
      &lt;/database&gt;
      {{/if}}
  
  &lt;/config&gt;
  </code></pre>
  
  <p><em>Vendor/Plugin/Model/Plugin.php:</em></p>
  
  <pre><code>&lt;?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-&gt;_tableName = '{{TableName}}';
      }
      public function setPrimaryKey()
      {
          $this-&gt;_primaryKey = '{{PrimaryKey}}';
      }
      public function setFields()
      {
          $this-&gt;_fields = Core::Config('database/table/fields');
      }
      {{/if}}
  }
  </code></pre>
  
  <p><em>Vendor/Plugin/View/plugin.phtml:</em></p>
  
  <pre><code>{{TableName}} Rows
  &lt;table&gt;
      &lt;tr&gt;
          {{foreach $Fields}}
              &lt;th&gt;{{$Fields.Label}}&lt;/th&gt;
          {{/foreach}}
      &lt;/tr&gt;
  
      &lt;?php foreach ($data as $_data) ?&gt;
          &lt;tr&gt;
              {{foreach $Fields}}
                  &lt;td&gt;&lt;?php echo $_data['{{$Fields.Name}}'] ?&gt;&lt;/td&gt;
              {{/foreach}}
          &lt;/tr&gt;
      &lt;?php endforeach; ?&gt;
  
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>How the code generator should work?</strong></p>
  
  <p>1> A GUI Form which will let users to add at least following fields</p>
  
  <p>Vendor:
  Plugin:
  Version:
  Author:</p>
  
  <p>Has Tables?:
  If selected to yes, it will allow users to add more fields like table name, it's fields etc.</p>
  
  <p>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.</p>
  
  <p>Expected output from <code>/Template</code> will be:
  (Suppose if we have following valures from user input</p>
  
  <pre><code>Vendor: Foo
  Plugin: Bar
  Version: 1.0.0
  Author: John Doe &lt;john.doe@example.com&gt;
  Has Tables?: Yes
  Table Name: blog
  Primary Key: blog_id
  Fields:
  + Name: title, Label: Title, Type: Text
  + Name: status, Label: Status, Type:Int
  ...
  </code></pre>
  
  <p>)</p>
  
  <pre><code>/Generated
  </code></pre>
  
  <ul>
  <li>Foo/Bar/config.xml</li>
  <li>Foo/Bar/Model/Bar.php</li>
  <li>Foo/Bar/View/bar.phtml &lt;- note the case sensitivty)</li>
  </ul>
  
  <p><strong>Generated Contents:</strong></p>
  
  <p><em>Foo/Bar/config.xml:</em></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;config&gt;
      &lt;module&gt;Foo/Bar&lt;/module&gt;
      &lt;version&gt;1.0.0&lt;/version&gt;
  
      &lt;database&gt;
          &lt;table&gt;blog&lt;/table&gt;
          &lt;pk&gt;blog_id&lt;/pk&gt;
          &lt;fields&gt;
  
              &lt;field&gt;
                  &lt;name&gt;title&lt;/name&gt;
                  &lt;label&gt;Title&lt;/label&gt;
                  &lt;type&gt;Text&lt;/type&gt;
              &lt;/field&gt;
              &lt;field&gt;
                  &lt;name&gt;status&lt;/name&gt;
                  &lt;label&gt;Status&lt;/label&gt;
                  &lt;type&gt;Int&lt;/type&gt;
              &lt;/field&gt;
              &lt;!--... --&gt;
  
          &lt;/fields&gt;
      &lt;/database&gt;
  
  &lt;/config&gt;
  </code></pre>
  
  <p><em>Foo/Bar/Model/Bar.php:</em></p>
  
  <pre><code>&lt;?php
  
  /**
   * @category Foo_Bar
   * @author  John Doe &lt;john.doe@example.com&gt;
   */
  class Foo_Bar_Model_Bar extends Core_Model_Abstract
  {
      public function __construct()
      {
          parent::__construct();
      }
  
  
      public function setTable()
      {
          $this-&gt;_tableName = 'blog';
      }
      public function setPrimaryKey()
      {
          $this-&gt;_primaryKey = 'blog_id';
      }
      public function setFields()
      {
          $this-&gt;_fields = Core::Config('database/table/fields');
      }
  
  }
  </code></pre>
  
  <p><em>Foo/Bar/View/bar.phtml:</em></p>
  
  <pre><code>blog Rows
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;Title&lt;/th&gt;
          &lt;th&gt;Status&lt;/th&gt;
      &lt;/tr&gt;
  
      &lt;?php foreach ($data as $_data) ?&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;?php echo $_data['title'] ?&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?php echo $_data['status'] ?&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;?php endforeach; ?&gt;
  
  &lt;/table&gt;
  </code></pre>
  
  <p>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 <code>/Templates</code> folder and regenerate them after parsing those variables (simple, conditional, loop etc.) to <code>/Generated</code> folder.</p>
  
  <p>Any insights on this, how should I start with? any rough idea, solutions &amp; references are highly appreciated.
  Thanks in advance.</p>