Skip to content

Commit 49ca0bc

Browse files
committed
Initial commit
0 parents  commit 49ca0bc

File tree

2,318 files changed

+198713
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,318 files changed

+198713
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.idea/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Morébec
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
# Object Generator Component
2+
The Object Generator Component, provides functionality to generate PHP objects from Yaml Definitions.
3+
This component is used by [Orkestra](https:/github.com/Morebec/Orkestra).
4+
5+
## Rationale
6+
Because of its syntax, defining classes in PHP requires a lot of typing:
7+
from namespaces, use declarations, type hints, implements and extends, properties, methods,
8+
constructors, getters and setters, to phpDocBlocks, there's a lot to do.
9+
After hundreds of classes, your fingers and your motivation can get pretty tired.
10+
11+
We figured, wouldn't it be great to be able to define classes with less verbosity?
12+
Why not use some sort of DSL that could be translated to working PHP classes?
13+
Hence, the Object Generator Component.
14+
15+
16+
## Installation
17+
```json
18+
{
19+
"repositories": [
20+
{
21+
"url": "https://github.com/Morebec/ObjectGenerator.git",
22+
"type": "git"
23+
}
24+
],
25+
"require": {
26+
"morebec/object-generator": "^1.0"
27+
}
28+
}
29+
```
30+
31+
## Usage
32+
### 1. Create a Yaml Object Definition File
33+
```yaml
34+
# Person.yaml
35+
Person:
36+
namespace: My\Name\Space
37+
extends: Human
38+
desc: This Class represents a Person
39+
implements: MortalInterface
40+
type: class
41+
42+
props:
43+
fullname:
44+
desc: Fullname of the person
45+
type: string
46+
accessor: true
47+
48+
age:
49+
desc: Age of the person
50+
type: int
51+
accessor: true
52+
53+
methods:
54+
__construct:
55+
desc: Constructs a Person instance
56+
params:
57+
fullname:
58+
desc: Fullname of the person
59+
type: string
60+
init: true
61+
age:
62+
desc: Age of the person
63+
type: int
64+
init: true
65+
default: 10
66+
code: |
67+
Assertion::notBlank($fullname, 'A person must have a name!');
68+
Assertion::min($age, 0, 'A person must have a positive age!');
69+
```
70+
71+
### 2. Compile the Object File to PHP
72+
73+
```bash
74+
bin/console compile:file person.yaml
75+
```
76+
77+
### 3. Review your newly created PHP file
78+
```php
79+
<?php
80+
81+
namespace My\Name\Space;
82+
83+
/**
84+
* This Class represents a Person
85+
*/
86+
class Person extends Human implements MortalInterface
87+
{
88+
/** @var string Fullname of the person */
89+
private $fullname;
90+
91+
/** @var int Age of the person */
92+
private $age;
93+
94+
95+
/**
96+
* Constructs a Person instance
97+
* @param string $fullname Fullname of the person
98+
* @param int $age Age of the person
99+
*/
100+
public function __construct(string $fullname, int $age = 10)
101+
{
102+
$this->age = $age;
103+
$this->fullname = $fullname;
104+
Assertion::notBlank($fullname, 'A person must have a name!');
105+
Assertion::min($age, 0, 'A person must have a positive age!');
106+
}
107+
108+
109+
/**
110+
* Returns the value of fullname
111+
* @return string Value of fullname
112+
*/
113+
public function getFullname(): string
114+
{
115+
return $this->fullname;
116+
}
117+
118+
119+
/**
120+
* Returns the value of age
121+
* @return int Value of age
122+
*/
123+
public function getAge(): int
124+
{
125+
return $this->age;
126+
}
127+
}
128+
```
129+
130+
131+
### Library Usage
132+
The Object Generator Component can also be used in projects as a library.
133+
For more information, please read the documentation.
134+
135+
[Documentation](https://github.com/Morebec/ObjectGenerator/tree/docs/user/index.md)
136+
137+
138+
## Full object reference
139+
```yaml
140+
ClassName:
141+
namespace: null
142+
final: false
143+
abstract: false
144+
type: ~ # One of "class"; "interface"; "trait", Required
145+
desc: ''
146+
extends: null
147+
implements: []
148+
traits: []
149+
annotations: []
150+
use: []
151+
152+
props:
153+
154+
# Prototype
155+
-
156+
type: null
157+
value: __undefined__
158+
desc: ''
159+
annotations: []
160+
static: false
161+
constant: false
162+
visibility: private # One of "public"; "protected"; "private"
163+
164+
# You can configure how the getter of this property will be generated here.
165+
# If you prefer not generating a getter, simply ommit this parameter.
166+
accessor:
167+
168+
# You can specify the name of your setter, here.
169+
# If nothing is specified as a name, it will default to getPropertyName.
170+
name: null
171+
desc: ''
172+
visibility: public # One of "public"; "protected"; "private"
173+
174+
# You can specify some PHP code to be injected here.
175+
code: null
176+
annotations: []
177+
abstract: false
178+
static: false
179+
final: false
180+
181+
# You can configure how the setter of this property will be generated here.
182+
# If you prefer not generating a setter, simply ommit this parameter.
183+
mutator:
184+
185+
# You can specify the name of your setter, here.
186+
# If nothing is specified as a name, it will default to setPropertyName.
187+
name: null
188+
desc: ''
189+
visibility: public # One of "public"; "protected"; "private"
190+
191+
# You can specify some PHP code to be injected here.
192+
code: null
193+
annotations: []
194+
abstract: false
195+
static: false
196+
final: false
197+
methods:
198+
199+
# Prototype
200+
-
201+
desc: ''
202+
visibility: public # One of "public"; "protected"; "private"
203+
abstract: false
204+
final: false
205+
static: false
206+
annotations: []
207+
208+
# You can specify some PHP code to be injected here.
209+
code: null
210+
params:
211+
212+
# Prototype
213+
name:
214+
type: __undefined__
215+
default: __undefined__
216+
desc: ''
217+
annotations: []
218+
219+
# This option is mostly useful for __construct.
220+
# It will initialize the parameter to a property of the same name. (E.g: $this->property = $parameter)
221+
init: false
222+
223+
# Return type definition goes here. If you don't want to provide a return type, simply ommit this parameter
224+
return:
225+
type: __undefined__
226+
desc: ''
227+
```

bin/console

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require __DIR__ . '/../vendor/autoload.php';
5+
6+
use Morebec\ObjectGenerator\Application\Console\ConsoleCommand\CompileFileCommand;
7+
use Morebec\ObjectGenerator\Application\Console\ConsoleCommand\DumpObjectReferenceCommand;
8+
use Morebec\ObjectGenerator\Application\Shared\Service\ApplicationService;
9+
use Symfony\Component\Console\Application;
10+
11+
$appService = new ApplicationService();
12+
13+
$application = new Application();
14+
15+
// Register commands
16+
$application->add(new CompileFileCommand($appService));
17+
$application->add(new DumpObjectReferenceCommand($appService));
18+
19+
$application->run();
20+

composer.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "morebec/object-generator",
3+
"description": "The Object Generator component allows one to generate PHP Objects from YAML definitions.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "jwillp",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"repositories": [
13+
{
14+
"url": "https://github.com/Morebec/ValueObjects.git",
15+
"type": "git"
16+
}
17+
],
18+
"config": {
19+
"preferred-install": {
20+
"*": "dist"
21+
},
22+
"sort-packages": true
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Morebec\\ObjectGenerator\\": "src/"
27+
}
28+
},
29+
"require": {
30+
"morebec/value-objects": "^1.0",
31+
"nette/php-generator": "^3.3",
32+
"symfony/config": "^5.0",
33+
"symfony/console": "^5.0",
34+
"symfony/filesystem": "^5.0",
35+
"symfony/property-access": "^5.0",
36+
"symfony/yaml": "^5.0"
37+
},
38+
"require-dev": {
39+
"phpunit/phpunit": "^8"
40+
},
41+
42+
"scripts": {
43+
"fix-namespaces": "find src/ -iname '*.php' -exec phpactor class:transform {} --transform=fix_namespace_class_name \\; | grep .php"
44+
}
45+
}

0 commit comments

Comments
 (0)