Highco's avatar

Composer package 'registration' and module/package development

Hello all,

TL;DR

How can i create a 'module manager' in laravel where composer package can plug into and where i can get back the list of the plugged in packages ?

The problem

I build an APP that can perform somme image transformation on uploaded images. This transformations are provided by composer paskages, and are groupped in different use: (color, transformation, cut and resize, etc... )

I would like to know the list of 'image modifier' package and in which usecase (color, transformation, cut and resize, etc... ).

I don't like to put this in the composer.json., i would like to do this in a system like the package say: Hey laravel, i am 'Awesome Effects 3000' and i can do 'solarize' and 'destructurate' in 'colors'

A little like this in php:

class awesomePackage3000 extends imagePackageManager
{
    function registerImagePackage() {
        $app->imagePackages[] = [
            'name' => 'Awesome Effect 3000',
            'identifier' => 'awesome_effect_3000',
            'version' => '4.2',
            'can_do' => ['solarize', 'destructurate'],
            'use_case' => 'colors'
        ];
    }

}

I can have an artisan command for updating the list of package that are modules like this: artisan modules:refresh Where it update the 'modules' table in the db for example.

And doing somewhere else in laravel (in the imageModulesController for example) :


    if (!checkModuleAvaliability('awesome_effect_3000'){
        abort(403, 'Module do not exist or is not activated');
    }

The questions

  • I don't know where to declare the imagePackageManager class ...
  • ... and how it will working/live inside laravel.
  • That's a "service provider" in sens of laravel?

Many thanks!! Seby

0 likes
1 reply
sdebacker's avatar
Level 2

Hello Seby,

A service provider is a class that can be used to store data in the container. In the boot method of your module’s ServiceProvider class, you could add:

        // Get the modules already present in the container
        if (!isset($this->app['imagePackages'])) {
            $this->app->instance('imagePackages', []);
        }
        $packages = $this->app['imagePackages'];
        // Add the new module
        $imagePackages[] = ['hello'];
        // store it again in the container
        $this->app->instance('imagePackages', $imagePackages);

You could also bind a Repository class (http://laravel-recipes.com/recipes/44/resolving-objects-from-the-ioc-container) to app’s imagePackages or use a package like https://github.com/pingpong-labs/modules

1 like

Please or to participate in this conversation.