Codebuilder is a php tool to generates php code, you can create any php code that you need.
You can build php code througth php classes using something like this.
// You can create php code with php through the CodeBuilder toolkit.
$comment = new Annotations\Comment("This is a comment for a class method");
$comment->add(new Annotations\PHPDocs(
DOCS_PARAM,
"string",
new Expressions\Variable("comemntParam")
));
// This is the class method.
$method = new Classes\ClassMethod("methodForAClass");
$method->add($comment);
// The class with a Statementblock, that means a { } curly braces when the code is included.
$classes = new Classes\ClassComponent("ClassName");
$classes->add(new Statements\StatementBlock($method));
// Finally the php tags.
$tag = new Classes\Tags($classes);
Examples/class.builder.php
-> Examples/outputs/class.output.php
<?php
class ClassName
{
/**
* This is a comment for class method
*
* @param string $comemntParam
*/
public function methodForAClass()
{
}
}
- Intro
- Expressions
- Grouping
- Statements
- Statics
- Structural functions
- Creating classes
- Comments & Annotations
- TODO
CodeBuilder is a builder from php to creates php code with separated class helpers.
Expressions works to grouping classes, objects and anything following a precedence rules.
We are going to start with a variable creation, this is the most simple component to represent a value.
use CodeBuilder\Expressions\Variable
Method | Params |
---|---|
asArray | ($index: Base) |
$var = new Variable("var");
echo $var->resolve();
$var
// becomes in array variable
$var = new Variable("var");
$var->asArray(new CodeBuilder\Expressions\Literals\StringLiteral("assoc_index"));
echo $var->resolve();
$var["assoc_index"]
There are several types of expressions when we want grouping the language components, something like unary that means 2 components.
Is a group of two types of components.
use CodeBuilder\Expressions\Unary
$expression = new Unary(
new Variable("exp1"),
";"
);
echo $expression->resolve();
$name;
Is a group of three types of components.
use CodeBuilder\Expressions\Binary
$expression = new Binary(
new Variable("name"),
"=",
new StringLiteral("text!!!");
);
echo $expression->resolve();
$name = "text!!!"
$expression = new Unary(
new Binary(
new Variable("name"),
"=",
new StringLiteral("text!!!");
),
";"
);
echo $expression->resolve();
$name = "text!!!";
use CodeBuilder\Expressions\Ternary;
$ternary = new Ternary(
new Unary(
"!",
new Variable("name")
),
new Variable("name"),
new StringLiteral("")
);
echo $ternary->resolve();
!$name ? $name : ""
There are a lot of operator types, but we can intentify by types of them for example logic or arithmetic operators.
class Arithmetic extends Operator
{
const ADD = '+';
const SUB = '-';
const MULT = '*';
const DIV = '/';
const MOD = '%';
}
$bin = new Binary(
new Variable("num1"),
Arithmetic.ADD,
new Unary(new Variable("num2"), ";")
);
echo $bin->resolve();
$num1 + $num2;
class Combined extends Operator
{
const ADD = '+=';
const SUB = '-=';
const MUL = '*=';
const DIV = '/=';
const STR = '.=';
const CONCAT = '.';
const COMMA = ',';
}
$bin = new Binary(
new Variable("num1"),
Combined.ADD,
new Unary(new Variable("num2"), ";")
);
echo $bin->resolve();
$num1 += $num2;
class Comparison extends Operator
{
const SET = '=';
const ARRAY_ASSIGN = '=>';
const EQUAL = '==';
const LESS_THAN = '<';
const NOT_EQUAL = '!=';
const BLANK_SPACE = '=';
const IDENTICAL = '===';
const GREATER_THAN = '>';
const STATIC_CLASS = '::';
const NOT_IDENTICAL = '!==';
const LESS_THAN_EQUAL = '<=';
const GREATER_THAN_EQUAL = '>=';
const _INSTANCEOF = 'instanceof';
}
$bin = new Binary(
new Variable("num1"),
Comparison._INSTANCEOF,
new Unary(new Variable("num2"), ";")
);
echo $bin->resolve();
$num1 instanceof $num2;
Just a ++ operator
class Increment extends Operator
{
const INCREMENT = '++';
const PRE_INCREMENT = 'PRE_INC';
}
$bin = new Unary(new Variable("index"), Increment.INCREMENT);
echo $bin->resolve();
$index++
Just a -- operator
class Decrement extends Operator
{
const DECREMENT = '--';
const PRE_DECREMENT = 'PRE_DEC';
}
$bin = new Unary(new Variable("index"), Decrement.DECREMENT);
echo $bin->resolve();
$index--
class Logical extends Operator
{
const _AND_ = 'and';
const _OR_ = 'or';
const _AND = '&&';
const _OR = '||';
const _XOR = '!';
const _AND_BIN = '&';
const _OR_BIN = '|';
const _XOR_BIN = '^';
const _SR = '>>';
const _SL = '<<';
const _NOT_BIN = '~';
}
$bin = new Binary(
new Variable("sum"),
Decrement._AND_BIN,
new Variable("subs")
);
echo $bin->resolve();
$sum & $subs
To represent a literal type of value for code generated.
.
.
$expr = new Unary(
"echo",
new StringLiteral("hi literal!")
)
echo $expr->resolve();
echo "hi literal!"
But arrays in php is more complicated.
new ArrayLiteral(val: Base)
Methods | Example |
---|---|
asFunction | array('val1', 'val2', val3') |
getClass | static |
$arr = new ArrayLiteral(new Variable("values"));
$arr->add(new StringLiteral("str"));
$arr->add(new IntegerLiteral(123));
$arr->add(new BooleanLiteral(true));
$arr->add(new DoubleLiteral(1.0));
echo $arr->resolve();
$values["str", 123, true, 1.0]
// $arr->asFunction();
array("str", 123, true, 1.0)
Class functions to group expressions and literals.
$b = new Brackets(new Unary("return", ";"));
echo $b->resolve();
{ return; }
$b = new Parenthesis(new Binary(new Variable("a"), "+", new Variable("b")));
echo $b->resolve();
( $a + $b )
$b = new SquareBrackets(new StringLiteral("square"));
echo $b->resolve();
[ "square" ]
This class creates a class struct in php receives in construct a string with the name of class to be created.
CodeBuilder\Classes\ClassComponent
ClassComponent($className: String)
->getClass()
- returns a string name class
ClassName::class
- returns a string name class
->getName()
- returns a class name assigned.
addExtends($name: String)
- Receives a string extends name, basically the class name where extending.
addImplements($name: String)
- Receives a string implements name, basically the interface name that you wants to implement.
add($component: Base)
- Receives a component object compatible with class creation component.
$method = new Classes\ClassMethod("methodForAClass");
$method->add($comment);
$method->add(new Statements\ReturnStatement(
new Expressions\Unary(
new Literals\StringLiteral("Hi!!"),
";"
),
));
$classes = new Classes\ClassComponent("ClassName");
$classes->add(new Statements\StatementBlock($method));
echo $classes->resolve();
<?php
class ClassName
{
public function methodForAClass()
{
return "Hi!!";;
}
}
CodeBuilder\Classes\ClassAttribute
This class receive a Variable object and creates an attribute property in the class.
ClassAttribute($className: Expression\Variable)
getName()
- returns a string name assigned.
addVisibility($attrVisibility: String)
- Receives a string with visibility option, default
public
.
- Receives a string with visibility option, default
Example in CodeBuilder/Examples/class.attr.builder.php
$attr = new Classes\ClassAttribute(
new Expressions\Variable("attributeClass")
);
$attr->addVisibility("protected");
$attr->add($comment);
<?php
class ClassName
{
protected $attributeClass;
}
CodeBuilder\Classes\ClassNamespace
This class receive a class namespace as string.
namespace \Example;
ClassNamespace($namespace: String)
add($ns: String|Base)
- You can add a comment for the base namespace additionally.
- The next additions in the add method becomes in a
use
for the class.
$ns = new Classes\ClassNamespace("BaseNamespace\Created\FromPHP");
$ns->add("\BaseNamespace\Test");
$ns->add("\BaseNamespace\Test2");
<?php
namespace BaseNamespace\Created\FromPHP;
use \BaseNamespace\Test;
use \BaseNamespace\Test2;
class ClassName
{
}
CodeBuilder\Classes\ClassTrait
This class receive a use trait as string.
use \Example\Trait;
ClassTrait($trait: String)
add($trait: String)
- These additions in the add method becomes in a
use
for the class.
- These additions in the add method becomes in a
Example in CodeBuilder/Examples/class.trait.builder.php
$ns = new Classes\ClassTrait("BaseTrait\Created\FromPHP");
$ns->add("\BaseTrait\Test");
<?php
class ClassName
{
use BaseTrait\Created\FromPHP, \BaseTrait\Test;
}