Coder Social home page Coder Social logo

twiggenerator's Introduction

TwigGenerator project status# build status#

TwigGenerator is a PHP code generator based on the Twig template engine. It leverages the power of Twig templates to simplify the generation of PHP code, to make it more extensible, and more readable.

Installation

Checkout this GitHub repository and the two submodules (Twig and Symfony ClassLoader):

git clone https://github.com/cedriclombardot/TwigGenerator.git
cd TwigGenerator
wget -nc http://getcomposer.org/composer.phar
php composer.phar install

Usage

To generate PHP classes, you need to create a "Builder", and one or more Twig templates. Then, add the new Builder to a "Generator", and generate the result.

Creating a Builder class

First, create a class extending TwigGenerator\Builder\BaseBuilder - no need for methods at start.

<?php

namespace MyProject\Builder;

use TwigGenerator\Builder\BaseBuilder;

class MyBuilder extends BaseBuilder
{
}

Tip: Alternatively, a builder can implement the TwigGenerator\Builder\BuilderInterface if it has to extend a custom class.

Creating Twig Templates

Next, create a couple twig templates under the templates/ directory. Usually, you need at least one template for the main structure, plus one template per feature added to the class.

Here is an example main template (or layout) for creating a custom PHP class (to be stored in templates/_base/common.php.twig):

<?php
{{ namespace is defined ? "namespace " ~ namespace ~ ";" : "" }}

class {{ className }} {{ extends is defined ? "extends " ~ extends : "" }}
{
{% block functions %}
{% endblock %}
}

And now, an example for adding a custom method (to be stored in templates/MyBuilder.php.twig):

{% extends "_base/common.php.twig" %}

{% block functions %}
	public function tellMeHello()
	{
		echo "Hello world";
	}
{% endblock %}

Generating the code

Use a TwigGenerator\Builder\Generator instance to generate the result. For instance:

<?php
// initialize the autoload
require_once '/path/to/TwigGenerator/src/autoload.php';
// alternatively, use your favorite PSR-0 autoloader configured with TwigGenerator, Symfony and Twig

// initialize a builder
$builder = new MyProject\Builder\MyBuilder();
$builder->setOutputName('MyBuilder.php');

// add specific configuration for my builder
$builder->setVariable('className', 'MyBuilder');

// create a generator
$generator = new TwigGenerator\Builder\Generator();
$generator->setTemplateDirs(array(
	__DIR__.'/templates',
));

// allways regenerate classes even if they exist -> no cache
$generator->setMustOverwriteIfExists(true);

// set common variables
$generator->setVariables(array(
	'namespace' => 'MyProject\Generated',
));

// add the builder to the generator
$generator->addBuilder($builder);

// You can add other builders here

// Run generation for all builders
$generator->writeOnDisk(__DIR__.'/Generated');

The file will be generated in MyProject\Generated\MyBuilder.php, as follows:

<?php
namespace MyProject\Generated;

class MyBuilder
{
	public function tellMeHello()
	{
		echo "Hello world";
	}
}

Other Examples

You can see some basic code generation samples in the tests, and on some GitHub repositories like fzaninotto/Doctrine2ActiveRecord, or cedriclombardot/AdmingeneratorGeneratorBundle.

Unit Tests

Then, just run:

phpunit

twiggenerator's People

Contributors

cedriclombardot avatar czogori avatar ludofleury avatar willdurand avatar fzaninotto avatar vworldat avatar yosmanyga avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.