New onsite patient portal, take 4.
[openemr.git] / portal / patient / fwk / libs / verysimple / Util / ExceptionThrower.php
blobabb34b82b5d13bcc2c62da2765dd35884f39a28d
1 <?php
2 /** @package verysimple::Util */
4 /**
5 * Utility for catching PHP errors and converting them to an exception
6 * that can be caught at runtime
8 * @example <pre>
9 * try
10 * {
11 * ExceptionThrower::Start();
12 * // @TODO PHP command here
13 * ExceptionThrower::Stop();
14 * }
15 * catch (Exception $ex)
16 * {
17 * ExceptionThrower::Stop();
18 * // handle or re-throw exception
19 * }</pre>
20 * @package verysimple::Util
21 * @author Jason Hinkle
22 * @copyright 1997-2008 VerySimple, Inc.
23 * @license http://www.gnu.org/licenses/lgpl.html LGPL
24 * @version 1.0
26 class ExceptionThrower {
27 static $IGNORE_DEPRECATED = true;
29 /**
30 * Start redirecting PHP errors
32 * @param int $level
33 * PHP Error level to catch (Default = E_ALL & ~E_DEPRECATED)
35 static function Start($level = null) {
36 if ($level == null) {
37 if (defined ( "E_DEPRECATED" )) {
38 $level = E_ALL & ~ E_DEPRECATED;
39 } else {
40 // php 5.2 and earlier don't support E_DEPRECATED
41 $level = E_ALL;
42 self::$IGNORE_DEPRECATED = true;
45 set_error_handler ( array (
46 "ExceptionThrower",
47 "HandleError"
48 ), $level );
51 /**
52 * Stop redirecting PHP errors
54 static function Stop() {
55 restore_error_handler ();
58 /**
59 * Fired by the PHP error handler function.
60 * Calling this function will
61 * always throw an exception unless error_reporting == 0. If the
62 * PHP command is called with @ preceeding it, then it will be ignored
63 * here as well.
65 * @param string $code
66 * @param string $string
67 * @param string $file
68 * @param string $line
69 * @param string $context
71 static function HandleError($code, $string, $file, $line, $context) {
72 // ignore supressed errors
73 if (error_reporting () == 0)
74 return;
75 if (self::$IGNORE_DEPRECATED && strpos ( $string, "deprecated" ) === true)
76 return true;
78 throw new Exception ( $string . " in " . basename ( $file ) . " at line $line", $code );