You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/best-practices.rst
+21-2
Original file line number
Diff line number
Diff line change
@@ -11,8 +11,27 @@ It is useful to use some kind of template system to load the contents for your b
11
11
Hack in Traits
12
12
--------------
13
13
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.
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.
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::
0 commit comments