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
Added some additional information on using the Composer autoloader and /src/ in the Directory structure.
Co-authored-by: Curtis Conard <cconard96@gmail.com>
Co-authored-by: Johan Cwiklinski <jcwiklinski@teclib.com>
Copy file name to clipboardExpand all lines: source/plugins/guidelines.rst
+74-3Lines changed: 74 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ The plugin directory structure should look like the following:
20
20
21
21
* |phpfile| `...`
22
22
23
-
* |folder| `inc`
23
+
* |folder| `inc` and/or `src`
24
24
25
25
* |phpfile| `...`
26
26
@@ -42,20 +42,91 @@ The plugin directory structure should look like the following:
42
42
* |phpfile| `...`
43
43
44
44
* `front` will host all PHP files directly used to display something to the user,
45
-
* `inc` will host all classes,
45
+
* `inc` is the legacy way to host all classes,
46
+
* `src` is the new way to host classes; relying on `PSR-4 autoload`_,
46
47
* if you internationalize your plugin, localization files will be found under the `locale` directory,
47
48
* if you need any scripting tool (like something to extract or update your translatable strings), you can put them in the `tools` directory
48
49
* a `README.md` file describing the plugin features, how to install it, and so on,
49
50
* a `LICENSE` file containing the license,
50
51
* `MyPlugin.xml` and `MyPlugin.png` can be used to reference your plugin on the `plugins directory website <http://plugins.glpi-project.org>`_,
51
52
* the required `setup.php` and `hook.php` files.
52
53
54
+
PSR-4 autoload
55
+
++++++++++++++
56
+
57
+
.. version-added:: 10.0
58
+
59
+
In order to use the Composer PSR-4 autoloader in your plugin, must place your PHP class files in the `/src` directory instead of `/inc`. In this scenario the `/inc` directory should no longer be present in the plugin folder structure.
60
+
61
+
The convention to be used is (Case sensitive): `namespace GlpiPlugin\Myplugin;`. The namespace should be added to every class in the `/src` directory and per the PSR-12 PHP convention be placed in the top of your class. Classes using the `GlpiPlugin\Myplugin\` namespaces will be loaded from: `GLPI_ROOT\plugins\myplugin\src\`. To include folders inside the `/src` directory simply add them to your namespace and use keywords i.e. `namespace GlpiPlugin\Myplugin\SubFolder\` will load from `GLPI_ROOT\plugins\myplugin\src\SubFolder\`.
use GlpiPlugin\Myplugin\ChildClass\ResultOutcomes;
114
+
115
+
function usingTest() : void
116
+
{
117
+
$t = new Test();
118
+
$r = new ResultOutcomes();
119
+
}
120
+
121
+
?>
122
+
123
+
53
124
Where to write files?
54
125
+++++++++++++++++++++
55
126
56
127
.. warning::
57
128
58
-
Plugins my never ask user to give them write access on their own directory!
129
+
Plugins may never ask user to give them write access on their own directory!
59
130
60
131
The GLPI installation already ask for administrator to get write access on its ``files`` directory; just use ``GLPI_PLUGIN_DOC_DIR/{plugin_name}`` (that would resolve to ``glpi_dir/files/_plugins/{plugin_name}`` in default basic installations).
0 commit comments