gm314 / nitria
PHP 7 代码生成器
0.2.2
2022-07-07 17:52 UTC
Requires
- php: >=7.1.0
- gm314/common: *
Requires (Dev)
README
PHP7代码的类生成器。它将负责缩进、使用语句、PHP文档创建。
安装
{ "require": { "gm314/nitria": "*" } }
用法
创建一个类并设置继承和实现
$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"');
属性
属性具有名称、类型、修饰符和可选的默认值。示例将生成一个名为 myName
、默认值为 19.08
的 static
private
属性。
$classGenerator->addProperty("myName","float","private", false, '19.08', 'doc bloc comment');
这将生成以下代码
/** * @var float doc bloc comment */ private $myName = 19.08;
或者您可以使用简写版本来表示非静态属性。
// 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");
对于静态属性。
// add static property $classGenerator->addPrivateStaticProperty("iAmPrivatStatic", "int"); $classGenerator->addProtectedStaticProperty("iAmProtectedStatic", "bool"); $classGenerator->addPublicStaticProperty("iAmPublicStatic", "array");
方法
$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;');
上述代码将生成以下方法
/** * @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 = $classGenerator->addPublicMethod("sayIf"); $method->addParameter("int", "intParam"); $method->setReturnType("int", false); // add a simple line of code $method->addCodeLine('return $intParam * $intParam;');
如果语句
$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语句
$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语句
$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语句
$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/End2End下查看
- GeneratorTest
- CodeTest
许可证
MIT