2017 © Pedro PelĂĄez
 

library twig-generator

A generator for PHP code based on Twig template engine

image

symfony2admingenerator/twig-generator

A generator for PHP code based on Twig template engine

  • Saturday, July 22, 2017
  • by sescandell
  • Repository
  • 4 Watchers
  • 1 Stars
  • 13,298 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 20 Forks
  • 2 Open issues
  • 10 Versions
  • 3 % Grown

The README.md

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., (*1)

Installation

Use composer to take advantage of this bundle:, (*2)

composer require "symfony2admingenerator/twig-generator"

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., (*3)

Creating a Builder class

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

<?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., (*5)

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., (*6)

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

<?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):, (*8)

{% 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:, (*9)

<?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:, (*10)

<?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., (*11)

Running the tests

After installing dependencies through composer, you can use phpunit from vendor files:, (*12)

   vendor/phpunit/phpunit/phpunit

The Versions

22/07 2017

dev-master

9999999-dev

A generator for PHP code based on Twig template engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cedric LOMBARDOT
by François Zaninotto

twig generator code generation

22/07 2017

v1.3.1

1.3.1.0

A generator for PHP code based on Twig template engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cedric LOMBARDOT
by François Zaninotto

twig generator code generation

21/07 2017

v1.3.0

1.3.0.0

A generator for PHP code based on Twig template engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cedric LOMBARDOT
by François Zaninotto

twig generator code generation

10/01 2016

v1.2.2

1.2.2.0

A generator for PHP code based on Twig template engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cedric LOMBARDOT
by François Zaninotto

twig generator code generation

21/11 2014

v1.1.4

1.1.4.0

A generator for PHP code based on Twig template engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cedric LOMBARDOT
by François Zaninotto

twig generator code generation

21/11 2014

v1.1.3

1.1.3.0

A generator for PHP code based on Twig template engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cedric LOMBARDOT
by François Zaninotto

twig generator code generation

20/11 2014

v1.1.2

1.1.2.0

A generator for PHP code based on Twig template engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cedric LOMBARDOT
by François Zaninotto

twig generator code generation

20/11 2014

v1.1.1

1.1.1.0

A generator for PHP code based on Twig template engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cedric LOMBARDOT
by François Zaninotto

twig generator code generation

17/11 2014

v1.1.0

1.1.0.0

A generator for PHP code based on Twig template engine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cedric LOMBARDOT
by François Zaninotto

twig generator code generation

10/11 2012

v1.0.0

1.0.0.0

A generator for PHP code based on Twig template engine

  Sources   Download

MIT

The Requires

 

by Cedric LOMBARDOT
by François Zaninotto

twig generator code generation