Retrieve all modules

I’m building a application where I give the admin the option to activate/deactivate modules using the web administration so he doesn’t have to edit the configuration files manually so I need to know what modules are inside the module/ folder. I could parse the folder and see what’s inside but it may be a better solution.

Something like Yii::app ()->getModules () but with a result which includes the loaded/unloaded modules. Is there such a method I can use?

Did you try that? I think that exact method should be available: http://www.yiiframework.com/doc/api/CModule#getModules-detail (CApplication extends CModule).

Yii::app ()->getModules () only returns the modules which are manually added in the config file:




'modules'=>array('forum',...)

But this is not very friendly for noobs so I need a way to know exactly all the modules available in the modules/ dir…loaded or not.

try this:




  /**

   * Find a module searching in application modules and if it's not found there

   * looks in modules' modules

   * @param String $moduleID The model to find

   * @return The module, if it's found else null

   */

  public static function findModule($moduleID) {

    if(Yii::app()->getModule($moduleID)) {

      return Yii::app()->getModule($moduleID);

    }

    $modules = Yii::app()->getModules();

    foreach ($modules as $mod=>$conf) {

      if(Yii::app()->getModule($mod)) {

        return self::findInModule(Yii::app()->getModule($mod), $moduleID);

      }

    }

    return null;

  }


  /**

   * Search for a child module

   * @param String $parent The parent module

   * @param String $moduleID The module to find

   * @return The module, if it's not found returns null

   */

  private static function findInModule($parent, $moduleID) {

    if ($parent->getModule($moduleID)) {

      return $parent->getModule($moduleID);

    } else {

      $modules = $parent->getModules();

      foreach ($modules as $mod => $conf) {

        return $this->findInModule($parent->getModule($mod), $moduleID);

      }

    }

    return null;

  }



This is a findModule function. I need something that returns all the available modules not a find function.

Bump…before I do anything stupid that I might regret later :P

Corrections

Thanks to jerry2801, your functions put me in the right way.

I’ve noticed errors when I was trying to find a nested module. I’ll figured it out, I posted my code for those who need it too.




/**

    * Find a module searching in application modules and if it's not found there

    * looks in modules' modules

    * @param String $moduleID The model to find

    * @return The module, if it's found else null

    */

    public static function findModule($moduleID) {

        if(Yii::app()->getModule($moduleID)) {

            return Yii::app()->getModule($moduleID);

        }

        $modules = Yii::app()->getModules();

        foreach ($modules as $mod => $conf) {

            if(Yii::app()->getModule($mod)) {

                $module = self::findInModule(Yii::app()->getModule($mod), $moduleID);

                if($module)

                    return $module;

            }

        }

        return null;

    }


    /**

    * Search for a child module

    * @param String $parent The parent module

    * @param String $moduleID The module to find

    * @return The module, if it's not found returns null

    */

    private static function findInModule($parent, $moduleID) {

        if ($parent->getModule($moduleID)) {

            return $parent->getModule($moduleID);

        } else {

            $modules = $parent->getModules();

            if(!empty($modules)) { // Here was the principal error, when $modules is empty, it returns a CBaseController object.

                foreach ($modules as $mod => $conf) {

                    return $this->findInModule($parent->getModule($mod), $moduleID);

                }

            }

        }

        return null;

    }



I never would call


Yii::app()->getModule($mod) or $parent->getModule($mod)

in a loop method on finding a module.

That means, all that all these modules will be instantiated and initialized…

Do you really want to instantiate all the modules on finding a module?

Why not only parse the array of the configured modules returned by ‘getModules()’?