XoopsCore icon indicating copy to clipboard operation
XoopsCore copied to clipboard

Roadmap: Namespace Conventions for Modules

Open geekwright opened this issue 10 years ago • 2 comments

A standard for namespace use in modules needs to be defined. Some elements to consider:

  • namespace key in xoops_version.php
  • autoloader conventions, for example psr-4 autoloading for the declared namespace from the class directory. A standard here would eliminate a great deal of preload chatter at startup.

geekwright avatar Feb 21 '15 20:02 geekwright

An example of a xoops module namespace could be: xoops\modules\dummy

Example of usage in file ROOT/modules/dummy/class/ObjectHandler.php:

namespace xoops\modules\dummy\class; class ObjectHandler {}

Do we need a namespace key in xoops_version.php?

The XoopsLoad class would handle the mapping:

            $prefix = 'xoops\\modules\\';
            $len = strlen($prefix);
            if (strncmp($prefix, $class, $len) === 0) {
                $relative_class = substr($class, $len);
                $base_dir = XOOPS_ROOT_PATH . DIRECTORY_SEPARATOR . 'modules';
                $file = $base_dir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $relative_class) . '.php';
                if (!self::loadFile($file)) {
                    return false;
                }
            } else {
                return false;
            }

trabisdementia avatar Mar 12 '17 13:03 trabisdementia

I started to convert some of the XOOPS 2.5.x modules to Namespaces, using "Xoopsmodules\modulename" as the top namespace, so we have in Oledrion (https://github.com/mambax7/oledrion)

namespace Xoopsmodules\Oledrion;

This can be then easily changed to whatever the Core team decides to use in the future...

mambax7 avatar Jan 02 '18 09:01 mambax7