Skip to content

Commit

Permalink
Implement addon manager class, addons now have to register their call…
Browse files Browse the repository at this point in the history
…backs.
  • Loading branch information
franzliedke committed Jan 18, 2015
1 parent 8af42e8 commit de305cf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
61 changes: 37 additions & 24 deletions include/addons.php
Expand Up @@ -10,39 +10,55 @@
if (!defined('PUN'))
exit;

$flux_addons = array();
$flux_addons_loaded = false;
$flux_addons = new flux_addon_manager();

function flux_addons_load()

class flux_addon_manager
{
global $flux_addons, $flux_addons_loaded;
var $hooks = array();

var $loaded = false;

foreach (glob(PUN_ROOT.'addons/*.php') as $addon_file)
function load()
{
$addon_name = 'addon_'.basename($addon_file, '.php');
include $addon_file;
$this->loaded = true;

$flux_addons[] = new $addon_name;
}
foreach (glob(PUN_ROOT.'addons/*.php') as $addon_file)
{
$addon_name = 'addon_'.basename($addon_file, '.php');

$flux_addons_loaded = true;
}
include $addon_file;
$addon = new $addon_name;

function flux_hook($name)
{
global $flux_addons, $flux_addons_loaded;
$addon->register($this);
}
}

function bind($hook, $callback)
{
if (!isset($this->hooks[$hook]))
$this->hooks[$hook] = array();

if (!$flux_addons_loaded)
flux_addons_load();
$this->hooks[$hook][] = $callback;
}

// Give every plugin the chance to run some code specific for this hook
foreach ($flux_addons as $addon)
function hook($name)
{
$hook = 'hook_'.$name;
$addon->$hook();
if (!$this->loaded)
$this->load();

$callbacks = isset($this->hooks[$name]) ? $this->hooks[$name] : array();

// Execute every registered callback for this hook
foreach ($callbacks as $callback)
{
list($addon, $method) = $callback;
$addon->$method();
}
}
}


/**
* Class flux_addon
*
Expand All @@ -51,9 +67,6 @@ function flux_hook($name)
*/
class flux_addon
{
function hook_register_validate()
{ }

function hook_register_pre_submit()
function register($manager)
{ }
}
11 changes: 11 additions & 0 deletions include/functions.php
Expand Up @@ -670,6 +670,17 @@ function get_tracked_topics()
}


//
// Shortcut method for executing all callbacks registered with the addon manager for the given hook
//
function flux_hook($name)
{
global $flux_addons;

$flux_addons->hook($name);
}


//
// Update posts, topics, last_post, last_post_id and last_poster for a forum
//
Expand Down

0 comments on commit de305cf

Please sign in to comment.