Skip to content

Instantly share code, notes, and snippets.

@kjantzer
Last active October 13, 2015 16:58
Show Gist options
  • Save kjantzer/4227244 to your computer and use it in GitHub Desktop.
Save kjantzer/4227244 to your computer and use it in GitHub Desktop.
Slim Class static method init
/*
Catch-all Static methods when they are not explicitly defined
if the method name is prefixed with "slim_", we will instansiate the called class
and attempt to call the method name with the prefix stripped off
ex: "slim_transcode_task" becomes "transcode_task"
it is designed to be used with the SLIM api
ex: $app->get('/book/:book_id/baker/trascode', 'Baker::slim_transcode_task');
add $construct_args variable to determine how many arguments are used to construct the class
ex: static protected $construct_args = 1;
*/
public static function __callStatic($method_name, $arguments){
global $app;
// is the method name prefixed with "slim_" ?
preg_match("/^slim_(.*)/", $method_name, $matches);
if( isset($matches[1]) ){
$method_name = $matches[1];
// get parameters from the url route
$route_params = $app->router()->getCurrentRoute()->getParams();
// note: $arguents and $route_params contain the same data, but the former is a regular array
// while the later is an associative array. We use the reg array for calling the method, but we need the
// associative array to check/convert book_id (bsa_id to auto_id)
$class_name = get_called_class();
// look for "$construct_args" which tells us how many of the url parameters should be used to init the class
if( isset($class_name::$construct_args) ){
// get the arguments for constructing the class
$args = array_splice($route_params, 0, $class_name::$construct_args);
array_splice($arguments, 0, $class_name::$construct_args); // also splice these from arguments so the method doesn't get them
// no arguments? just start a new class
if(count($args) == 0)
$that = new $class_name;
// else, use reflection class to create in instance with arguments (http://stackoverflow.com/a/1858400/484780)
else {
// if "book_id" is a route parameter, lets perform a check
if( isset($args['book_id']) )
checkBookID($args); // checks id (translates bsa_id to auto_id)
// instantiate the class
$r = new ReflectionClass($class_name);
$that = $r->newInstanceArgs($args);
}
// this class doens't need any arguments to be instanitated
}else{
$that = new $class_name(); // instanitate the class
}
// see if the method name exists on this class, then call it
if( method_exists($that, $method_name) ){
if($method_name === 'find' && empty($arguments))
$arguments[0] = array();
if(($method_name === 'add' || $method_name === 'update') && empty($arguments))
$arguments[0] = _data();
$result = call_user_func_array(array($that, $method_name), $arguments);
if( $result !== null && $result !== '')
_return( $result );
}else{
error_log('!!! The method “'.$method_name.'” does not exist on “'.$class_name.'”');
}
}else{
error_log('!!! The method “'.$method_name.'” does not exist.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment