| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_HooksWrapper |
| 3 |
{ |
| 4 |
private $obj = NULL; |
| 5 |
private $h = NULL; |
| 6 |
|
| 7 |
public function __construct( $obj, HC3_IHooks $hooks ) |
| 8 |
{ |
| 9 |
$this->obj = $obj; |
| 10 |
$this->h = $hooks; |
| 11 |
} |
| 12 |
|
| 13 |
public function _getClass() |
| 14 |
{ |
| 15 |
return get_class( $this->obj ); |
| 16 |
} |
| 17 |
|
| 18 |
public function __call( $method, $args ) |
| 19 |
{ |
| 20 |
$hook = get_class($this->obj) . '::' . $method; |
| 21 |
$hook = strtolower( $hook ); |
| 22 |
$hook = str_replace( '_', '/', $hook ); |
| 23 |
|
| 24 |
$hookBefore = $hook . '::before'; |
| 25 |
$hookAfter = $hook . '::after'; |
| 26 |
|
| 27 |
if( method_exists($this->obj, $method) OR $this->h->exists($hook) OR $this->h->exists($hookAfter) OR $this->h->exists($hookBefore) ){ |
| 28 |
// before hook, may alter args or stop execution |
| 29 |
$args = $this->h->apply( $hookBefore, $args ); |
| 30 |
if( $args === FALSE ){ |
| 31 |
// echo "ESCAPE EXECUTION!"; |
| 32 |
// exit; |
| 33 |
return; |
| 34 |
// return $return; |
| 35 |
} |
| 36 |
|
| 37 |
// own object method |
| 38 |
$return = NULL; |
| 39 |
if( method_exists($this->obj, $method) ){ |
| 40 |
$return = call_user_func_array( |
| 41 |
array($this->obj, $method), $args |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
// just listen |
| 46 |
$this->h->apply( $hook, $args ); |
| 47 |
|
| 48 |
// after hook, may alter return value |
| 49 |
$return = $this->h->apply( $hookAfter, $return, $args ); |
| 50 |
|
| 51 |
if( $return === $this->obj ){ |
| 52 |
return $this; |
| 53 |
} |
| 54 |
else { |
| 55 |
return $return; |
| 56 |
} |
| 57 |
} |
| 58 |
else { |
| 59 |
echo 'Undefined method - ' . get_class($this->obj) . '::' . $method; |
| 60 |
exit; |
| 61 |
} |
| 62 |
} |
| 63 |
} |