How to set correct Module Instance?

I’m creating a filemanager module inspired on the https://github.com/PendalF89/yii2-filemanager, but with new features like on-the-fly thumbnail generation.

In the main model I have a function that return the thumbnail url this function will work in different ways depending on the thumbnailOnTheFly parameter that is defined in the module configurations.

Configuration:




'filemanager' => [

            'class' => 'fabiomlferreira\filemanager\Module',

            'rename' => true, 

            'autoUpload' => true,

            'optimizeOriginalImage' => true,

            'maxSideSize' => 1200,

            'thumbnailOnTheFly' => false,  //if is true the component thumbnail should be used

            // Thumbnails info

            'thumbs' => [ 

                'default' => [

                    'name' => 'default',

                    'size' => [125, 125],

                ],

                'blog_list' => [

                    'name' => 'blog_list',

                    'size' => [330, 330],

                ],

            ],

]



The method to return the thumbnail url is this:




/**

     * @param string $alias thumb alias

     * @return string thumb url

     */

    public function getThumbUrl($alias)

    {

        $module = Module::getInstance();

        //if is to use the regular thumbnail generation

        if($module->thumbnailOnTheFly == false){

            $thumbs = $this->getThumbs();

           

            if ($alias === 'original') {

                return $this->url;

            }


            return !empty($thumbs[$alias]) ? $thumbs[$alias] : '';

        }else{

            $sizes = !empty($module->thumbs[$alias]) ? $module->thumbs[$alias] : '';

            if($sizes == '')

                return '';

            

            $width = $sizes['size'][0];

            $height = $sizes['size'][1];

            $mode = isset($sizes['mode']) ? $sizes['mode'] : ImageInterface::THUMBNAIL_OUTBOUND;

            return Yii::$app->thumbnail->url("$this->url", [

                'thumbnail' => [

                    'width' =>  $width,

                    'height' => $height,

                    'mode' => $mode 

                ],

                'placeholder' => [

                    'width' =>  $width,

                    'height' => $height

                ]

            ]);

        }

    }



This code sometimes works, but other times the $module = Module::getInstance(); is null I because there are no instance initiated for the Module.

Can someone give me an hand. I don’t understand why in some points this return the module and in other points return null. If someone wants can install the module via composer “fabiomlferreira/yii2-file-manager”:“dev-master”


Yii::$app->getModule("filemanager")

This is the solution that I end up the problem is if the user change the module name.

I did this:




public $moduleName = 'filemanager';


========


$module = Module::getInstance();

        if($module===null)

            $module = \Yii::$app->getModule($this->moduleName);




I create a public var called $moduleName that is by default ‘filemanager’ is the user use other name he must set the moduleName before call a method that need the module instance.

1 Like