Skip to content

Commit 0adb2ce

Browse files
committed
Updated docs
1 parent 73a4ac0 commit 0adb2ce

9 files changed

+157
-29
lines changed

docs/best-practices.rst

+21-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,27 @@ It is useful to use some kind of template system to load the contents for your b
1111
Hack in Traits
1212
--------------
1313

14-
Let's assume you generate a php class. This class will be used in your desired framework as it serves a specific purpose in there. It possible needs to fulfill an interface or some abstract methods and your generated code will also take care of this - wonderful. Now imagine the programmer wants to change the code your code generation tools created. Once you run the tools again his changes probably got overwritten, which would be bad.
15-
Here is the trick: First we declare the generated class as "host" class. Your code generation tools should first check if the host class exists and if not create it, or if it exists, load the existing class. For the possible required methods your host class must contain create a trait and implement the required logic at the trait. Check in the host class, if the trait is available and add it if not present. Now you only need to write the host class if there is an update (or if your code generation tools have some kind of force parameter). It also keeps programmer modified code intact and your tools can safely overwrite the trait. If you want to give the programmer more freedom offer him hook methods in the host class, that - if he wants to - can overwrite with his own logic.
14+
Let's assume you generate a php class. This class will be used in your desired framework as it serves a specific purpose in there. It possible needs to fulfill an interface or some abstract methods and your generated code will also take care of this - wonderful. Now imagine the programmer wants to change the code your code generation tools created. Once you run the code generation tools again his changes probably got overwritten, which would be bad.
15+
16+
Here is the trick: First we declare the generated class as "host" class:
17+
18+
.. image:: images/hack-in-trait.png
19+
:width: 50%
20+
:align: center
21+
22+
Your generated code will target the trait, where you can savely overwrite code. However, you must make sure the trait will be used from the host class and also generate the host class, if it doesn't exist. So here are the steps following this paradigm:
23+
24+
25+
1. Create the trait
26+
2. Check if the host class exists
27+
28+
a. if it exists, load it
29+
b. if not, create it
30+
31+
3. Add the trait to the host class
32+
4. Generate the host class code
33+
34+
That way, the host class will be user-land code and the developer can write his own code there. The code generation tools will keep that code intact, so it won't be destroyed when code generation tools run again. If you want to give the programmer more freedom offer him hook methods in the host class, that - if he wants to - can overwrite with his own logic.
1635

1736
Format in Post-Processing
1837
-------------------------

docs/conf.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
# built documents.
5656
#
5757
# The short X.Y version.
58-
version = '1.0'
58+
#version = '1.0'
5959
# The full version, including alpha/beta/rc tags.
60-
release = '1.0'
60+
#release = '1.0'
6161

6262
# The language for content autogenerated by Sphinx. Refer to documentation
6363
# for a list of supported languages.
@@ -112,6 +112,8 @@
112112
# a list of builtin themes.
113113
html_theme = 'sphinx_rtd_theme'
114114

115+
html_style = 'style.css'
116+
115117
# Theme options are theme-specific and customize the look and feel of a theme
116118
# further. For a list of options available for each theme, see the
117119
# documentation.
@@ -139,7 +141,7 @@
139141
# Add any paths that contain custom static files (such as style sheets) here,
140142
# relative to this directory. They are copied after the builtin static files,
141143
# so a file named "default.css" will overwrite the builtin "default.css".
142-
#html_static_path = ['_static']
144+
html_static_path = ['static']
143145

144146
# Add any extra paths that contain custom files (such as robots.txt or
145147
# .htaccess) here, relative to this directory. These files are copied

docs/generator.rst

+41-22
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,39 @@ Generates code for a given model. Additionally (and by default), it will generat
2626
* Config: ``gossi\codegen\config\CodeGeneratorConfig``
2727
* Options:
2828

