jonathanguo / code-generator
代码生成器
v1.0.4
2019-09-27 09:48 UTC
This package is auto-updated.
Last update: 2024-09-08 03:58:24 UTC
README
此包是无耻地复制了 krlove/code-generator。它完成了任务,但看起来作者已经不再积极维护此包。当我于2018年12月20日克隆仓库时,有一个问题,自2017年以来没有回应的两个PR。
另一个原因是,我需要代码生成器生成一些具有自定义样式的代码。这些要求可能不是通用的。所以我决定复制仓库而不是分叉它。
代码生成器
代码生成器是一个PHP工具,它提供了一个生成代码的接口。目前只支持PHP类生成。
安装
使用composer composer require jonathanguo/code-generator --dev
安装此包。代码生成器通常仅用于开发环境。不建议在生产环境中安装。
使用示例
<?php use JonathanGuo\CodeGenerator\Model\ArgumentModel; use JonathanGuo\CodeGenerator\Model\ClassModel; use JonathanGuo\CodeGenerator\Model\ConstantModel; use JonathanGuo\CodeGenerator\Model\ClassNameModel; use JonathanGuo\CodeGenerator\Model\DocBlockModel; use JonathanGuo\CodeGenerator\Model\MethodModel; use JonathanGuo\CodeGenerator\Model\NamespaceModel; use JonathanGuo\CodeGenerator\Model\PropertyModel; use JonathanGuo\CodeGenerator\Model\UseTraitModel; use JonathanGuo\CodeGenerator\Model\UseClassModel; use JonathanGuo\CodeGenerator\Model\VirtualMethodModel; use JonathanGuo\CodeGenerator\Model\VirtualPropertyModel; require 'vendor/autoload.php'; $phpClass = new ClassModel(); $phpClass->setNamespace(new NamespaceModel('NamespaceOfTheClass')); $name = new ClassNameModel('TestClass', 'BaseTestClass'); $name->addImplements('\\NamespaceOne\\InterfaceOne'); $phpClass->addUses(new UseClassModel('NamespaceTwo')); $name->addImplements('InterfaceTwo'); $phpClass->setName($name); $phpClass->addTrait(new UseTraitModel('TraitOne')); $phpClass->addTrait(new UseTraitModel('TraitTwo')); $phpClass->addConstant(new ConstantModel('CONST_ONE', 'value')); $phpClass->addConstant(new ConstantModel('CONST_TWO', 1)); $phpClass->addProperty(new PropertyModel('propertyOne')); $phpClass->addProperty(new PropertyModel('propertyTwo', 'protected')); $privateProperty = new PropertyModel('propertyThree', 'private', 'defaultValue'); $privateProperty->setDocBlock(new DocBlockModel('@var string')); $phpClass->addProperty($privateProperty); $phpClass->addProperty(new VirtualPropertyModel('virtualPropertyOne', 'int')); $phpClass->addProperty(new VirtualPropertyModel('virtualPropertyTwo', 'mixed')); $phpClass->addMethod(new MethodModel('methodOne')); $phpClass->addMethod(new MethodModel('methodTwo', 'protected')); $privateMethod = new MethodModel('methodThree', 'private'); $privateMethod->addArgument(new ArgumentModel('arg1')); $privateMethod->addArgument(new ArgumentModel('arg2', 'array', 'array()')); $privateMethod->setBody('return \'result\';'); $privateMethod->setDocBlock(new DocBlockModel('@var mixed arg1', '@var array arg2', '@return string')); $phpClass->addMethod($privateMethod); $phpClass->addMethod(new VirtualMethodModel('virtualMethodOne')); $virtualMethodTwo = new VirtualMethodModel('virtualMethodTwo', 'array'); $virtualMethodTwo->addArgument(new ArgumentModel('arg1', 'array')); $phpClass->addMethod($virtualMethodTwo); echo $phpClass->render();
输出
<?php namespace NamespaceOfTheClass; use NamespaceTwo; /** * @property int $virtualPropertyOne * @property mixed $virtualPropertyTwo * @method void virtualMethodOne() * @method array virtualMethodTwo(array $arg1) */ class TestClass extends BaseTestClass implements \NamespaceOne\InterfaceOne, InterfaceTwo { use TraitOne; use TraitTwo; const CONST_ONE = 'value'; const CONST_TWO = ; public $propertyOne; protected $propertyTwo; /** * @var string */ private $propertyThree = 'defaultValue'; public function methodOne() { } protected function methodTwo() { } /** * @var mixed arg1 * @var array arg2 * @return string */ private function methodThree($arg1, array $arg2) { return 'result'; } }
如果您不想生成类/属性的DocBlock,可以通过以下方式关闭:
$phpClass->setGenerateClassDocBlock(false);