# HG changeset patch # User Dan Fuhry # Date 1353342997 18000 # Node ID b19698a37d58479e865d69b208ff53449190d859 # Parent c660cf54c04523d34bfb7e4e67c38592ff1c107a Plugins: support closures diff -r c660cf54c045 -r b19698a37d58 includes/plugins.php --- a/includes/plugins.php Mon Nov 19 11:35:17 2012 -0500 +++ b/includes/plugins.php Mon Nov 19 11:36:37 2012 -0500 @@ -27,7 +27,23 @@ * @access private */ - var $hook_list; + var $hook_list = array(); + + /** + * List of hooks which are lambda (anonymous) functions + * @var array + * @access private + */ + + var $hook_list_lambda = array(); + + /** + * Temporary array which holds closures currently being called + * @var array + * @access private Technically public access, but seriously just don't touch it + */ + + var $__current_hook = array(); /** * The list of plugins that should be loaded. Used only by common.php. @@ -104,6 +120,12 @@ function setHook($name, $dont_split = false) { + if ( !empty($this->hook_list_lambda[$name]) ) + { + $this->__current_hook =& $this->hook_list_lambda[$name]; + // This is kind of terrible right now, I'm not sure how to handle inheritance and such. + $this->attachHook($name, 'foreach ( $plugins->__current_hook as $func ) { call_user_func($func); }; unset($plugins->__current_hook);'); + } if ( !empty($this->hook_list[$name]) && is_array($this->hook_list[$name]) ) { if ( $dont_split ) @@ -136,11 +158,22 @@ function attachHook($name, $code) { - if ( !isset($this->hook_list[$name]) ) + if ( is_callable($code) ) { - $this->hook_list[$name] = Array(); + if ( !isset($this->hook_list_lambda[$name]) ) + { + $this->hook_list_lambda[$name] = Array(); + } + $this->hook_list_lambda[$name][] = $code; } - $this->hook_list[$name][] = $code; + else + { + if ( !isset($this->hook_list[$name]) ) + { + $this->hook_list[$name] = Array(); + } + $this->hook_list[$name][] = $code; + } } /**