Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Karmasterrr on June 21, 2022, 05:00:02 PM

Title: What is the proper way to use COMPOSER inside SMF mod ?
Post by: Karmasterrr on June 21, 2022, 05:00:02 PM
What is the proper way to use php composer inside SMF mod ?
Title: Re: What is the proper way to use COMPOSER inside SMF mod ?
Post by: Arantor on June 22, 2022, 02:36:32 AM
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.
Title: Re: What is the proper way to use COMPOSER inside SMF mod ?
Post by: Karmasterrr on June 22, 2022, 07:32:38 AM
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 ...
Title: Re: What is the proper way to use COMPOSER inside SMF mod ?
Post by: Arantor on June 22, 2022, 07:47:18 AM
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.
Title: Re: What is the proper way to use COMPOSER inside SMF mod ?
Post by: Dhayzon on June 28, 2022, 05:29:47 PM
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
(https://i.imgur.com/OXcdhQC.jpg)

composer file


{
    "autoload": {
        "classmap": [
        ]
    },
    "config": {
            "vendor-dir": "Plugins"
    },
    "require": {
        "guzzlehttp/guzzle": "^7.4",
        "sabre/xml": "^2.2"
    }
}
Title: Re: What is the proper way to use COMPOSER inside SMF mod ?
Post by: Arantor on June 28, 2022, 06:11:08 PM
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.