Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion source/plugins/guidelines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Guidelines
Directories structure
^^^^^^^^^^^^^^^^^^^^^

PRE GLPI 10
++++++++++

Comment on lines +9 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire paragraph must be removed if it's indeed related to versions prior to GLPI v10.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change currently only adds the label. You want me to drop the paragraph or just accept this change from someone else?

But considering my recent rewrite i think it needs a rewrite for glpi11 as well 😅

In that regard, What are the (structuring) rules regarding documenting for various versions. With GLPI11 alot is changing in regards to the folder structure (dropping marketplace, no more front (if done right), etc)

Should the convention documentation only describe the latest (x) versions, only the supported versions, all versions?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Developers documentation currently only targets GLPI v11.

Changes made in v11 can be marked with a .. versionadded:: 11.0.0 or .. versionchanged:: 11.0.0 and v10 info can be kept - if you want.
But everything older should just be removed (versions are no longer supported).

Real structure will depend of what your plugin propose. See :doc:`requirements <requirements>` to find out what is needed. You may also want to :ref:`take a look at GLPI File Hierarchy Standard <fhs>`.

.. warning::
Expand Down Expand Up @@ -50,12 +53,80 @@ The plugin directory structure should look like the following:
* `MyPlugin.xml` and `MyPlugin.png` can be used to reference your plugin on the `plugins directory website <http://plugins.glpi-project.org>`_,
* the required `setup.php` and `hook.php` files.

POST GLPI 10
+++++++++++

In GLPI 10 and newer installations you are advised to use namespaces and the Composer PSR-4 autoloader. Classes using namespaces are no longer loaded by the old autoload.function.php but by the newer Composer autoloader. In order to use the Composer 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.

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\`.

+-------------+------------------------------------------------------------+
| Directive | Composer mapping |
+=============+============================================================+
| \GlpiPlugin | maps to /plugins or /marketplace |
+-------------+------------------------------------------------------------+
| \MyPlugin | maps to: /myplugin/src converted strtolower |
+-------------+------------------------------------------------------------+
| \SubFolder | maps to /src/SubFolder/ using provided case |
+-------------+------------------------------------------------------------+
| \ClassName | maps to ../ClassName.php using provided case apending .php |
+-------------+------------------------------------------------------------+


GLPI_ROOT/marketplace/myplugin/src/Test.php

.. code-block:: php

<?php

namespace GlpiPlugin\MyPlugin;

class Test extends CommonDBTM
{
\\ Your class code...
}

?>

GLPI_ROOT/marketplace/myplugin/src/ChildClass/ResultOutcomes.php

.. code-block:: php

<?php

namespace GlpiPlugin\MyPlugin\ChildClass;

class ResultOutcomes extends CommonDBTM
{
\\ Your class code...
}

?>

GLPI_ROOT/marketplace/myplugin/setup.php

.. code-block:: php

<?php

use GlpiPlugin\MyPlugin\Test;
use GlpiPlugin\Myplugin\ChildClass\ResultOutcomes;

function usingTest() : void
{
$t = new Test();
$r = new ResultOutcomes();
}

?>


Where to write files?
+++++++++++++++++++++

.. warning::

Plugins my never ask user to give them write access on their own directory!
Plugins may never ask user to give them write access on their own directory!

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

Expand Down