Skip to content

phalcondroid/CodeBuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeBuilder for PHP

Codebuilder is a php tool to generates php code, you can create any php code that you need.

Quick sample

You can build php code througth php classes using something like this.

How to define a comment.

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

Defining a class method.

// This is the class method.
$method  = new Classes\ClassMethod("methodForAClass");
$method->add($comment);

Defining a class.

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

Defining a php tag <?php.

// Finally the php tags.
$tag = new Classes\Tags($classes);

Output

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()
    {
    }
}

Documentation

Table of Contents

  1. Intro
  2. Expressions
  3. Grouping
  4. Statements
  5. Statics
  6. Structural functions
  7. Creating classes
  8. Comments & Annotations
  9. TODO

Intro

CodeBuilder is a builder from php to creates php code with separated class helpers.

Expressions

Expressions works to grouping classes, objects and anything following a precedence rules.

Creating a variable

We are going to start with a variable creation, this is the most simple component to represent a value.

Variable namespace

use CodeBuilder\Expressions\Variable

Functionalities
Method Params
asArray ($index: Base)
Example #1
$var = new Variable("var");
echo $var->resolve();
Output
$var
Example #2
// becomes in array variable
$var = new Variable("var");
$var->asArray(new CodeBuilder\Expressions\Literals\StringLiteral("assoc_index"));
echo $var->resolve();
Output
$var["assoc_index"]

Understanding expression types

There are several types of expressions when we want grouping the language components, something like unary that means 2 components.

Unary expression

Is a group of two types of components.

use CodeBuilder\Expressions\Unary

$expression = new Unary(
    new Variable("exp1"),
    ";"
);
echo $expression->resolve();
Output
$name;

Binary expression

Is a group of three types of components.

use CodeBuilder\Expressions\Binary

$expression = new Binary(
    new Variable("name"),
    "=",
    new StringLiteral("text!!!");
);
echo $expression->resolve();
Output
$name = "text!!!"
Combined
$expression = new Unary(
    new Binary(
        new Variable("name"),
        "=",
        new StringLiteral("text!!!");
    ),
    ";"
);
echo $expression->resolve();
Output
$name = "text!!!";

Ternary expression

use CodeBuilder\Expressions\Ternary;

$ternary = new Ternary(
    new Unary(
        "!",
        new Variable("name")
    ),
    new Variable("name"),
    new StringLiteral("")
);
echo $ternary->resolve();
Output
!$name ? $name : ""

Operator types

There are a lot of operator types, but we can intentify by types of them for example logic or arithmetic operators.

Arithmetic

class Arithmetic extends Operator
{
    const ADD = '+';
    const SUB = '-';
    const MULT = '*';
    const DIV = '/';
    const MOD = '%';
}
Example
$bin = new Binary(
    new Variable("num1"),
    Arithmetic.ADD,
    new Unary(new Variable("num2"), ";")
);
echo $bin->resolve();
Output
$num1 + $num2;

Combine

class Combined extends Operator
{
    const ADD = '+=';
    const SUB = '-=';
    const MUL = '*=';
    const DIV = '/=';
    const STR = '.=';
    const CONCAT = '.';
    const COMMA = ',';
}
Example
$bin = new Binary(
    new Variable("num1"),
    Combined.ADD,
    new Unary(new Variable("num2"), ";")
);
echo $bin->resolve();
Output
$num1 += $num2;

Comparison

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';
}
Example
$bin = new Binary(
    new Variable("num1"),
    Comparison._INSTANCEOF,
    new Unary(new Variable("num2"), ";")
);
echo $bin->resolve();
Output
$num1 instanceof $num2;

Increment

Just a ++ operator

class Increment extends Operator
{
    const INCREMENT = '++';
    const PRE_INCREMENT = 'PRE_INC';
}
Example
$bin = new Unary(new Variable("index"), Increment.INCREMENT);
echo $bin->resolve();
Output
$index++

Decrement

Just a -- operator

class Decrement extends Operator
{
    const DECREMENT = '--';
    const PRE_DECREMENT = 'PRE_DEC';
}
Example
$bin = new Unary(new Variable("index"), Decrement.DECREMENT);
echo $bin->resolve();
Output
$index--

Logical

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 = '~';
}
Example
$bin = new Binary(
    new Variable("sum"),
    Decrement._AND_BIN,
    new Variable("subs")
);
echo $bin->resolve();
Output
$sum & $subs

Literals

To represent a literal type of value for code generated.

.

String
Object
Null
Integer
Double
Constant
Char
Boolean

.

Example
$expr = new Unary(
    "echo",
    new StringLiteral("hi literal!")
)
echo $expr->resolve();
Output
echo "hi literal!"
Array

But arrays in php is more complicated.

new ArrayLiteral(val: Base)

Methods Example
asFunction array('val1', 'val2', val3')
getClass static
Example
$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();
Output
$values["str", 123, true, 1.0]

// $arr->asFunction();
array("str", 123, true, 1.0)

Grouping

Class functions to group expressions and literals.

Brackets

Example
$b = new Brackets(new Unary("return", ";"));
echo $b->resolve();
Output
{ return; }

Parenthesis

Example
$b = new Parenthesis(new Binary(new Variable("a"), "+", new Variable("b")));
echo $b->resolve();
Output
( $a + $b )

Square brackets

Example
$b = new SquareBrackets(new StringLiteral("square"));
echo $b->resolve();
Output
[ "square" ]

Creating classes

This class creates a class struct in php receives in construct a string with the name of class to be created.

CodeBuilder\Classes\ClassComponent

Constructor

ClassComponent($className: String)

Getters

  • ->getClass()
    • returns a string name class ClassName::class
  • ->getName()
    • returns a class name assigned.

Setters

  • 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.
Example
$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();
Output
<?php

class ClassName
{
    public function methodForAClass()
    {
        return "Hi!!";;
    }
}

Class attributes

CodeBuilder\Classes\ClassAttribute

This class receive a Variable object and creates an attribute property in the class.

Constructor

ClassAttribute($className: Expression\Variable)

Getters

  • getName()
    • returns a string name assigned.

Setters

  • addVisibility($attrVisibility: String)
    • Receives a string with visibility option, default public.

Example in CodeBuilder/Examples/class.attr.builder.php

Creates an Attribute

$attr = new Classes\ClassAttribute(
    new Expressions\Variable("attributeClass")
);
$attr->addVisibility("protected");
$attr->add($comment);

Output

<?php

class ClassName
{
    protected $attributeClass;
}

Namespaces

CodeBuilder\Classes\ClassNamespace

This class receive a class namespace as string.

namespace \Example;

Constructor

ClassNamespace($namespace: String)

Setters

  • 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.
Example
$ns = new Classes\ClassNamespace("BaseNamespace\Created\FromPHP");
$ns->add("\BaseNamespace\Test");
$ns->add("\BaseNamespace\Test2");
Output
<?php

namespace BaseNamespace\Created\FromPHP;

use \BaseNamespace\Test;
use \BaseNamespace\Test2;

class ClassName
{
}

Traits

CodeBuilder\Classes\ClassTrait

This class receive a use trait as string.

use \Example\Trait;

Constructor

ClassTrait($trait: String)

Setters

  • add($trait: String)
    • These additions in the add method becomes in a use for the class.

Example in CodeBuilder/Examples/class.trait.builder.php

$ns = new Classes\ClassTrait("BaseTrait\Created\FromPHP");
$ns->add("\BaseTrait\Test");

Output

<?php

class ClassName
{
    use BaseTrait\Created\FromPHP, \BaseTrait\Test;
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages