Autoloader.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Autoloader
  4. *
  5. * @package Less
  6. * @subpackage autoload
  7. */
  8. class Less_Autoloader {
  9. /**
  10. * Registered flag
  11. *
  12. * @var boolean
  13. */
  14. protected static $registered = false;
  15. /**
  16. * Library directory
  17. *
  18. * @var string
  19. */
  20. protected static $libDir;
  21. /**
  22. * Register the autoloader in the spl autoloader
  23. *
  24. * @return void
  25. * @throws Exception If there was an error in registration
  26. */
  27. public static function register(){
  28. if( self::$registered ){
  29. return;
  30. }
  31. self::$libDir = dirname(__FILE__);
  32. if(false === spl_autoload_register(array('Less_Autoloader', 'loadClass'))){
  33. throw new Exception('Unable to register Less_Autoloader::loadClass as an autoloading method.');
  34. }
  35. self::$registered = true;
  36. }
  37. /**
  38. * Unregisters the autoloader
  39. *
  40. * @return void
  41. */
  42. public static function unregister(){
  43. spl_autoload_unregister(array('Less_Autoloader', 'loadClass'));
  44. self::$registered = false;
  45. }
  46. /**
  47. * Loads the class
  48. *
  49. * @param string $className The class to load
  50. */
  51. public static function loadClass($className){
  52. // handle only package classes
  53. if(strpos($className, 'Less_') !== 0){
  54. return;
  55. }
  56. $className = substr($className,5);
  57. $fileName = self::$libDir . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  58. if(file_exists($fileName)){
  59. require $fileName;
  60. return true;
  61. }else{
  62. throw new Exception('file not loadable '.$fileName);
  63. }
  64. }
  65. }