2017 © Pedro Peláez
 

library codegen

PHP Code Generation Library

image

corneltek/codegen

PHP Code Generation Library

  • Saturday, June 3, 2017
  • by c9s
  • Repository
  • 6 Watchers
  • 33 Stars
  • 68,212 Installations
  • PHP
  • 22 Dependents
  • 0 Suggesters
  • 2 Forks
  • 12 Open issues
  • 33 Versions
  • 7 % Grown

The README.md

CodeGen

Build Status Latest Stable Version Total Downloads Monthly Downloads Daily Downloads Latest Unstable Version License, (*1)

Transform your dynamic calls to static calls!, (*2)

Expressions

ConcatExpr

$concat = new ConcatExpr('foo1', 'bar2');
$concat // 'foo1' . 'bar2'

AssignExpr

$assign = new AssignExpr('$foo', 10);
echo $assign; // $foo = 10

Wrapping Expr with Statement object

$statement = new Statement($assign);
echo $statement; // $foo = 10;

UserClass

Creating UserClass

use CodeGen\UserClass;
$cls = new UserClass('FooClass');
$code = $cls->render();

Implementing an interface

$cls = new UserClass('FooClass');
$class->implementInterface('ArrayAccess');

Adding properties

$cls->addPublicProperty('foo');
$cls->addPublicProperty('foo', 1);
$cls->addPublicProperty('foo', ['foo','bar']);
$cls->addProtectedProperty('foo');
$cls->addProtectedProperty('foo', 1);
$cls->addProtectedProperty('foo', ['foo','bar']);

Adding class methods

$cls->addMethod('public','getName',[],['return $this->name;']);
$cls->addMethod('public','setName',['$name'],['$this->name = $name;']);

Generating class file by PSR-0 or PSR-4

$cls->generatePsr0ClassUnder('src'); // This places 'Foo\Bar' at src/Foo/Bar.php
$cls->generatePsr4ClassUnder('src/Zoo'); // This places 'My\Foo\Bar' at src/Zoo/Bar.php

Generators

ArrayAccessGenerator

$generator = new ArrayAccessGenerator;
$userClass = new UserClass('MyZoo');
$userClass->addPublicProperty('animals', array(
    'tiger' => 'John',
    'cat'   => 'Lisa',
));
$generator->generate('animals', $userClass);
$userClass->requireAt('tests/generated/my_zoo.fixture');
$zoo = new MyZoo;

AppClassGenerator

$foo = new FooClass(1,2);
$generator = new AppClassGenerator(array(
    'prefix' => 'OhMy',
));

$appClass = $generator->generate($foo);
// echo $appClass->render();

$this->assertCodeEqualsFile('tests/data/app_class_generator_ohmyfoo.fixture', $appClass);

$path = $appClass->generatePsr4ClassUnder('tests/generated'); 
$this->assertFileExists($path);
require_once($path);

$this->assertTrue(class_exists('OhMyFooClass'));

$ohMyFoo = new OhMyFooClass;
$this->assertEquals(1, $ohMyFoo->foo);

Statements

Generating require_once statement

use CodeGen\Constant;
use CodeGen\Statement\RequireOnceStatement;
$varfile = new Constant('file.php');
$requireStmt = new RequireOnceStatement($varfile);

The code above generates:, (*3)

require_once 'file.php';

Generating require statement

use CodeGen\Constant;
use CodeGen\Statement\RequireStatement;
$varfile = new Constant('file.php');
$requireStmt = new RequireStatement($varfile);

The code above generates:, (*4)

require 'file.php';
use CodeGen\Constant;
use CodeGen\Variable;
use CodeGen\Statement\RequireStatement;
$varfile = new Variable('$file');
$requireStmt = new RequireStatement($varfile);

The code above generates:, (*5)

require $file;

Generating if isset statement condition

$foo = new Variable('$foo');
$ifFoo = new IfIssetStatement($foo, ['key', 'key2', 0], function() use ($foo) {
    $block = new Block;
    $block[] = new Statement(new AssignExpr($foo, new Constant(30)));
    return $block;
});

The code above generates:, (*6)

if (isset($foo['key']['key2'][0])) {
    $foo = 30;
}

Framework Generators

PHPUnit TestCase Generator

Apache Config Generator

use CodeGen\Generator\AccessorClassGenerator;
use CodeGen\Frameworks\Apache2\VirtualHostDirectiveGroup;
$g = new AccessorClassGenerator([
    'prefix' => 'App',
]);
$appClass = $g->generate(new VirtualHostDirectiveGroup);
$appClass->generatePsr4ClassUnder('src/CodeGen/Frameworks/Apache2/');
use CodeGen\Generator\AccessorClassGenerator;
use CodeGen\Frameworks\Apache2\VirtualHostDirectiveGroup;
$g = new AccessorClassGenerator([
    'namespace' => 'CodeGen\Frameworks\Apache2',
    'class_name' => 'ApacheSiteConfig',
]);
$appClass = $g->generate(new VirtualHostDirectiveGroup);
$appClass->generatePsr4ClassUnder('src/CodeGen/Frameworks/Apache2/');

The Versions

03/06 2017

dev-master

9999999-dev http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

18/05 2017

3.2.1

3.2.1.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

18/05 2017

3.2.0

3.2.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

17/05 2017

3.1.2

3.1.2.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

17/01 2017

3.1.1

3.1.1.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

15/01 2017

3.1.0

3.1.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

10/06 2016

3.0.0

3.0.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

26/03 2016

2.7.1

2.7.1.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

25/12 2015

2.7.0

2.7.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

07/11 2015

dev-codegen-app

dev-codegen-app http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

21/10 2015

2.6.0

2.6.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

08/10 2015

2.5.0

2.5.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

28/09 2015

2.4.0

2.4.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

28/09 2015

2.3.1

2.3.1.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

25/08 2015

2.3.0

2.3.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

23/08 2015

2.2.0

2.2.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

17/07 2015

2.1.0

2.1.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

24/06 2015

2.0.3

2.0.3.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

15/04 2015

2.0.2

2.0.2.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

14/04 2015

2.0.1

2.0.1.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

14/04 2015

2.0.0

2.0.0.0 http://github.com/c9s/CodeGen

PHP Code Generation Library

  Sources   Download

MIT

The Requires

 

The Development Requires

17/03 2015

1.4.5

1.4.5.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

11/02 2015

1.4.4

1.4.4.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

08/02 2015

1.4.3

1.4.3.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

03/10 2014

1.4.2

1.4.2.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

30/09 2014

1.4.1

1.4.1.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

30/09 2014

1.4.0

1.4.0.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

29/09 2014

1.3.1

1.3.1.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

26/04 2014

1.3.0

1.3.0.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

25/04 2014

1.2.0

1.2.0.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

25/04 2014

1.1.0

1.1.0.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

25/04 2014

1.0.0

1.0.0.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

25/04 2014

0.2.0

0.2.0.0 http://github.com/c9s/ClassTemplate

Class template Utilities

  Sources   Download

MIT

The Requires

 

The Development Requires