KyleMassacre Posted April 21, 2021 Posted April 21, 2021 Hello everyone, I forked over a basic hook system that I generally use in my Laravel projects to make it I guess "Framework Agnostic" and figured that some other people on here may find it useful. Its very easy to use, just require the file somewhere in a global file ie global_func.php if you're a MCC user and just instantiate the class require('Hook.php') $hook = Hook::getInstance(); Then you can use the get method anywhere that you would like your hook ran. For example, in index.php: $hooks->get('index_loaded', [$ir['userid']], function ($user) { return $user; }, 'Hello World'); Then somewhere in an autoloaded file you can listen for the hook /** * In this example we are listening for the 'index_loaded' hook, * if found, will replace 'Hello World' with $ir['userid'] and * print it to the screen */ $hooks->listen('index_loaded', function ($callback, $output, $user) { if(empty($output)) { $output = $user; } echo $output; },1); Hook.php <?php /** * Class Hook * Forked from https://github.com/esemve/Hook * @license MIT * @copyright 2016 Bence Kádár */ class Hook { protected $watch = []; protected $stop = []; private static $instance = null; public static function getInstance() { if (self::$instance == null) { self::$instance = new Hook; } return self::$instance; } /** * Return the hook answer. * * @param string $hook Hook name * @param array $params * @param callable|null $callback * @param string $htmlContent content wrapped by hook * * @return null|void */ public function get(string $hook, $params = [], callable $callback = null, $htmlContent = '') { return $this->run($hook, $params, $callback, $htmlContent); } /** * Stop all another hook running. * * @param string $hook Hook name */ public function stop(string $hook) { $this->stop[$hook] = true; } /** * Subscribe to hook. * * @param string $hook Hook name * @param $priority * @param $function */ public function listen(string $hook, $function, $priority = null) { $caller = debug_backtrace(null, 3)[0]; if (empty($this->watch[$hook])) { $this->watch[$hook] = []; } if (!is_numeric($priority)) { $priority = null; } $this->watch[$hook][$priority] = [ 'function' => $function, 'caller' => [ 'file' => $caller['file'], 'line' => $caller['line'], 'class' => $caller['class'], ], ]; ksort($this->watch[$hook]); } /** * Return all registered hooks. * * @return array */ public function getHooks() { $hookNames = (array_keys($this->watch)); ksort($hookNames); return $hookNames; } /** * Return all listeners for hook. * * @param string $hook * * @return array */ public function getEvents($hook) { $output = []; foreach ($this->watch[$hook] as $key => $value) { $output[$key] = $value['caller']; } return $output; } /** * Run hook events. * * @param string $hook Hook name * @param array $params Parameters * @param $callback * @param null $output html wrapped by hook * * @return mixed */ protected function run(string $hook, array $params, $callback, $output = null) { array_unshift($params, $output); array_unshift($params, $callback); if (array_key_exists($hook, $this->watch)) { if (is_array($this->watch[$hook])) { foreach ($this->watch[$hook] as $function) { if (!empty($this->stop[$hook])) { unset($this->stop[$hook]); break; } $output = call_user_func_array($function['function'], $params); $params[1] = $output; } } } return $output; } /** * Return the listeners. * * @return array */ public function getListeners() { return $this->watch; } } 2 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.