What is the proper way to use php composer inside SMF mod ?
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.
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 ...
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.
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"
}
}
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.