承接 jameslevi/stencil 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jameslevi/stencil

Composer 安装命令:

composer require jameslevi/stencil

包简介

Is a simple PHP Class templating library.

关键字:

README 文档

README


Is a simple PHP Class templating library.

Getting Started

  1. You can install via composer.
composer require jameslevi/stencil
  1. If not using any framework, paste the following code at the upper part of your project to load the composer autoload mechanism.
<?php

if(file_exists(__DIR__.'/vendor/autoload.php'))
{
    require __DIR__.'/vendor/autoload.php';
}
  1. Basic implementations.
<?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__ . "/");

The code above will generate PHP file with content like this.

<?php

namespace App\Controller;

class UserController extends Controller
{
}

Import Class Implementation

You can use "use" method to import classes in to your template.

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

You can also set alias for your imported class to avoid naming conflict.

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

This command will generate code like this.

use Carbon\Carbon as MyDateTime;

Extend Classes

You can extend class using "extends" method. Just make sure you imported your parent class.

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

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

Implement Interfaces

You can implement one or more interface classes using "implement" method.

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

The example above will generate code like this.

use MyInterface1;
use MyInterface2;

class MyClass implements MyInterface1, MyInterface2
{
}

Abstract Class

You can set your class as an abstract class.

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

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

The example above will generate code like this.

abstract class MyAbstractClass
{
}

Raw Content

You can add content in each line using "raw" method. You can use "setIndention" method to set tab spaces in the beginning of each line.

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

$php->setIndention(1);

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

The example above will generate code like this.

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

Line Breaks

You can add single line break using "lineBreak" method.

$php->lineBreak();

You can also make multiple line breaks by providing the first argument.

$php->lineBreak(3);

Constants

You can declare constant values in your class using "addConstant" method.

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

The example above will generate code like this.

const PI = 3.14;

Variables

You can declare class variables with public, private and protected visibility.

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

The above example will generate code like this.

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

Non-Static Variables

Variables that are only accessible when class is instantiated.

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

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

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

The above example will generate code like this.

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

Static Variables

Variables that are accessible even without instantiating a class.

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

The above example will generate code like this.

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

Single Line Comment

You can add single line comment using "addLineComment" method.

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

The above example will generate code like this.

// This is a single line comment.

Block Comment for Variables

You also add multi-line comments using "addComment" method.

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 above example will generate code like this.

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

Method Functions

You can add methods for your class using "addMethod" method.

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

The above example will generate code like this.

public function addUser()
{
}

Static Methods

You can also set if method is static using "setAsStatic" method.

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

The above example will generate code like this.

public static function addUser()
{
}

Method Arguments

You can add multiple arguments in your method using "addParam" method.

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

The above example will generate code like this.

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

Method Content

You can add content in to your method using "raw" method.

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

The above example will generate code like this.

public function addUser()
{
    return null;
}

Abstract Method

You can also add abstract method in to your abstract class using "setAsAbstract" method.

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

The above example will generate code like this.

abstract public function addUser();

Block Comments for Methods

You can add block comments using "addComment" method.

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

The above example will generate code like this.

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

Constructor Method

You can also add constructor to your class using "makeConstructor" static method.

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

The above example will generate code like this.

public function __construct($a)
{
}

Contribution

For issues, concerns and suggestions, you can email James Crisostomo via nerdlabenterprise@gmail.com.

License

This package is an open-sourced software licensed under MIT License.

jameslevi/stencil 适用场景与选型建议

jameslevi/stencil 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php class」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jameslevi/stencil 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jameslevi/stencil 我们能提供哪些服务?
定制开发 / 二次开发

基于 jameslevi/stencil 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 31
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-12