kzykhys/php-class-generator
Composer 安装命令:
composer require kzykhys/php-class-generator
包简介
Generate PSR compliant classes from plain text document
README 文档
README
Generate PSR compliant classes from plain text document
Requirements
- PHP 5.3.3 +
Installation
Download phar
Download php-cg.phar and store anywhere.
via Composer
{
"require": {
"kzykhys/php-class-generator": "dev-master"
}
}
Usage
Write your class and save the text file to *.pcg (Syntax is described below)
For example ./doc/myclass.pcg
KzykHys\ClassGenerator\Sample < KzykHys\ClassGenerator\Container << \IteratorAggrigate \Countable
> The sample of PHP Class Generator
> Generates PHP classes from plain text document (*.pcg)
+ iterator:\ArrayIterator
# container:array
# length:integer[get set] > The length of code
# compiled:boolean[is set] > Whether this class is compiled or not
- options:array
- generator:Generator[get set]
+ __construct(options:array)
+ generate(document:string version:string):\KzykHys\ClassGenerator\Compiler\StreamWriter
+ getString():string > Returns the code as a string
+ write(filename:string) > Write the code to file
+ count():integer
# traverseContainer():Container
Run the command
$ php php-pcg.phar --from ./doc --to ./src
PHP file will be generated to ./src/Full/Qualified/ClassName.php
<?php namespace KzykHys\ClassGenerator; /** * The sample of PHP Class Generator * Generates PHP classes from plain text document (*.pcg) */ class Sample extends Container implements \IteratorAggrigate, \Countable { /** * @var \ArrayIterator $iterator */ public $iterator; /** * @var array $container */ protected $container; /** * The length of code * * @var integer $length */ protected $length; /** * Whether this class is compiled or not * * @var boolean $compiled */ protected $compiled; /** * @var array $options */ private $options; /** * @var Generator $generator */ private $generator; /** * @param array $options */ public function __construct(array $options) { } /** * @param string $document * @param string $version * * @return \KzykHys\ClassGenerator\Compiler\StreamWriter */ public function generate($document, $version) { } /** * Returns the code as a string * * * @return string */ public function getString() { } /** * Write the code to file * * @param string $filename */ public function write($filename) { } /** * * @return integer */ public function count() { } /** * * @return Container */ protected function traverseContainer() { } }
The Syntax
Overview
- Class Definition (required)
- Field Definition (optionai)
- Method Definition (optional)
Class Definition (required)
%ClassName% < %BaseClassName% << %InterfaceName% %InterfaceName% > %Comment%
> %Comment%
%ClassName%is required ** If your class is in the namespace,%ClassName%will be like this\\Namespace\\Package\\ClassName<representsextends%BaseClassName%is optional<<representsimplements%InterfaceName%is optional>starts comment to end of the line- You can place comments to same line and next line of class definition
Field Definition (optional)
%FieldVisibility% %FieldName% : %FieldType% \[%FieldAccessor%\] > %Comment%
> %Comment%
%FieldVisibility%is required%FieldVisibility%takes+as public#as protected-as private%FieldName%is required%FieldType%is optional%FieldAccessor%takesset,bindas setter,get,isas getter>starts comment to end of the line- You can place comments to same line and next line of field definition
Method Definition (optionai)
%MethodVisibility% %MethodName% (%Argument% : %ArgumentType%) : %ReturnType%
%MethodVisibility%is required%MethodVisibility%takes+as public#as protected-as private%MethodName%is required- Braces
()are required (even if there is no argument) %Argument%and%ArgumentType%is optional%ReturnType%is optional
Write a Readable Document
You can use any white spaces to write a readable document
Following code is same as the code used in Usage
KzykHys\ClassGenerator\Sample < KzykHys\ClassGenerator\Container << \IteratorAggrigate \Countable
> The sample of PHP Class Generator
> Generates PHP classes from plain text document (*.pcg)
+ iterator : \ArrayIterator
# container : array
# length : integer[get set]
> The length of code
# compiled : boolean[is set]
> Whether this class is compiled or not
- options : array
- generator : Generator[get set]
+ __construct(options:array)
+ generate(document:string
version:string) : \KzykHys\ClassGenerator\Compiler\StreamWriter
+ getString() : string
> Returns the code as a string
+ write(filename:string)
> Write the code to file
+ count() : integer
# traverseContainer() : Container
Build Phar Archive
You can build phar archive from source code by following command
php-cg build
If fails like this
[UnexpectedValueException]
creating archive "php-cg.phar" disabled by the php.ini setting phar.readonly
Run following
php -d phar.readonly=0 php-cg build
Notes
- Class constant is not supported yet.
- Implementation of interface is not generated automatically.
Author
Kazuyuki Hayashi (@kzykhys)
kzykhys/php-class-generator 适用场景与选型建议
kzykhys/php-class-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 101 次下载、GitHub Stars 达 11, 最近一次更新时间为 2012 年 11 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kzykhys/php-class-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kzykhys/php-class-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 101
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 27
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-11-20