Skip to content

Commit ff24ae6

Browse files
DonutsNLcconard96trasher
authored
Added information (#145)
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>
1 parent 3a3b0cd commit ff24ae6

1 file changed

Lines changed: 74 additions & 3 deletions

File tree

‎source/plugins/guidelines.rst‎

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The plugin directory structure should look like the following:
2020

2121
* |phpfile| `...`
2222

23-
* |folder| `inc`
23+
* |folder| `inc` and/or `src`
2424

2525
* |phpfile| `...`
2626

@@ -42,20 +42,91 @@ The plugin directory structure should look like the following:
4242
* |phpfile| `...`
4343

4444
* `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`_,
4647
* if you internationalize your plugin, localization files will be found under the `locale` directory,
4748
* if you need any scripting tool (like something to extract or update your translatable strings), you can put them in the `tools` directory
4849
* a `README.md` file describing the plugin features, how to install it, and so on,
4950
* a `LICENSE` file containing the license,
5051
* `MyPlugin.xml` and `MyPlugin.png` can be used to reference your plugin on the `plugins directory website <http://plugins.glpi-project.org>`_,
5152
* the required `setup.php` and `hook.php` files.
5253

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\`.
62+
63+
+-------------+------------------------------------------------------------+
64+
| Directive | Composer mapping |
65+
+=============+============================================================+
66+
| \GlpiPlugin | maps (virtually) to /plugins or /marketplace |
67+
+-------------+------------------------------------------------------------+
68+
| \MyPlugin | maps to: /myplugin/src converted strtolower |
69+
+-------------+------------------------------------------------------------+
70+
| \SubFolder | maps to /src/SubFolder/ using provided case |
71+
+-------------+------------------------------------------------------------+
72+
| \ClassName | maps to ../ClassName.php using provided case apending .php |
73+
+-------------+------------------------------------------------------------+
74+
75+
76+
``GLPI_ROOT/marketplace/myplugin/src/Test.php``
77+
78+
.. code-block:: php
79+
80+
<?php
81+
82+
namespace GlpiPlugin\MyPlugin;
83+
84+
class Test extends CommonDBTM
85+
{
86+
\\ Your class code...
87+
}
88+
89+
?>
90+
91+
``GLPI_ROOT/marketplace/myplugin/src/ChildClass/ResultOutcomes.php``
92+
93+
.. code-block:: php
94+
95+
<?php
96+
97+
namespace GlpiPlugin\MyPlugin\ChildClass;
98+
99+
class ResultOutcomes extends CommonDBTM
100+
{
101+
\\ Your class code...
102+
}
103+
104+
?>
105+
106+
``GLPI_ROOT/marketplace/myplugin/setup.php``
107+
108+
.. code-block:: php
109+
110+
<?php
111+
112+
use GlpiPlugin\MyPlugin\Test;
113+
use GlpiPlugin\Myplugin\ChildClass\ResultOutcomes;
114+
115+
function usingTest() : void
116+
{
117+
$t = new Test();
118+
$r = new ResultOutcomes();
119+
}
120+
121+
?>
122+
123+
53124
Where to write files?
54125
+++++++++++++++++++++
55126

56127
.. warning::
57128

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!
59130

60131
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).
61132

0 commit comments

Comments
 (0)