Skip to content

dshoreman/code-builder

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

119 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Build or modify source code

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • PHP 97.8%
  • HTML 2.2%