2017 © Pedro Peláez
 

library marsgen

A PHP code generation and scaffolding tool.

image

metehan/marsgen

A PHP code generation and scaffolding tool.

  • Tuesday, April 25, 2017
  • by Metehan
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Marsgen PHP File Generator

Marsgen is a PHP generator. It generates predefined files and folders with given data. It can be used to generate scaffolding. It can create files and folders and put codes inside., (*1)

Steps
  • Prepare code generation template
  • Create a seed file
  • Run from command line

Usage

If you have PHP installed and if its callable from your terminal just download this repository and run this command: (for windows there is a bat file so you can just use "marsgen" instead of "php marsgen.php"), (*2)

D:\marsgen>php marsgen.php generate /example/planets /example/planet_seed.php /output

This command will fetch every file under /example/planets folder and it will load config from /example/planet_seed.php and generate all files and folders under /output folder., (*3)

Seed file and output folder are optional. If no output folder defined it will generate files on the folder where command called from., (*4)

Templating and Syntax

Template file names and folder names may have variables. Lets say you want to generate VenusModel.php and VenusView.php you can name the template files as -Planet-Model.php and -Planet-View.php and set what to replace in seed file. Inside __FileSystem__ everything will be replaced by using str_replace() function., (*5)

$seed['__FileSystem__'] = [
  '-Planet-' => 'Venus'
];
$seed['myVar'] = 'Hello World';
$seed['myArray'] = [['name'=>'item1'],['name'=>'item2']];

In template files you can place variables with /*{myVar}*/ syntax with above seed file this will generate Hello world as output., (*6)

You can loop code blocks with "for" /*{for:myArray}*/ Name value is /*{myArray>name}*/. /*{/for*/} This "for" loop will output: Name value is: item1. Name value is: item2. You can use isset blocks inside for blocks. Probably you already noticed syntax for accessing array values is: /*{myArray>name}*/, (*7)

Isset block is simple /*{isset:myVar}*/ myVar's value is /*{myVar}*/ /*{/isset}*/, (*8)

Sample

Template file: SourceFolder/Controllers/-Planet-Controller.php, (*9)

<?php 
namespace Planets\Controllers;

use Planets\Models\/*{the_planet}*/Model;
use Planets\Models\/*{the_planet}*/View;

/*
* Mercury Class
*/
class /*{the_planet}*/Controller extends BaseController
{

  function __construct()
  {
    # code...
  }

  function get(){
    //Some Values
    $/*{first_planet}*/ = 'First Planet';
    $/*{second_planet}*/ = 'Second Planet';
    $/*{third_planet}*/ = 'Blue Marble';
    $/*{fourth_planet}*/ = 'Second Home';

    //Let's use for each
    /*{for:outter_planets}*/
    $/*{outter_planets>name}*/Distance = '/*{outter_planets>distance}*/';/*{/for}*/ 

    $model = New /*{the_planet}*/Model();
    $view = New /*{the_planet}*/View();
  }
}

Template file: MercurySeedFile.php, (*10)

<?php
$seed = [];
$seed['__FileSystem__'] = [
  '-Planet-' => 'Mercury',
  '-Echo-' => 'Hello'
];

$seed['the_planet'] = 'Mercury';
$seed['first_planet'] = 'Mercury';
$seed['second_planet'] = 'Venus';
$seed['third_planet'] = 'Earth';
$seed['fourth_planet'] = 'Mars';

$seed['outter_planets'] = [
  ['name' => 'Jupiter', 'distance' =>'5.2 AU'],
  ['name' => 'Saturn', 'distance' =>'9.5 AU'],
  ['name' => 'Uranus', 'distance' =>'19.2 AU'],
  ['name' => 'Neptune', 'distance' =>'30.1 AU']
];

return $seed;

Result: TargetFolder/Controllers/MercuryController.php, (*11)

<?php 
namespace Planets\Controllers;

use Planets\Models\MercuryModel;
use Planets\Models\MercuryView;

/*
* Mercury Class
*/
class MercuryController extends BaseController
{

  function __construct()
  {
    # code...
  }

  function get(){
    //Some Values
    $Mercury = 'First Planet';
    $Venus = 'Second Planet';
    $Earth = 'Blue Marble';
    $Mars = 'Second Home';

    //Let's use for each
    $JupiterDistance = '5.2 AU';
    $SaturnDistance = '9.5 AU';
    $UranusDistance = '19.2 AU';
    $NeptuneDistance = '30.1 AU'; 

    $model = New MercuryModel();
    $view = New MercuryView();
  }
}

Notes:

  • Nested foreach() and isset() controls are not supported.
  • There is no else condition for isset() function for now.

This is a personal helper repository for one of my projects so I only included functions what I needed. If you add more functionality please send a pull request to my repository so everyone can enjoy it :), (*12)

Roadmap:

Current version is enough for my needs but I may add these features to template parser later. - Option to add values from command line instead of seed file - Else condition for isset(). - If/else condition. - Switch/Case condition. - Nested foreach support. - Global command line support., (*13)

The Versions

25/04 2017

dev-master

9999999-dev

A PHP code generation and scaffolding tool.

  Sources   Download

MIT

The Requires