A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.2 KiB

  1. <?php
  2. namespace SparkLib;
  3. define('SLASH', DIRECTORY_SEPARATOR);
  4. /**
  5. * A simple but configurable autoloader. Expects everything to
  6. * live under the directory specified by LIBDIR, with most classes
  7. * under LIBDIR/classes/.
  8. *
  9. * <code>
  10. * define('LIBDIR', '/path/to/your/code/lib/');
  11. * require LIBDIR . 'classes/SparkLib/Autoloader.php';
  12. * </code>
  13. */
  14. class Autoloader {
  15. /**
  16. * Set this to declare explicit include paths, relative to
  17. * AUTOLOADER_INCLUDE_ROOT, for things that don't conform to
  18. * the classes/ClassName.php pattern. For example:
  19. *
  20. * <code>
  21. * \SparkLib\Autoloader::$classPath = array(
  22. * 'CupsPrintIPP' => 'phpprintipp/php_classes/CupsPrintIPP.php',
  23. * 'SphinxClient' => 'classes/sphinxapi.php',
  24. * );
  25. * </code>
  26. */
  27. public static $classPath = array();
  28. /**
  29. * Set this to declare special directories for classes containing
  30. * a given substring.
  31. *
  32. * <code>
  33. * \SparkLib\Autoloader::$searchPaths = array(
  34. * 'Saurus' => 'classes/dinosaurs/',
  35. * );
  36. * </code>
  37. */
  38. public static $searchPaths = array();
  39. /**
  40. * Set this to define any extra files which should be included
  41. * if we fail to find a class.
  42. */
  43. public static $extraAutoloaders = array();
  44. /**
  45. * Install this puppy as an autoloader, do some basic configuration.
  46. */
  47. public static function setup ()
  48. {
  49. // TODO
  50. // Get this in full conformance with:
  51. // http://groups.google.com/group/php-standards/web/psr-0-final-proposal?pli=1
  52. // ...and see about just setting include_path() instead of knowing an absolute
  53. // path to things.
  54. // If LIBDIR isn't defined, assume that this file lives in a path like
  55. // /somepath/lib/classes/SparkLib/, where we want somepath/lib/ to be
  56. // our include root...
  57. if (constant('LIBDIR')) {
  58. define('AUTOLOADER_INCLUDE_ROOT', LIBDIR);
  59. } else {
  60. define(
  61. 'AUTOLOADER_INCLUDE_ROOT',
  62. realpath(dirname(__FILE__) . SLASH . '..' . SLASH . '..' . SLASH)
  63. );
  64. }
  65. // Register autoloaders
  66. spl_autoload_register(array('\SparkLib\Autoloader', 'load'));
  67. }
  68. /**
  69. * Do the actual business of autoloading.
  70. *
  71. * @param string name of class
  72. */
  73. public static function load ($class)
  74. {
  75. if (isset(self::$classPath[$class])) {
  76. $path = self::$classPath[$class];
  77. }
  78. else {
  79. // TODO: All these strpos() calls are probably expensive.
  80. foreach (static::$searchPaths as $substr => $dir) {
  81. // we first check for a \, which indicates a namespace
  82. // - those we want to skip
  83. if ((false === strpos($class, '\\')) && (false !== strpos($class, $substr))) {
  84. $path = $dir . $class . '.php';
  85. break;
  86. }
  87. }
  88. }
  89. // If we got here, try for a corresponding file in classes
  90. if (! isset($path)) {
  91. $class = str_replace('\\', SLASH, $class);
  92. $path = 'classes' . SLASH . $class . '.php';
  93. }
  94. $full_path = AUTOLOADER_INCLUDE_ROOT . SLASH . $path;
  95. if (is_file($full_path)) {
  96. include $full_path;
  97. } else {
  98. // If we've got any fallback autoloaders, pull 'em in so
  99. // they can try to find the class in question
  100. foreach (static::$extraAutoloaders as $autoloader_path) {
  101. include_once($autoloader_path);
  102. }
  103. }
  104. }
  105. }