| 1 |
<?php |
| 2 |
namespace MBSocial; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
|
| 5 |
/// ssst. Plugin communicating |
| 6 |
class whistle |
| 7 |
{ |
| 8 |
|
| 9 |
protected $listeners = array(); |
| 10 |
protected $told = array(); |
| 11 |
protected $asked = array(); |
| 12 |
protected $offers = array(); |
| 13 |
protected $offer_order = array(); |
| 14 |
|
| 15 |
protected $log_active = false; |
| 16 |
//protected $timer_active = false; |
| 17 |
//protected $time_start = 0; |
| 18 |
|
| 19 |
public static function getInstance() |
| 20 |
{ |
| 21 |
return new whistle(); |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
public function __construct() |
| 26 |
{ |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
public function tell($msg, $args = array(), $priority='') |
| 31 |
{ |
| 32 |
|
| 33 |
|
| 34 |
$this->told[$msg][] = $args; |
| 35 |
$this->checkListeners($msg, "tell"); |
| 36 |
//$this->timer($msg, array('tell') ); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
// if there is only one good answer |
| 41 |
public function ask($msg, $respond = null) |
| 42 |
{ |
| 43 |
$return = null; |
| 44 |
//$this->timer($msg, array('ask') ); |
| 45 |
if (isset($this->told[$msg])) |
| 46 |
{ |
| 47 |
$response = end($this->told[$msg]); |
| 48 |
|
| 49 |
if (is_null($respond)) |
| 50 |
$return = $response; |
| 51 |
elseif (is_callable($respond)) |
| 52 |
{ |
| 53 |
$return = call_user_func($respond, $response); |
| 54 |
} |
| 55 |
} |
| 56 |
elseif (! is_null($respond)) // if not now, and there is a respond callback, tell them later. |
| 57 |
{ |
| 58 |
$this->listen($msg, $respond, 'tell'); |
| 59 |
} |
| 60 |
|
| 61 |
$checked = $this->checkListeners($msg, "ask"); |
| 62 |
if (! is_null($checked)) // if the listener has a better answer. |
| 63 |
$return = $checked; |
| 64 |
|
| 65 |
return $return; |
| 66 |
} |
| 67 |
|
| 68 |
// direction: if to listen to somebody telling something, or asking something. |
| 69 |
public function listen($msg, $callback, $direction) |
| 70 |
{ |
| 71 |
$this->listeners[$direction][$msg][] = $callback; // wtf! |
| 72 |
} |
| 73 |
|
| 74 |
// Callback structure for more complicated data. |
| 75 |
public function offer($msg, $callback, $args = array(), $priority = 10) |
| 76 |
{ |
| 77 |
|
| 78 |
if ( isset($this->offers[$msg]) ) |
| 79 |
$count = count($this->offers[$msg]); |
| 80 |
else |
| 81 |
$count = 0; |
| 82 |
|
| 83 |
$this->offer_order[$msg][ $count ] = $priority; |
| 84 |
$this->offers[$msg][] = array('callback' => $callback, |
| 85 |
'args' => $args); |
| 86 |
|
| 87 |
$this->checkListeners($msg, 'ask'); |
| 88 |
} |
| 89 |
|
| 90 |
// Check if there are any offers |
| 91 |
public function hasOffer($msg) |
| 92 |
{ |
| 93 |
if (isset($this->offers[$msg]) && $this->offers[$msg] > 0) |
| 94 |
{ |
| 95 |
return true; |
| 96 |
} |
| 97 |
else |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
public function collect($msg, $collect_args = array() ) |
| 102 |
{ |
| 103 |
$results = array(); |
| 104 |
$this->checkListeners($msg, 'tell'); |
| 105 |
|
| 106 |
if (isset($this->offers[$msg])) |
| 107 |
{ |
| 108 |
$offers = $this->offers[$msg]; |
| 109 |
|
| 110 |
$offer_order = $this->offer_order[$msg]; |
| 111 |
|
| 112 |
asort($offer_order, SORT_NUMERIC); // sort by priority |
| 113 |
|
| 114 |
if (count ($offer_order) == count($offers) ) |
| 115 |
{ |
| 116 |
$new_offers = array(); |
| 117 |
foreach($offer_order as $index => $prio) |
| 118 |
{ |
| 119 |
$new_offers[] = $offers[$index]; |
| 120 |
} |
| 121 |
$offers = $new_offers; |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
//MI()->errors()->add( new \Exception('Whistle collect - Offer order and Offers are of different size') ); |
| 126 |
} |
| 127 |
|
| 128 |
foreach($offers as $offer) |
| 129 |
{ |
| 130 |
$call = $offer['callback']; |
| 131 |
$args = $offer['args']; // perhaps this is useless, of this should be returned if no callback is given? |
| 132 |
|
| 133 |
if(is_callable($call) ) |
| 134 |
$results[] = call_user_func($call, $args, $collect_args); |
| 135 |
else |
| 136 |
{ |
| 137 |
//MI()->errors()->add( new \Exception('Offer ' . $msg . ' has an invalid callback') ); |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
$this->checkListeners($msg . '_done', 'tell'); // our option to plug after. |
| 144 |
return $results; |
| 145 |
} |
| 146 |
|
| 147 |
protected function checkListeners($msg, $direction) |
| 148 |
{ |
| 149 |
if (isset($this->listeners[$direction][$msg])) |
| 150 |
{ |
| 151 |
$response = null; |
| 152 |
|
| 153 |
foreach($this->listeners[$direction][$msg] as $listener) |
| 154 |
{ |
| 155 |
if (isset($this->told[$msg])) |
| 156 |
{ |
| 157 |
$response = end($this->told[$msg]); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
if(is_callable($listener) ) |
| 162 |
{ |
| 163 |
$response = call_user_func( $listener, $response); |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
//MI()->errors()->add( new \Exception('Listener is not a callback') ); |
| 168 |
} |
| 169 |
} |
| 170 |
return $response; // this will return only the last response, possibly also not great. |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** Send message to log file |
| 175 |
* |
| 176 |
* Function will tell log function to record statement |
| 177 |
* @param @msg String The message to log |
| 178 |
* @param @args Array Extra variables to show in log file |
| 179 |
*/ |
| 180 |
|
| 181 |
public function log($msg, $args) |
| 182 |
{ |
| 183 |
$logger = $this->ask('system/logger'); |
| 184 |
try { |
| 185 |
$logger->debug($msg . ":" . var_export($args, true) ); |
| 186 |
} |
| 187 |
catch (Exception $e) |
| 188 |
{ |
| 189 |
$logger->error('Logger ran into trouble: ', $e); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
} // class |
| 194 |
|