Autoloader.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Matrix;
  3. /**
  4. *
  5. * Autoloader for Matrix classes
  6. *
  7. * @package Matrix
  8. * @copyright Copyright (c) 2014 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
  9. * @license https://opensource.org/licenses/MIT MIT
  10. */
  11. class Autoloader
  12. {
  13. /**
  14. * Register the Autoloader with SPL
  15. *
  16. */
  17. public static function Register()
  18. {
  19. if (function_exists('__autoload')) {
  20. // Register any existing autoloader function with SPL, so we don't get any clashes
  21. spl_autoload_register('__autoload');
  22. }
  23. // Register ourselves with SPL
  24. return spl_autoload_register(['Matrix\\Autoloader', 'Load']);
  25. }
  26. /**
  27. * Autoload a class identified by name
  28. *
  29. * @param string $pClassName Name of the object to load
  30. */
  31. public static function Load($pClassName)
  32. {
  33. if ((class_exists($pClassName, false)) || (strpos($pClassName, 'Matrix\\') !== 0)) {
  34. // Either already loaded, or not a Matrix class request
  35. return false;
  36. }
  37. $pClassFilePath = __DIR__ . DIRECTORY_SEPARATOR .
  38. 'src' . DIRECTORY_SEPARATOR .
  39. str_replace(['Matrix\\', '\\'], ['', '/'], $pClassName) .
  40. '.php';
  41. if ((file_exists($pClassFilePath) === false) || (is_readable($pClassFilePath) === false)) {
  42. // Can't load
  43. return false;
  44. }
  45. require($pClassFilePath);
  46. }
  47. }