Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Dynamic Modules load and info retrieve
  • Hi guys,
    I wrote this code for scan available plugins and retrieve informations :
    foreach (array_keys(\File::read_dir(APPPATH . '..' . DS . 'plugins' . DS, 1)) as $plugin_name) 
    {
        $plugin_path = \Fuel::add_module($plugin_name);           
        $plugin_inst = "\\$plugin_name\\controller_$plugin_name";
                
        $plugins_info[$plugin_name] = $plugin_inst::info();
    }
            
    $view->plugins = $plugins_info;
    

    It works fine but I'm not sure if is the best way for execute info() method in dynamic way. Any suggestion?
  • It's fine, just know it's kinda slow (like any dynamic method call) compared to a normal method call. Alternatively you could use call_user_func() instead, when the method or class doesn't exist that will just output false instead of crashing:
    $plugins_info[$plugin_name] = call_user_func($plugin_inst.'::info');
    
  • You could always use
    call_user_method
    

    But it's slower than what you're doing now. Could you be a little more specific in what you want to accomplish, and what you mean by "dynamic"... you mean pass arguments? Or perhaps check if the method exists?
  • Ok, thank you guys!
    now I've changed previous call with call_user_func($plugin_inst.'::info'); suggested by Jelmer. @frank :
    The doubt on my code was on :
    $plugin_inst = "\\$plugin_name\\controller_$plugin_name";
    

    For "dynamic" I meant there's not a manual call to a module method like "\PluginNamesp\PluginClass\Plugin\Method" ... but call methods using variables inside a loop. This is useful if you want create an available plugins table like wordpress.
  • My mistake, I was looking at
    $plugins_info[$plugin_name] = $plugin_inst::info();
    
  • Less code :
    foreach (array_keys(\File::read_dir(APPPATH . '..' . DS . 'plugins' . DS, 1)) as $plugin)
    {
        \Fuel::add_module($plugin);           
        $plugins_info[$plugin] = call_user_func("\\$plugin\\controller_$plugin" . '::info');
    }
    
    $view->plugins = $plugins_info;
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion