Giter Club home page Giter Club logo

code-builder's Introduction

Code Builder

Build Status

This library can be used to generate or idempotently add classes, methods, properties, use statements, etc. to existing source code using prototypes.

A prototype is an object which defines structural code elements.

Usage

The library provides a source code prototype builder:

$builder = SourceBuilder::create()
    ->namespace('Animals');
    ->use('Measurements\\Height');
    ->class('Rabbits')
        ->extends('Leopridae')
        ->property('force')
            ->visibility('private')
            ->type('int')
            ->defaultValue(5)
        ->end()
        ->method('jump')
            ->parameters()
                ->parameter('how')
                    ->default('high')
                    ->type('Height')
                ->end();
            ->end()
        ->end()
    ->end();

$sourcePrototype = $builder->build();

the above prototype can either be used to generate a new class:

$renderer = new TwigRenderer();
$renderer->render($sourcePrototype);

Or it can be applied to an existing source code, given the following:

<?php

class Rabbits
{
}

When we do:

$updater = new TolerantUpdater();
$updater->apply($sourcePrototype, Code::fromString(file_get_contents('Rabbits.php')));

Then we get:

<?php

namespace Animals;

use Measurements\Height;

class Rabbits extends Leopridae
{
    private $force = 5;

    public function jump(Height $how = 'high')
    {
    }
}

About this project

This library is part of the phpactor project.

code-builder's People

Contributors

dantleech avatar greg0ire avatar

Watchers

 avatar  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.