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