Skip to content

Instantly share code, notes, and snippets.

@allada
Created January 31, 2013 19:34
Show Gist options
  • Save allada/4685694 to your computer and use it in GitHub Desktop.
Save allada/4685694 to your computer and use it in GitHub Desktop.
<?php
abstract class Model{
public static function invokeHook($hookName, $context, $caller, array $args){
// This example talks through traits and executes hook functions
foreach((new ReflectionClass(get_called_class()))->getTraits() as $trait){
foreach($trait->getMethods() as $method){
if($method->class . '_' . $hookName == $method->name){
// Found something to hook onto execute it in context
if(($return = $method->invokeArgs($context, $args)) !== null){
return $return;
}
// This is how it needs to be done now
/*if($method->class != $caller){ // This keeps recursion problems from happening.
if(is_object($context) || $context === null){
if(($return = $method->invokeArgs($context, $args)) !== null){
return $return;
}
}else{
// This part is really slow because it has to create a new closure to invoke it.
$closure = $method->getClosure()->bindTo(null, $context);
if(($return = call_user_func_array($closure, $args)) !== null){
return $return;
}
}
}*/
}
}
}
}
public static function create(array $data){
if(($ret = static::invokeHook('before_create', get_called_class(), __CLASS__, array(&$data))) !== null){
return $ret;
}
// Do some stuff to actually create the record in the database and assign the inserted ID to $insert_id
if(($ret = static::invokeHook('after_create', get_called_class(), __CLASS__, array(&$data, &$insert_id))) !== null){
return $ret;
}
return static::getById($insert_id);
}
public function update($someParam){
if(($ret = static::invokeHook('before_update', $this, __CLASS__, array(&$someParam))) !== null){
return $ret;
}
// Do some stuff to actually update the record in the database
if(($ret = static::invokeHook('after_update', $this, __CLASS__, array(&$someParam))) !== null){
return $ret;
}
return true;
}
}
trait Check_Permissions{
public static function Check_Permissions_before_create(array &$data){
global $logged_in_user;
if(!$logged_in_user->check_permission('create', get_called_class())){
// Returning false haults the record creation
return false;
}
// By returning null or returning nothing (null) it does not hault the creation of records
}
public function Check_Permissions_before_update(&$someParam){
global $logged_in_user;
if(!$logged_in_user->check_permission('update', get_called_class())){
// Returning false haults the record creation
return false;
}
// By returning null or returning nothing (null) it does not hault the creation of records
}
}
trait Log_Changes{
public static function Log_Changes_after_create(array &$data, &$insert_id){
global $logged_in_user;
$logged_in_user->log_change('insert', $insert_id, get_called_class());
}
public function Log_Changes_after_update($someParam){
global $logged_in_user;
$logged_in_user->log_change('update', $this->id, get_called_class());
}
}
class Order extends Model{
use Check_Permissions,
Log_Changes;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment