Skip to content

Instantly share code, notes, and snippets.

@GDmac
Created December 31, 2013 14:50
Show Gist options
  • Save GDmac/8197902 to your computer and use it in GitHub Desktop.
Save GDmac/8197902 to your computer and use it in GitHub Desktop.
EE Monkee_patch
<?php
$plugin_info = array(
'pi_name' => 'example_plugin',
'pi_version' => '0.1',
'pi_author' => 'John Doe',
'pi_author_url' => '',
'pi_description' => '',
'pi_usage' => '',
);
// ===========================================================
class example_plugin
{
private $db;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function example_function()
{
// store original db class
$this->db = ee()->db;
// method overrides
$overrides = array(
'query'=>'_query_override',
);
// override db class
ee()->db = new monkee_patch($this->db, $this, $overrides);
// ... do stuff that calls the db class
// ... the override query example just var_dumps the query
// restore db class
ee()->db = $this->db;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function _query_override($query)
{
var_dump($query);
// call original method
return $this->db->query($query);
}
}
// ===========================================================
// This allows to override functions from for example ee()->db
// Don't do this at home, here be dragons
class monkee_patch {
private $monkee;
private $monkee_caller;
private $monkee_methods;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function __construct(&$original, &$caller, $methods)
{
// swap
$this->monkee = $original;
$this->monkee_caller = $caller;
$this->monkee_methods = $methods;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function __call($method, $args)
{
if (array_key_exists($method, $this->monkee_methods))
{
return call_user_func_array(array($this->monkee_caller, $this->monkee_methods[$method]), $args);
}
else
{
return call_user_func_array(array($this->monkee, $method), $args);
}
}
}
// end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment