What is the proper way to use COMPOSER inside SMF mod ?

Started by Karmaster, June 21, 2022, 05:00:02 PM

Previous topic - Next topic

Karmaster

What is the proper way to use php composer inside SMF mod ?

Arantor

There isn't really one. If you do happen to use Composer you'll still need to do the install before shipping the files around since SMF itself has no mechanism for calling or using Composer.
Perhaps it would have been better if I'd simply never bothered. Y'all clearly would be less unhappy that way.

Karmaster

Okay I can ship all vendor files with mod.
Interesting, what would happen if different mods will do this:
require_once ("mod_dir/vendor/autoload.php");
in different mod folders ...

Arantor

Nothing untoward, aside from the fact I haven't seen a mod do this yet.

Every autoload.PHP invokes a uniquely named class in the rest of the files just in case someone tries to do what you're doing.
Perhaps it would have been better if I'd simply never bothered. Y'all clearly would be less unhappy that way.

Dhayzon

I  have this example

FOLDER

-YOURMOD
--PLUGINS
--NiceMod
--Init.php

Init.php

namespace YourMod\NiceMod;
 
Class Main{
  function __construct(){
        global $sourcedir, $modSettings,$settings; 
spl_autoload_register(function ($class) {
          global $sourcedir;   
            if (strpos($class, 'YourMod') !== false){
              $class = $sourcedir.'/'.$class.''; 
              require(str_replace("\\", "/", $class) . '.php');
            }
            return false;
        });

require($sourcedir.'/YourMod/Plugins/autoload.php');
  }



new  Main();

?>

INSIDE Plugins


composer file


{
    "autoload": {
        "classmap": [
        ]
    },
    "config": {
            "vendor-dir": "Plugins"
    },
    "require": {
        "guzzlehttp/guzzle": "^7.4",
        "sabre/xml": "^2.2"
    }
}

Arantor

You can actually simplify it down in that you can declare your plugin's files as using a PSR-4 namespace autoloader and let Composer build that for you so all you ever need to do in your own code is just invoke the autoloader from whatever you're hooking into SMF.
Perhaps it would have been better if I'd simply never bothered. Y'all clearly would be less unhappy that way.

Advertisement: