PHP Class Generator
Generate PSR compliant classes from plain text document, (*1)
Requirements
Installation
Download phar
Download php-cg.phar and store anywhere., (*2)
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
, (*3)
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, (*4)
$ php php-pcg.phar --from ./doc --to ./src
PHP file will be generated to ./src/Full/Qualified/ClassName.php
, (*5)
``` php
<?php, (*6)
namespace KzykHys\ClassGenerator;, (*7)
/**
* The sample of PHP Class Generator
* Generates PHP classes from plain text document (*.pcg)
*/
class Sample extends Container implements \IteratorAggrigate, \Countable {, (*8)
/**
* @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()
{
}
}, (*9)
The Syntax
----------
### Overview
* Class Definition (required)
* Field Definition (optionai)
* Method Definition (optional)
### Class Definition (required)
%ClassName% < %BaseClassName% << %InterfaceName% %InterfaceName% > %Comment%, (*10)
%Comment%, (*11)
* ``%ClassName%`` is required
** If your class is in the namespace, ``%ClassName%`` will be like this ``\\Namespace\\Package\\ClassName``
* ``<`` represents ``extends``
* ``%BaseClassName%`` is optional
* ``<<`` represents ``implements``
* ``%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%, (*12)
%Comment%, (*13)
* ``%FieldVisibility%`` is required
* ``%FieldVisibility%`` takes ``+`` as public ``#`` as protected ``-`` as private
* ``%FieldName%`` is required
* ``%FieldType%`` is optional
* ``%FieldAccessor%`` takes ``set``, ``bind`` as setter, ``get``, ``is`` as 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%, (*14)
* ``%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, (*15)
The sample of PHP Class Generator
Generates PHP classes from plain text document (*.pcg), (*16)
- 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], (*17)
- __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, (*18)
If fails like this
[UnexpectedValueException]
creating archive "php-cg.phar" disabled by the php.ini setting phar.readonly, (*19)
Run following
php -d phar.readonly=0 php-cg build
```, (*20)
Notes
- Class constant is not supported yet.
- Implementation of interface is not generated automatically.
Author
Kazuyuki Hayashi (@kzykhys), (*21)