Skip to content

Commit c1f0cb6

Browse files
committed
Update README.md
1 parent 2a92ecf commit c1f0cb6

File tree

1 file changed

+119
-2
lines changed

1 file changed

+119
-2
lines changed

README.md

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,119 @@
1-
# code-generator
2-
Code generator
1+
# Code generator
2+
3+
Code generator is a PHP tool that provides an interface for generating code. Currently only PHP class generation is supported.
4+
5+
## Installation
6+
Require the package using composer `composer require krlove/code-generator --dev`. Code generator is usually intended to be installed only in dev environment. Installation in prod environment is not recommended.
7+
8+
## Usage example
9+
```php
10+
<?php
11+
12+
use Krlove\CodeGenerator\Model\ArgumentModel;
13+
use Krlove\CodeGenerator\Model\ClassModel;
14+
use Krlove\CodeGenerator\Model\ConstantModel;
15+
use Krlove\CodeGenerator\Model\ClassNameModel;
16+
use Krlove\CodeGenerator\Model\DocBlockModel;
17+
use Krlove\CodeGenerator\Model\MethodModel;
18+
use Krlove\CodeGenerator\Model\NamespaceModel;
19+
use Krlove\CodeGenerator\Model\PropertyModel;
20+
use Krlove\CodeGenerator\Model\UseTraitModel;
21+
use Krlove\CodeGenerator\Model\UseClassModel;
22+
use Krlove\CodeGenerator\Model\VirtualMethodModel;
23+
use Krlove\CodeGenerator\Model\VirtualPropertyModel;
24+
25+
require 'vendor/autoload.php';
26+
27+
$phpClass = new ClassModel();
28+
$phpClass->setNamespace(new NamespaceModel('NamespaceOfTheClass'));
29+
30+
$name = new ClassNameModel('TestClass', 'BaseTestClass');
31+
$name->addImplements('\\NamespaceOne\\InterfaceOne');
32+
$phpClass->addUses(new UseClassModel('NamespaceTwo'));
33+
$name->addImplements('InterfaceTwo');
34+
35+
$phpClass->setName($name);
36+
37+
$phpClass->addTrait(new UseTraitModel('TraitOne'));
38+
$phpClass->addTrait(new UseTraitModel('TraitTwo'));
39+
40+
$phpClass->addConstant(new ConstantModel('CONST_ONE', 'value'));
41+
$phpClass->addConstant(new ConstantModel('CONST_TWO', 1));
42+
43+
$phpClass->addProperty(new PropertyModel('propertyOne'));
44+
$phpClass->addProperty(new PropertyModel('propertyTwo', 'protected'));
45+
$privateProperty = new PropertyModel('propertyThree', 'private', 'defaultValue');
46+
$privateProperty->setDocBlock(new DocBlockModel('@var string'));
47+
$phpClass->addProperty($privateProperty);
48+
49+
$phpClass->addProperty(new VirtualPropertyModel('virtualPropertyOne', 'int'));
50+
$phpClass->addProperty(new VirtualPropertyModel('virtualPropertyTwo', 'mixed'));
51+
52+
$phpClass->addMethod(new MethodModel('methodOne'));
53+
$phpClass->addMethod(new MethodModel('methodTwo', 'protected'));
54+
$privateMethod = new MethodModel('methodThree', 'private');
55+
$privateMethod->addArgument(new ArgumentModel('arg1'));
56+
$privateMethod->addArgument(new ArgumentModel('arg2', 'array', 'array()'));
57+
$privateMethod->setBody('return \'result\';');
58+
$privateMethod->setDocBlock(new DocBlockModel('@var mixed arg1', '@var array arg2', '@return string'));
59+
$phpClass->addMethod($privateMethod);
60+
61+
$phpClass->addMethod(new VirtualMethodModel('virtualMethodOne'));
62+
$virtualMethodTwo = new VirtualMethodModel('virtualMethodTwo', 'array');
63+
$virtualMethodTwo->addArgument(new ArgumentModel('arg1', 'array'));
64+
$phpClass->addMethod($virtualMethodTwo);
65+
66+
echo $phpClass->render();
67+
```
68+
69+
Output
70+
71+
```php
72+
<?php
73+
74+
namespace NamespaceOfTheClass;
75+
76+
use NamespaceTwo;
77+
78+
/**
79+
* @property int $virtualPropertyOne
80+
* @property mixed $virtualPropertyTwo
81+
* @method void virtualMethodOne()
82+
* @method array virtualMethodTwo(array $arg1)
83+
*/
84+
class TestClass extends BaseTestClass implements \NamespaceOne\InterfaceOne, InterfaceTwo
85+
{
86+
use TraitOne;
87+
use TraitTwo;
88+
89+
const CONST_ONE = 'value';
90+
const CONST_TWO = ;
91+
92+
public $propertyOne;
93+
94+
protected $propertyTwo;
95+
96+
/**
97+
* @var string
98+
*/
99+
private $propertyThree = 'defaultValue';
100+
101+
public function methodOne()
102+
{
103+
}
104+
105+
protected function methodTwo()
106+
{
107+
}
108+
109+
/**
110+
* @var mixed arg1
111+
* @var array arg2
112+
* @return string
113+
*/
114+
private function methodThree($arg1, array $arg2)
115+
{
116+
return 'result';
117+
}
118+
}
119+
```

0 commit comments

Comments
 (0)