function before($route = array()) { #print_r($route); exit; #inspect the $route array, looking at various options that may have been passed in if (@$route['options']['authenticate']) { authenticate_user() or halt("Access denied"); } if (@$route['options']['validation_function']) { call_if_exists($route['options']['validation_function'], params()) or halt("Woops! Params did not pass validation"); } }
function test_main_call_if_exists() { assert_empty(call_if_exists("unknown_function")); assert_equal(call_if_exists("count", array(1, 2, 3)), 3); assert_length_of(call_if_exists("array_merge", array(1, 2, 3), array(4, 5, 6)), 6); class TestCallIfExists { public function test($value = 1) { return $value * 10; } public static function testStatic($value = 1) { return $value * 20; } } $obj = new TestCallIfExists(); assert_equal(call_if_exists(array($obj, 'test'), 3), 30); assert_equal(call_if_exists(array('TestCallIfExists', 'testStatic'), 3), 60); assert_equal(call_if_exists('TestCallIfExists::testStatic', 3), 60); }
/** * Internal error handler dispatcher * Find and call matching error handler and exit * If no match found, call default error handler * * @access private * @param int $errno * @param string $errstr * @param string $errfile * @param string $errline * @return void */ function error_handler_dispatcher($errno, $errstr, $errfile, $errline) { $back_trace = debug_backtrace(); while ($trace = array_shift($back_trace)) { if ($trace['function'] == 'halt') { $errfile = $trace['file']; $errline = $trace['line']; break; } } # Notices and warning won't halt execution if (error_wont_halt_app($errno)) { error_notice($errno, $errstr, $errfile, $errline); return; } else { # Other errors will stop application $handlers = error(); $is_http_err = http_response_status_is_valid($errno); foreach ($handlers as $handler) { $e = is_array($handler['errno']) ? $handler['errno'] : array($handler['errno']); while ($ee = array_shift($e)) { if ($ee == $errno || $ee == E_LIM_PHP || $ee == E_LIM_HTTP && $is_http_err) { echo call_if_exists($handler['function'], $errno, $errstr, $errfile, $errline); exit; } } } echo error_default_handler($errno, $errstr, $errfile, $errline); stop_and_exit(); } }
/** * Call before_sending_header() if it exists, then send headers * * @param string $header * @return void */ function send_header($header = null, $replace = true, $code = false) { if (!headers_sent()) { call_if_exists('before_sending_header', $header); header($header, $replace, $code); } }
/** * Internal error handler dispatcher * Find and call matching error handler and exit * If no match found, call default error handler * * @access private * @param int $errno * @param string $errstr * @param string $errfile * @param string $errline * @return void */ function error_handler_dispatcher($errno, $errstr, $errfile, $errline) { $back_trace = debug_backtrace(); while ($trace = array_shift($back_trace)) { if ($trace['function'] == 'halt') { $errfile = $trace['file']; $errline = $trace['line']; break; } } $handlers = error(); $is_http_err = http_response_status_is_valid($errno); foreach ($handlers as $handler) { $e = is_array($handler['errno']) ? $handler['errno'] : array($handler['errno']); while ($ee = array_shift($e)) { if ($ee == $errno || $ee == E_LIM_PHP || $ee == E_LIM_HTTP && $is_http_err) { echo call_if_exists($handler['function'], $errno, $errstr, $errfile, $errline); exit; } } } echo error_default_handler($errno, $errstr, $errfile, $errline); exit; }
function test_main_call_if_exists() { assert_empty(call_if_exists("unknown_function")); assert_equal(call_if_exists("count", array(1, 2, 3)), 3); assert_length_of(call_if_exists("array_merge", array(1, 2, 3), array(4, 5, 6)), 6); }