定制 gm314/nitria 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

gm314/nitria

Composer 安装命令:

composer require gm314/nitria

包简介

PHP 7 Code Generator

README 文档

README

Build Status License Latest Stable Version Total Downloads Latest Unstable Version

Class generator for PHP7 code. It will take care of indentation, use statements, php doc creation.

Installation

{
    "require": {
        "gm314/nitria": "*"
    }
}

Usage

Create a class and set extends and implements

$classGenerator = new ClassGenerator("Generated\\MyClass", true);

// add extends statement, a use statement will be added automatically 
$classGenerator->setExtends("BaseClass\\ClassName");

// add implement statement
$classGenerator->addImplements("\\Serializable");

// add a constant
$classGenerator->addConstant("CONSTANT_STRING", '"hello"');

Properties

Properties have a name, a type, a modifier and an optional default value. The example will generate a static private property with the name myName and the default value 19.08.

 $classGenerator->addProperty("myName","float","private", false, '19.08', 'doc bloc comment');

This will result in the following code

    /**
     * @var float doc bloc comment
     */
    private $myName = 19.08;

Or you can use the short version for non static properties.

// add property (for classes use statement will be added - unless the class is in the same namespace)
$classGenerator->addPrivateProperty("iAmPrivat", 'MyPackage\MyClass');
$classGenerator->addProtectedProperty("iAmProtected", "array", []);
$classGenerator->addPublicProperty("iAmPublic", "float");

And for the static properties.

// add static property
$classGenerator->addPrivateStaticProperty("iAmPrivatStatic", "int");
$classGenerator->addProtectedStaticProperty("iAmProtectedStatic", "bool");
$classGenerator->addPublicStaticProperty("iAmPublicStatic", "array");

Methods

$method = $classGenerator->addPublicMethod("myFunction");
$method->addParameter("string", "parameterName", '"defaultValue!"');
$method->addParameter("\\DateTime", "datetime");

// the method will have a return type string that is not nullable
$method->setReturnType("string", false);
$method->addCodeLine('return $parameterName;');

the above code will generate the following method

/**
 * @param string $parameterName
 * @param \DateTime $datetime
 * @return string
 */
public function myFunction(string $parameterName = "defaultValue!", \DateTime $datetime) : string {
    return $parameterName;   
}
// method generation
$classGenerator->addPrivateMethod("iAmPrivate");
$classGenerator->addProtectedMethod("iAmProtected");
$classGenerator->addPublicMethod("iAmPublic");

// static method generation
$classGenerator->addPrivateStaticMethod("iAmPrivateStatic");
$classGenerator->addProtectedStaticMethod("iAmProtectedStatic");
$classGenerator->addPublicStaticMethod("iAmPublicStatic");

Method Content generation

Code

$method = $classGenerator->addPublicMethod("sayIf");
$method->addParameter("int", "intParam");
$method->setReturnType("int", false);

// add a simple line of code
$method->addCodeLine('return $intParam * $intParam;');

If Statement

$method = $classGenerator->addPublicMethod("sayIf");
$method->addParameter("int", "int");
$method->setReturnType("int", false);

// start if statement >> if ($int ===1) {
$method->addIfStart('$int === 1');
$method->addCodeLine('return 1;');

// add if else statement >> } else if ($int === 2) {
$method->addIfElseIf('$int === 2');
$method->addCodeLine('return 2;');

// add else statement >> } else {
$method->addIfElse();
$method->addCodeLine('return 3;');

// close if statement >> }
$method->addIfEnd();

While Statement

$method = $classGenerator->addPublicMethod("sayWhile");
$method->addParameter("int", "int");
$method->setReturnType("string", false);

$method->addCodeLine('$string = "";');

// start while statement >> while($int++ < 10) {
$method->addWhileStart('$int++ < 10');
$method->addCodeLine('$string .= "x";');

// end while statement >> }
$method->addWhileEnd();
$method->addCodeLine('return $string;');

Foreach Statement

$method = $classGenerator->addPublicMethod("sayForeach");
$method->addParameter("array", "list");
$method->setReturnType("string", false);

$method->addCodeLine('$string = "";');

// start foreach >> foreach($list as $item) {
$method->addForeachStart('$list as $item');
$method->addCodeLine('$string .= $item;');

// end foreach >> }
$method->addForeachEnd();
$method->addCodeLine('return $string;');

Switch Statement

$method = $classGenerator->addPublicMethod("saySwitch");
$method->addParameter("string", "value");
$method->setReturnType("string", false);

// start switch statement >> switch($value) {
$method->addSwitch('$value');

// case statement >> case "a":
$method->addSwitchCase('"a"');
$method->addCodeLine('return "a";');

// case break >> break;
$method->addSwitchBreak();

// default >> default:
$method->addSwitchDefault();
$method->addCodeLine('return "c";');
$method->addSwitchBreak();

$method->addSwitchEnd();

Tests / More examples

See under tests/End2End

  • GeneratorTest
  • CodeTest

License

MIT

gm314/nitria 适用场景与选型建议

gm314/nitria 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.27k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-28