corneltek/codegen

PHP Code Generation Library

3.2.2 2024-02-11 07:35 UTC

README

Build Status Latest Stable Version Total Downloads Monthly Downloads Daily Downloads Latest Unstable Version License

Transform your dynamic calls to static calls!

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:

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:

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:

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:

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/');