Includer.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /** @package verysimple::IO */
  3. /** import supporting libraries */
  4. require_once("IncludeException.php");
  5. /**
  6. * Provides helper functions for including classes and files dynamically
  7. * so that Exceptions are thrown instead of PHP errors and warnings
  8. *
  9. * @package verysimple::IO
  10. * @author Jason Hinkle
  11. * @copyright 1997-2008 VerySimple, Inc.
  12. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  13. * @version 1.0
  14. */
  15. class Includer
  16. {
  17. /**
  18. * Includes a file with the given path. If PHP is unable to include the file,
  19. * an IncludeException is thrown instead of a PHP warning
  20. * @param string path to file passed to the include_once statement
  21. */
  22. public static function IncludeFile($path)
  23. {
  24. // re-route error handling temporarily so we can catch errors
  25. // use include instead of require so we can catch runtime exceptions
  26. // reset error handling back to whatever it was
  27. //*
  28. set_error_handler(array("Includer", "IncludeException"), E_WARNING);
  29. include_once($path);
  30. restore_error_handler();
  31. //*/
  32. // this doesn't work but it seems like it should
  33. // if (@include_once($path) === false) throw new IncludeException("Unable to include file: " . $path);
  34. }
  35. /**
  36. * Ensures that a class is defined. If not, attempts to include the file
  37. * using the provided path. If unable to locate the class, an IncludeException
  38. * will be thrown. The path that will be used for include_once is
  39. * $classpath . "/" . $classname . ".php"
  40. *
  41. * @param string name of class (ex Phreeze)
  42. * @param string or array [optional] the relative path(s) where the file would be found
  43. */
  44. public static function RequireClass($classname, $classpath = "")
  45. {
  46. if (class_exists($classname)) return true;
  47. // normalize this as an array
  48. $classpaths = is_array($classpath) ? $classpath : array($classpath);
  49. $attempts = "";
  50. foreach ($classpaths as $path)
  51. {
  52. if (class_exists($classname)) break;
  53. try
  54. {
  55. // append a directory separater if necessary
  56. if ($path && substr($path,-1) != "/") $path .= "/";
  57. Includer::IncludeFile($path . $classname . ".php");
  58. }
  59. catch (IncludeException $ex) {$attempts .= " " . $ex->getMessage();}
  60. }
  61. if (!class_exists($classname))
  62. {
  63. // the class still isn't defined so there was a problem including the model
  64. throw new IncludeException("Unable to locate class '$classname': " . $attempts);
  65. }
  66. }
  67. /**
  68. * Handler for catching file-not-found errors and throwing an IncludeException
  69. */
  70. public static function IncludeException($code, $string, $file, $line, $context)
  71. {
  72. // check for repressed errors
  73. if (error_reporting() == 0) return;
  74. $tmp1 = explode(")",$string);
  75. $tmp2 = explode("(",$tmp1[0]);
  76. $mfile = isset($tmp2[1]) ? $tmp2[1] : "";
  77. $msg = "Error $code: " . ($mfile ? "Unable to include file: '" . $mfile . "'" : $string);
  78. throw new IncludeException($msg,$code);
  79. }
  80. }
  81. ?>