PHP code example of jameslevi / stencil

1. Go to this page and download the library: Download jameslevi/stencil 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/ */

    

jameslevi / stencil example snippets




if(file_exists(__DIR__.'/vendor/autoload.php'))
{
    



use Graphite\Component\Stencil\Stencil;

// Instantiate a new Stencil object.
$php = new Stencil("UserController");

// Declare the namespace of the PHP class.
$php->setNamespace("App\Controller");

// Indicate the class name of the PHP class.
$php->setClassname("UserController");

// Extend to a parent class.
$php->extends("Controller");

// Generate the PHP file.
$php->generate(__DIR__ . "/");



namespace App\Controller;

class UserController extends Controller
{
}

$php->use("Carbon\Carbon");

$php->use("Carbon\Carbon", "MyDateTime");

use Carbon\Carbon as MyDateTime;

// Import the carbon class and use the alias MyDateTime.
$php->use("Carbon\Carbon", "MyDateTime");

// Extend MyDateTime to the class.
$php->extends("MyDateTime");

// Declare the class name.
$php->setClassname("MyClass");

// Import your interfaces.
$php->use("MyInterface1");
$php->use("MyInterface2");

// Implement the interfaces.
$php->implement("MyInterface1");
$php->implement("MyInterface2");

use MyInterface1;
use MyInterface2;

class MyClass implements MyInterface1, MyInterface2
{
}

// Declare the class name.
$php->setClassName("MyAbstractClass");

// Set class as an abstract class.
$php->setAsAbstract();

abstract class MyAbstractClass
{
}

// Declare class name.
$php->setClassname("MyClass");

$php->setIndention(1);

// Add new raw line.
$php->raw("private $my_property1;");
$php->raw("private $my_property2;");

class MyClass
{
    private $my_property1;
    private $my_property2;
}

$php->lineBreak();

$php->lineBreak(3);
 
$php->addConstant("PI", 3.14);

const PI = 3.14;

$php->addVariable("name", "public", "Juan Dela Cruz");
$php->addVariable("nickname", "private");

public $name = "Juan Dela Cruz";
private $nickname;

// Add public variable.
$php->addPublicVariable("name", "Juan Dela Cruz");

// Add private variable.
$php->addPrivateVariable("age", 30);

// Add protected variable.
$php->addProtectedVariable("bank_id");

public $name = "Juan Dela Cruz";
private $age = 30;
protected $bank_id;

// Add public static variable.
$php->addPublicStaticVariable("name", "Juan Dela Cruz");

// Add private static variable.
$php->addPrivateStaticVariable("age", 30);

// Add protected static variable.
$php->addProtectedStaticVariable("bank_id");

public static $name = "Juan Dela Cruz";
private static $age = 30;
protected static $bank_id;

$php->addLineComment("This is a single line comment.");

// This is a single line comment.

use Graphite\Component\Stencil\Comment;

// Add variable comment.
$php->addComment(Comment::makeVar("string", "The name of the author of stencil."));

// Add new public variable.
$php->addPublicVariable("name", "James Levi Crisostomo");

/**
 * The name of the author of stencil.
 *
 * @var string
 */
public $name = "James Levi Crisostomo";

// Import method class.
use Graphite\Component\Stencil\Method;

// Instantiate a new method object.
$method = new Method("addUser");

// Add the new method in your template.
$php->addMethod($method);

public function addUser()
{
}

// Import method class.
use Graphite\Component\Stencil\Method;

// Instantiate a new method object.
$method = new Method("addUser");

// Set method as static.
$method->setAsStatic();

// Add the new method in your template.
$php->addMethod($method);

public static function addUser()
{
}

// Import method class.
use Graphite\Component\Stencil\Method;

// Instantiate a new method object.
$method = new Method("addUser");

// Add method parameters.
$method->addParam("a");
$method->addParam("b", 1, "int");

// Add the new method in your template.
$php->addMethod($method);

public function addUser($a, int $b = 1)
{
}

// Import method class.
use Graphite\Component\Stencil\Method;

// Instantiate a new method object.
$method = new Method("addUser");

// Set the indention in the beginning of each line.
$method->setIndention(1);

// Set the raw content.
$method->raw("return null;");

// Add new method in your template.
$php->addMethod($method);

public function addUser()
{
    return null;
}

// Import method class.
use Graphite\Component\Stencil\Method;

// Instantiate a new method object.
$method = new Method("addUser");

// Set method as an abstract method.
$method->setAsAbstract();

// Add new method in your template.
$php->addMethod($method);

abstract public function addUser();

// Import comment and method class.
use Graphite\Component\Stencil\Comment;
use Graphite\Component\Stencil\Method;

// Instantiate a new method comment object.
$comment = Comment::makeMethod("This comment is for adding user method.");

// Set the method parameters.
$comment->addIntegerParam("x");
$comment->addIntegerParam("y");

// Set the return data type of the method.
$comment->setReturnType("string");

// Instantiate a new method object.
$method = Method::makePublic("addUser");

// Set the method parameters.
$method->addIntegerParam("x");
$method->addIntegerParam("y");

// Set the indention value.
$method->setIndention(1);

// Set the return value of the method.
$method->raw("return 'Hello World';");

// Add the comment in your template.
$php->addComment($comment);

// Add the new method in your template.
$php->addMethod($method);

/**
 * This comment is for adding user method.
 * 
 * @param  int $x
 * @param  int $y
 * @return string
 */
public function addUser(int $x, int $y)
{
    return 'Hello World';
}

$php->addMethod(Method::makeConstructor()->addParam("a"));

public function __construct($a)
{
}