Autoloader.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * This file is part of GameQ.
  4. *
  5. * GameQ is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GameQ is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. *
  19. */
  20. /**
  21. * A simple PSR-4 spec auto loader to allow GameQ to function the same as if it were loaded via Composer
  22. *
  23. * To use this just include this file in your script and the GameQ namespace will be made available
  24. *
  25. * i.e. require_once('/path/to/src/GameQ/Autoloader.php');
  26. *
  27. * See: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
  28. *
  29. * @codeCoverageIgnore
  30. */
  31. spl_autoload_register(function ($class) {
  32. // project-specific namespace prefix
  33. $prefix = 'GameQ\\';
  34. // base directory for the namespace prefix
  35. $base_dir = __DIR__ . DIRECTORY_SEPARATOR;
  36. // does the class use the namespace prefix?
  37. $len = strlen($prefix);
  38. if (strncmp($prefix, $class, $len) !== 0) {
  39. // no, move to the next registered autoloader
  40. return;
  41. }
  42. // get the relative class name
  43. $relative_class = substr($class, $len);
  44. // replace the namespace prefix with the base directory, replace namespace
  45. // separators with directory separators in the relative class name, append
  46. // with .php
  47. $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
  48. // if the file exists, require it
  49. if (file_exists($file)) {
  50. require $file;
  51. }
  52. });