PHP code example of corneltek / codegen

1. Go to this page and download the library: Download corneltek/codegen library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

corneltek / codegen example snippets


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

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

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

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

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

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

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

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

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

$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

$generator = new ArrayAccessGenerator;
$userClass = new UserClass('MyZoo');
$userClass->addPublicProperty('animals', array(
    'tiger' => 'John',
    'cat'   => 'Lisa',
));
$generator->generate('animals', $userClass);
$userClass->

$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);

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



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



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



$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;
});

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

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