2 /** @package verysimple::IO */
5 * import supporting libraries
7 require_once("IncludeException.php");
10 * Provides helper functions for including classes and files dynamically
11 * so that Exceptions are thrown instead of PHP errors and warnings
13 * @package verysimple::IO
14 * @author Jason Hinkle
15 * @copyright 1997-2008 VerySimple, Inc.
16 * @license http://www.gnu.org/licenses/lgpl.html LGPL
23 * Includes a file with the given path.
24 * If PHP is unable to include the file,
25 * an IncludeException is thrown instead of a PHP warning
28 * string path to file passed to the include_once statement
30 public static function IncludeFile($path)
32 // re-route error handling temporarily so we can catch errors
33 // use include instead of require so we can catch runtime exceptions
34 // reset error handling back to whatever it was
36 set_error_handler(array (
41 restore_error_handler();
44 // this doesn't work but it seems like it should
45 // if (@include_once($path) === false) throw new IncludeException("Unable to include file: " . $path);
49 * Ensures that a class is defined.
50 * If not, attempts to include the file
51 * using the provided path. If unable to locate the class, an IncludeException
52 * will be thrown. The path that will be used for include_once is
53 * $classpath . "/" . $classname . ".php"
56 * string name of class (ex Phreeze)
58 * string or array [optional] the relative path(s) where the file would be found
60 public static function RequireClass($classname, $classpath = "")
62 if (class_exists($classname)) {
66 // normalize this as an array
67 $classpaths = is_array($classpath) ?
$classpath : array (
72 foreach ($classpaths as $path) {
73 if (class_exists($classname)) {
78 // append a directory separater if necessary
79 if ($path && substr($path, - 1) != "/") {
83 Includer
::IncludeFile($path . $classname . ".php");
84 } catch (IncludeException
$ex) {
85 $attempts .= " " . $ex->getMessage();
89 if (! class_exists($classname)) {
90 // the class still isn't defined so there was a problem including the model
91 throw new IncludeException("Unable to locate class '$classname': " . $attempts);
96 * Handler for catching file-not-found errors and throwing an IncludeException
98 public static function IncludeException($code, $string, $file, $line, $context)
100 // check for repressed errors
101 if (error_reporting() == 0) {
105 $tmp1 = explode(")", $string);
106 $tmp2 = explode("(", $tmp1 [0]);
107 $mfile = isset($tmp2 [1]) ?
$tmp2 [1] : "";
109 $msg = "Error $code: " . ($mfile ?
"Unable to include file: '" . $mfile . "'" : $string);
111 throw new IncludeException($msg, $code);