Plugin dependencies in composer?

Plugin dependencies in composer?

by Elliot Smith -
Number of replies: 2

I'm attempting to write a Moodle plugin which accesses a RESTful web service external to the Moodle instance. I'd like to use external libraries for the HTTP client and for the code which processes the responses.

Ideally, I would install these libraries using composer. But how do I incorporate a composer download into the plugin? Should I be distributing the vendor libraries with my code (which has licensing implications)? Or can I get Moodle to run a post-install script as part of the plugin install, which will call composer in the correct directory and download the dependencies?

Thanks.

Average of ratings: -
In reply to Elliot Smith

Re: Plugin dependencies in composer?

by Daniel Del Rio -

Did you ever find a solution to this?

In reply to Daniel Del Rio

Re: Plugin dependencies in composer?

by Darko Miletić -

You need to ship the third-party libraries with plugin. That means following:

Create composer.json file in the root of your plugin dir.

Add whatever libraries you need like this:

{
    "require-dev": {
        "foo/fii-test": "1.6.1.*",
        "faa/feee":"3.0.0",
    }
}

Install composer binary on your system

Execute composer from within the root of your plugin this should download stuff into [yourplugindir]/vendor

That's it. You ship the json and vendor dir with your plugin.

Whenever you need to use some of the libraries just provide the include like this:

require_once($CFG->dirroot . '/path/to/module/vendor/autoload.php');
Average of ratings: Useful (1)