autoload.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * PSR-4 autoloader implementation for the MaxMind\DB namespace.
  5. * First we define the 'mmdb_autoload' function, and then we register
  6. * it with 'spl_autoload_register' so that PHP knows to use it.
  7. *
  8. * @param mixed $class
  9. */
  10. /**
  11. * Automatically include the file that defines <code>class</code>.
  12. *
  13. * @param string $class
  14. * the name of the class to load
  15. */
  16. function mmdb_autoload($class): void
  17. {
  18. /*
  19. * A project-specific mapping between the namespaces and where
  20. * they're located. By convention, we include the trailing
  21. * slashes. The one-element array here simply makes things easy
  22. * to extend in the future if (for example) the test classes
  23. * begin to use one another.
  24. */
  25. $namespace_map = ['MaxMind\\Db\\' => __DIR__ . '/src/MaxMind/Db/'];
  26. foreach ($namespace_map as $prefix => $dir) {
  27. // First swap out the namespace prefix with a directory...
  28. $path = str_replace($prefix, $dir, $class);
  29. // replace the namespace separator with a directory separator...
  30. $path = str_replace('\\', '/', $path);
  31. // and finally, add the PHP file extension to the result.
  32. $path .= '.php';
  33. // $path should now contain the path to a PHP file defining $class
  34. if (file_exists($path)) {
  35. include $path;
  36. }
  37. }
  38. }
  39. spl_autoload_register('mmdb_autoload');