是一个简单的PHP类模板库。

v1.0.2 2021-05-02 07:47 UTC

This package is not auto-updated.

Last update: 2024-09-30 00:02:39 UTC


README


是一个简单的PHP类模板库。

入门

  1. 您可以通过composer进行安装。
composer require jameslevi/stencil
  1. 如果您没有使用任何框架,请将以下代码粘贴到项目上端以加载composer自动加载机制。
<?php

if(file_exists(__DIR__.'/vendor/autoload.php'))
{
    require __DIR__.'/vendor/autoload.php';
}
  1. 基本实现。
<?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__ . "/");

上面的代码将生成如下内容的PHP文件。

<?php

namespace App\Controller;

class UserController extends Controller
{
}

导入类实现

您可以使用 "use" 方法将类导入到模板中。

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

您也可以为导入的类设置别名以避免命名冲突。

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

此命令将生成如下代码。

use Carbon\Carbon as MyDateTime;

扩展类

您可以使用 "extends" 方法扩展类。只需确保您已导入父类。

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

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

实现接口

您可以使用 "implement" 方法实现一个或多个接口类。

// 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
{
}

原始内容

您可以使用 "raw" 方法在每行添加内容。您可以使用 "setIndention" 方法设置每行开头的制表符空格。

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

换行符

您可以使用 "lineBreak" 方法添加单个换行符。

$php->lineBreak();

您也可以通过提供第一个参数来添加多个换行符。

$php->lineBreak(3);

常量

您可以使用 "addConstant" 方法在类中声明常量值。

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

单行注释

您可以使用 "addLineComment" 方法添加单行注释。

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

上面的示例将生成如下代码。

// This is a single line comment.

变量的块注释

您也可以使用 "addComment" 方法添加多行注释。

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

方法函数

您可以使用 "addMethod" 方法为类添加方法。

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

静态方法

您也可以使用 "setAsStatic" 方法设置方法是否为静态。

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

方法参数

您可以使用 "addParam" 方法在方法中添加多个参数。

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

方法内容

您可以使用 "raw" 方法向方法中添加内容。

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

抽象方法

您也可以使用 "setAsAbstract" 方法在抽象类中添加抽象方法。

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

方法的块注释

您可以使用 "addComment" 方法添加块注释。

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

构造函数方法

您也可以使用 "makeConstructor" 静态方法为类添加构造函数。

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

上面的示例将生成如下代码。

public function __construct($a)
{
}

贡献

对于问题、担忧和建议,您可以通过 nerdlabenterprise@gmail.com 邮件给詹姆斯·克里索斯托莫。

许可证

本软件包是开源软件,许可协议为 MIT 许可。