nWidart / laravel-modules

Module Management In Laravel
https://docs.laravelmodules.com
MIT License
5.48k stars 951 forks source link

How to define module dependency to another module #428

Closed messi89 closed 6 years ago

messi89 commented 6 years ago

Hi guys

I'm looking on the documentation but I didn't find any answer...

Let's say that I have a module A and another module B, I want to tell to laravel don't enable B if A is not installed (or disabled)

Is there any option to define (on module.json maybe) require => ["A", "X", ""] on the module B config ?

nWidart commented 6 years ago

You can use general composer dependencies for this.

ghost commented 6 years ago

Sorry but would you please give a simple example for this, Thank you

robbanl commented 4 years ago

@nWidart Sorry to bump an old thread, but I just found this package today and I'm totally loving it. But, I'm having issues, same as thread starter :-(

Module Project composer.json requires Module Client. Running php artisan module:update I get the following error.

Could not find a matching version of package codepeak/project. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (dev).

It feels like composer isn't recognising the version nor the package :-( Or what do I miss or don't understand? :-)

nWidart commented 4 years ago

Hi,

Can you try to run composer update inside the module itself? To check if composer can find it. In either case, you will have to add this dependency to your root composer json file too. (you can lookup "composer merge plugin" if you'd like to avoid this)

robbanl commented 4 years ago

@nWidart Thank you for your reply. Running composer update gives me the error message

The requested package codepeak/client could not be found in any version, there may be a typo in the package name.

Not sure about the package composer merge plugin, but as a work-around, I've made a helper in the Laravel-project:

/**
 * Make sure module is enabled and installed
 *
 * @todo Run version compare with $version variable
 * @throws Exception
 */
function require_module(string $name, $version = null): void
{
    // Make sure module Client exists and is enabled
    $client = Module::find($name);
    if (is_null($client) || $client->isEnabled() === false) {
        throw new \Exception('Module "' . $name . '" is required.');
    }
}

And inside the modules ServiceProvider I run require_module('client');. If the module cannot be found, there will be an error thrown. Not sure how this impacts performance, but it works for now at least :-)