Giter Club home page Giter Club logo

cg's Introduction

Code generation library

This project aims to provide tools for easy code generation and manipulation. Since we're relying on metaprogramming for a lot of our libraries (Doctrine, Symfony, etc), generating code for this may get a bit tedious. This library aims to provide easy code generation for:

  • General purpose PHP files and classes
  • Doctrine entities
  • Symfony bundles
  • An alternate implementation for the symfony container builder

Currently, the library is still under development, and if you want to know what's up or would like to help, let me know! You can create issues here, or contact me directly at [email protected].

But, Doctrine and Symfony already have code generators, why do I need this one?

Melp\Cg provides a document-oriented approach to generating and inflecting code. Applying changes to code can easily be implemented using the provided visitor implementation. This makes both the generation and the use of the code generator very easy and understandable. The aim is to create a simple yet powerful interface to easily generate but also modify existing code.

Building code

A builder pattern is provided for quick structural setup, for example:

<?php

echo (string)
    (new Buffer())->append(
        new Builder()
            ->file()
                ->docComment()
                    ->author('Gerard van Helden <[email protected]>')
                ->end()
                ->namespacex('foo\\bar')
                    ->functionx('div')
                        ->arg()
                            ->name('a')
                        ->end()
                        ->arg()
                            ->name('b')
                        ->end()
                        ->raw('return $a / $b;')->end()
                    ->end()
                ->end()
            ->peek()
    );
;

Renders the following code:

<?php
/**
 * @author Gerard van Helden <[email protected]>
 */
namespace foo\bar
{
    function div($a, $b)
    {
        return $a / $b;
    }
}

Using visitors

There are some default visitors available which help you with the most common of tasks, for example, applying the SingleNamespaceVisitor would cause the namespace to be a single statement on top of the file, if there is only one namespace configured:

use Melp\Cg\Common\Buffer;
use Melp\Cg\Common\Iterator\Traverser;
use Melp\Cg\Php\Builder;
use Melp\Cg\Php\Visitor\SingleNamespaceVisitor;

$node = (new Builder())
    ->file()
        ->namespacex('foo\\bar')
            ->raw('// This is part of the namespace' . PHP_EOL)->end()
        ->end()
    ->peek();

$buffer = new Buffer();
echo (string)$buffer->append($node);

$traverser = new Traverser();
$traverser->addVisitor(new SingleNamespaceVisitor());
$traverser->traverse($node);

$buffer->reset();
echo (string)$buffer->append($node);

Renders:

<?php
namespace foo\bar
{
    // This is part of the namespace
}
<?php
namespace foo\bar;
// This is part of the namespace

Inflection and mutation

You can get an inflectable document by using the included parser library which parses the general structure of a document.

Applications

cg's People

Contributors

drm avatar

Watchers

 avatar  avatar

Forkers

nestor

cg's Issues

Awesome developer experience

Just an issue to notify you that The stack is empty. You should probably peek() before the last end() is a great error message. Thanks for thinking about developer experience ๐Ÿ‘

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.