29-
+-------------------------+---------+---------------+-----------------------------------------------------------------------------+
30-
| Key | Type | Default Value | Description |
31-
+=========================+=========+===============+=============================================================================+
32-
| generateDocblock | boolean | true | enables docblock generation prior to code generation |
33-
+-------------------------+---------+---------------+-----------------------------------------------------------------------------+
34-
| generateEmptyDocblock | boolean | true | allows generation of empty docblocks |
35-
+-------------------------+---------+---------------+-----------------------------------------------------------------------------+
36-
| generateScalarTypeHints | boolean | false | generates scalar type hints, e.g. in method/function parameters (PHP 7) |
37-
+-------------------------+---------+---------------+-----------------------------------------------------------------------------+
38-
| generateReturnTypeHints | boolean | false | generates scalar type hints for return values (PHP 7) |
39-
+-------------------------+---------+---------------+-----------------------------------------------------------------------------+
29+
+-------------------------+-----------------------------------+---------------+-------------------------------------------------------------------------+
30+
| Key | Type | Default Value | Description |
31+
+=========================+===================================+===============+=========================================================================+
32+
| generateDocblock | boolean | true | enables docblock generation prior to code generation |
33+
+-------------------------+-----------------------------------+---------------+-------------------------------------------------------------------------+
34+
| generateEmptyDocblock | boolean | true | allows generation of empty docblocks |
35+
+-------------------------+-----------------------------------+---------------+-------------------------------------------------------------------------+
36+
| generateScalarTypeHints | boolean | false | generates scalar type hints, e.g. in method/function parameters (PHP 7) |
37+
+-------------------------+-----------------------------------+---------------+-------------------------------------------------------------------------+
38+
| generateReturnTypeHints | boolean | false | generates scalar type hints for return values (PHP 7) |
39+
+-------------------------+-----------------------------------+---------------+-------------------------------------------------------------------------+
40+
| enableSorting | boolean | true | Enables sorting |
41+
+-------------------------+-----------------------------------+---------------+-------------------------------------------------------------------------+
42+
| useStatementSorting | boolean|string|Closure|Comparator | default | Sorting mechanism for use statements |
43+
+-------------------------+-----------------------------------+---------------+-------------------------------------------------------------------------+
44+
| constantSorting | boolean|string|Closure|Comparator | default | Sorting mechanism for constants |
45+
+-------------------------+-----------------------------------+---------------+-------------------------------------------------------------------------+
46+
| propertySorting | boolean|string|Closure|Comparator | default | Sorting mechanism for properties |
47+
+-------------------------+-----------------------------------+---------------+-------------------------------------------------------------------------+
48+
| methodSorting | boolean|string|Closure|Comparator | default | Sorting mechanism for methods |
49+
+-------------------------+-----------------------------------+---------------+-------------------------------------------------------------------------+
4050

4151
**Note**: when ``generateDocblock`` is set to ``false`` then ``generateEmptyDocblock`` is ``false`` as well.
4252

53+
**Note 2**: For sorting ...
54+
55+
* ... a string will used to find a comparator with that name (at the moment there is only default).
56+
* ... with a boolean you can disable sorting for a particular member
57+
* ... you can pass in your own ``\Closure`` for comparison
58+
* ... you can pass in a Comparator_ for comparison
59+
60+
.. _Comparator: https://phootwork.github.io/lang/comparison/
61+
4362
* Example:
4463

4564
::
@@ -63,17 +82,17 @@ Generates a complete php file with the given model inside. Especially useful whe
6382
* Config: ``gossi\codegen\config\CodeFileGeneratorConfig``
6483
* Options: Same options as ``CodeGenerator`` plus:
6584

66-
+--------------------+-----------------+---------------+----------------------------------------------------------------------------------------+
67-
| Key | Type | Default Value | Description |
68-
+====================+=================+===============+========================================================================================+
69-
| headerComment | string | empty | A comment, that will be put after the ``<?php`` statement |
70-
+--------------------+-----------------+---------------+----------------------------------------------------------------------------------------+
71-
| headerDocblock | string|Docblock | empty | A docblock that will be positioned after the possible header comment |
72-
+--------------------+-----------------+---------------+----------------------------------------------------------------------------------------+
73-
| blankLineAtEnd | boolean | true | Places an empty line at the end of the generated file |
74-
+--------------------+-----------------+---------------+----------------------------------------------------------------------------------------+
75-
| declareStrictTypes | boolean | false | Whether or not a ``declare(strict_types=1);`` is placed at the top of the file (PHP 7) |
76-
+--------------------+-----------------+---------------+----------------------------------------------------------------------------------------+
85+
+--------------------+----------------------+---------------+----------------------------------------------------------------------------------------+
86+
| Key | Type | Default Value | Description |
87+
+====================+======================+===============+========================================================================================+
88+
| headerComment | null|string|Docblock | null | A comment, that will be put after the ``<?php`` statement |
89+
+--------------------+----------------------+---------------+----------------------------------------------------------------------------------------+
90+
| headerDocblock | null|string|Docblock | null | A docblock that will be positioned after the possible header comment |
91+
+--------------------+----------------------+---------------+----------------------------------------------------------------------------------------+
92+
| blankLineAtEnd | boolean | true | Places an empty line at the end of the generated file |
93+
+--------------------+----------------------+---------------+----------------------------------------------------------------------------------------+
94+
| declareStrictTypes | boolean | false | Whether or not a ``declare(strict_types=1);`` is placed at the top of the file (PHP 7) |
95+
+--------------------+----------------------+---------------+----------------------------------------------------------------------------------------+
7796

7897
**Note**: ``declareStrictTypes`` sets ``generateScalarTypeHints`` and ``generateReturnTypeHints`` to ``true``.
7998

docs/getting-started.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ c) From Reflection:
8080
use gossi\codegen\generator\CodeGenerator;
8181
use gossi\codegen\model\PhpClass;
8282

83-
$class = PhpClass::fromReflection(new \ReflectionClass('MyClass'));
83+
$reflection = new \ReflectionClass('MyClass');
84+
$class = PhpClass::fromReflection($reflection->getFileName());
8485

