ElkArte Community

Elk Development => Feature Discussion => Topic started by: emanuele on December 31, 2013, 08:33:29 am

Title: Addons
Post by: emanuele on December 31, 2013, 08:33:29 am
Code: [Select]
	if (($loaders = cache_get_data('addon_loaders', 3600)) === null)
{
$addondirs = glob(ADDONSDIR . '/*', GLOB_ONLYDIR);
foreach ($addondirs as $dir)
{
$loader = array();
$loader['file'] = $dir . '/index.php';

if (file_exists($loader['file']))
{
require_once($loader['file']);
$function = ucfirst(basename($dir)) . '_Addon';

if (class_exists($function))
$loader['function'] = array($function, 'Hooks');
elseif (function_exists($function))
$loader['function'] = $function;
else
continue;

call_user_func($loader['function']);
$loaders[] = $loader;
}
}
cache_put_data('addon_loaders', $loaders, 3600);
}
elseif (!empty($loaders))
{
foreach ($loaders as $loader)
{
require_once($loader['file']);
call_user_func($loader['function']);
}
}

Just before
Code: [Select]
call_integration_include_hook('integrate_pre_include');

So, putting addons into a specific directory with an index.php properly formatted, will allow to load the hooks automatically.
Cached because addons shouldn't change so frequently and it avoids few filesystem accesses.

Something like this would be enough:
Code: [Select]
class Test_Addon
{
public static function Hooks()
{
add_integration_function('a_hook', 'my_function', false);
}
}

or for lazier people:
Code: [Select]
function Test_Addon()
{
add_integration_function('a_hook', 'my_function', false);
}