Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / IO / Includer.php
blob8ac4af41f2f3df6adb63c4cde77a303fc1ebfeed
1 <?php
2 /** @package verysimple::IO */
4 /**
5 * import supporting libraries
6 */
7 require_once("IncludeException.php");
9 /**
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
17 * @version 1.0
19 class Includer
22 /**
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
27 * @param
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
35 // *
36 set_error_handler(array (
37 "Includer",
38 "IncludeException"
39 ), E_WARNING);
40 include_once($path);
41 restore_error_handler();
42 // */
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);
48 /**
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"
55 * @param
56 * string name of class (ex Phreeze)
57 * @param
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)) {
63 return true;
66 // normalize this as an array
67 $classpaths = is_array($classpath) ? $classpath : array (
68 $classpath
70 $attempts = "";
72 foreach ($classpaths as $path) {
73 if (class_exists($classname)) {
74 break;
77 try {
78 // append a directory separater if necessary
79 if ($path && substr($path, - 1) != "/") {
80 $path .= "/";
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);
95 /**
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) {
102 return;
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);