8586
$generator = new CodeGenerator();
8687
$code = $generator->generate($class);

docs/images/hack-in-trait.odg

8.59 KB
Binary file not shown.

docs/images/hack-in-trait.png

17.2 KB
Loading

docs/index.rst

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
Welcome to PHP Code Generator's documentation!
22
==============================================
33

4+
.. |license| image:: https://poser.pugx.org/gossi/php-code-generator/license
5+
:target: https://packagist.org/packages/gossi/php-code-generator
6+
:alt: License
7+
8+
.. |version| image:: https://poser.pugx.org/gossi/php-code-generator/v/stable
9+
:target: https://packagist.org/packages/gossi/php-code-generator
10+
:alt: Latest Stable Version
11+
12+
.. |downloads| image:: https://poser.pugx.org/gossi/php-code-generator/downloads
13+
:target: https://packagist.org/packages/gossi/php-code-generator
14+
:alt: Total Downloads
15+
16+
17+
.. |hhvm| image:: http://hhvm.h4cc.de/badge/gossi/php-code-generator.svg?style=flat
18+
:target: http://hhvm.h4cc.de/package/gossi/php-code-generator
19+
:alt: HHVM Status
20+
21+
.. |build| image:: https://travis-ci.org/gossi/php-code-generator.svg?branch=master
22+
:target: https://travis-ci.org/gossi/php-code-generator
23+
:alt: Build Status
24+
25+
.. |quality| image:: https://scrutinizer-ci.com/g/gossi/php-code-generator/badges/quality-score.png?b=master
26+
:target: https://scrutinizer-ci.com/g/gossi/php-code-generator/?branch=master
27+
:alt: Scrutinizer Code Quality
28+
29+
.. |coverage| image:: https://scrutinizer-ci.com/g/gossi/php-code-generator/badges/coverage.png?b=master
30+
:target: https://scrutinizer-ci.com/g/gossi/php-code-generator/?branch=master
31+
:alt: Code Coverage
32+
33+
.. |br| raw:: html
34+
35+
<br>
36+
37+
|license| |version| |downloads| |br|
38+
|hhvm| |build| |quality| |coverage|
39+
440
This is a code generator for php code.
541

642
Quickstart

docs/model.rst

+46-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,51 @@ If you already have your class loaded, then you can use reflection to load your
223223
<?php
224224
use gossi\codegen\model\PhpClass;
225225

226-
$class = PhpClass::fromReflection(new \ReflectionClass('MyClass'));
226+
$reflection = new \ReflectionClass('MyClass');
227+
$class = PhpClass::fromReflection($reflection->getFileName());
227228

228229
Also reflection is nice, there is a catch to it. You must make sure ``MyClass`` is loaded. Also all the requirements (use statements, etc.) are loaded as well, anyway you will get an error when initializing the the reflection object.
230+
231+
Understanding Values
232+
--------------------
233+
234+
The models ``PhpConstant``, ``PhpParameter`` and ``PhpProperty`` support values; all of them implement the ``ValueInterface``. Though, there is a difference between values and expressions. Values refer to language primitives (``string``, ``int``, ``float``, ``bool`` and ``null``). Additionally you can set a ``PhpConstant`` as value (the lib understands this as a library primitive ;-). If you want more complex control over the output, you can set an expression instead, which will be generated as is.
235+
236+
Some Examples::
237+
238+
<?php
239+
PhpProperty::create('foo')->setValue('hello world.');
240+
// $foo = 'hello world.';
241+
242+
PhpProperty::create('foo')->setValue(300);
243+
// $foo = 300;
244+
245+
PhpProperty::create('foo')->setValue(3.14);
246+
// $foo = 3.14;
247+
248+
PhpProperty::create('foo')->setValue(false);
249+
// $foo = false;
250+
251+
PhpProperty::create('foo')->setValue(null);
252+
// $foo = null;
253+
254+
PhpProperty::create('foo')->setValue(PhpConstant::create('BAR'));
255+
// $foo = BAR;
256+
257+
PhpProperty::create('foo')->setExpression('self::MY_CONST');
258+
// $foo = self::MY_CONST;
259+
260+
PhpProperty::create('foo')->setExpression("['my' => 'array']");
261+
// $foo = ['my' => 'array'];
262+
263+
For retrieving values there is a ``hasValue()`` method which returns ``true`` whether there is a value or an expression present. To be sure what is present there is also an ``isExpression()`` method which you can use as a second check::
264+
265+
<?php
266+
267+
if ($prop->hasValue()) {
268+
if ($prop->isExpression()) {
269+
// do something with an expression
270+
} else {
271+
// do something with a value
272+
}
273+
}

docs/static/style.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import 'css/theme.css';
2+
3+
.rst-content .section li > ol,
4+
.rst-content li > ol.arabic {
5+
margin-bottom: 0;
6+
}

0 commit comments

Comments
 (0)