/** * Verify that $callback is a valid function callback * * This is used to be absolutely sure a callback is valid before registering * it, to avoid later errors on the throwing of an error * @param string|array * @return true|Error_Raise_Error * @throws ERROR_UTIL_ERROR_FUNCTION_DOESNT_EXIST If the callback is a * string and isn't the name of any function * @throws ERROR_UTIL_ERROR_INTERNAL_FUNCTION If the callback is the name * of an internal, pre-defined function like "function_exists" * @throws ERROR_UTIL_ERROR_INVALID_INPUT If the callback is neither * a string, an array(classname, method), or an array(object, method) * @throws ERROR_UTIL_ERROR_METHOD_DOESNT_EXIST if the callback is an * array, and the method is not a method of the class * @access private */ function _validCallback($callback) { static $init = false; if (!$init) { $init = true; Error_Raise::setErrorMsgGenerator('Error_Util', array('Error_Util', 'genErrorMessage')); } if (is_string($callback)) { if (!function_exists($callback)) { return Error_Raise::exception('Error_Util', ERROR_UTIL_ERROR_FUNCTION_DOESNT_EXIST, array('function' => $callback)); } $a = get_defined_functions(); if (in_array($callback, $a['internal'])) { return Error_Raise::exception('Error_Util', ERROR_UTIL_ERROR_INTERNAL_FUNCTION, array('function' => $callback)); } return true; } if (is_array($callback)) { if (!isset($callback[0]) || !isset($callback[1])) { return Error_Raise::exception('Error_Util', ERROR_UTIL_ERROR_INVALID_INPUT, array('expected' => array(0, 1), 'was' => array_keys($callback), 'var' => 'array_keys($callback)', 'paramnum' => 1)); } if (is_string($callback[0])) { if (!is_string($callback[1])) { return Error_Raise::exception('Error_Util', ERROR_UTIL_ERROR_INVALID_INPUT, array('expected' => 'string', 'was' => gettype($callback[1]), 'var' => '$callback[1]', 'paramnum' => 1)); } if (!class_exists($callback[0])) { return Error_Raise::exception('Error_Util', ERROR_UTIL_ERROR_CLASS_DOESNT_EXIST, array('class' => $callback[0])); } if (!in_array(strtolower($callback[1]), get_class_methods($callback[0]))) { return Error_Raise::exception('Error_Util', ERROR_UTIL_ERROR_METHOD_DOESNT_EXIST, array('method' => $callback[1], 'class' => $callback[0])); } return true; } elseif (is_object($callback[0])) { if (!method_exists($callback[0], $callback[1])) { return Error_Raise::exception('Error_Util', ERROR_UTIL_ERROR_METHOD_DOESNT_EXIST, array('method' => $callback[1], 'class' => get_class($callback[0]))); } return true; } else { return Error_Raise::exception('Error_Util', ERROR_UTIL_ERROR_INVALID_INPUT, array('expected' => array('array', 'string'), 'was' => gettype($callback[0]), 'var' => '$callback[0]', 'paramnum' => 1)); } // is a callback method return true; } return Error_Raise::exception('error_util', ERROR_UTIL_ERROR_INVALID_INPUT, array('expected' => array('array', 'string'), 'was' => gettype($callback), 'var' => '$callback', 'paramnum' => 1)); }
/** * Initialize error raising for a package * * Use this method to assign an error message handler to a package. Note: * This is an alias to {@link set ErrorMsgGenerator()} * * As an example, here is a simple Error_Raise usage: * * <code> * <?php * // define error constants for package foo * define("FOO_ERROR_NOT_NUMBER", 1); * define("FOO_ERROR_NOT_STRING", 2); * * /** * * Error message generator for package Foo * {@*} * function getFooMsg($code, $args, $state) * { * $msgs = array( * FOO_ERROR_NOT_NUMBER => 'parameter %p% is a %type%, not a number', * FOO_ERROR_NOT_STRING => 'parameter %p% is a %type%, not a string', * ); * if (isset($msgs[$code])) { * $message = $msgs[$code]; * } else { * $message = 'Code ' . $code . ' is not a valid error code'; * } * return Error_Raise::sprintfErrorMessageWithState( * $message, $args, $state); * } * * // initialize error creation * Error_Raise::initialize('foo', 'getFooMsg'); * // that's it! Now you can do this any of these: * Error_Raise::warning('foo', FOO_ERROR_NOT_STRING, * array('p' => 'bar', 'type' => 'object')); * Error_Raise::notice('foo', FOO_ERROR_NOT_STRING, * array('p' => 'bar', 'type' => 'object')); * Error_Raise::exception('foo', FOO_ERROR_NOT_NUMBER, * array('p' => 'bar', 'type' => 'object')); * $e = Error_Raise::error('foo', FOO_ERROR_NOT_NUMBER, * array('p' => 'bar', 'type' => 'object')); * echo $e->getMessage(); * // displays 'foo error : parameter bar is a object, not a number' * echo $e->getMessage(ERROR_RAISE_HTML); * // displays '<strong>foo error :</strong> parameter <strong>bar</strong> * // is a <strong>object</strong>, not a number * ?> * </code> * @param string package name * @param array|string call_user_func_array-compatible function/method * * This function name can be either be a * global function (string), static class method * array(classname, method), or object method * array(&$obj, method). Be sure to assign by * reference in PHP 4 * @static */ function initialize($package, $errorMsgGenerator) { return Error_Raise::setErrorMsgGenerator($package, $errorMsgGenerator); }