ext
9 months ago
src
9 months ago
LICENSE
6 years ago
autoload.php
9 months ago
package.xml
9 months ago
autoload.php
48 lines
| 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 | /* |
| 22 | * A project-specific mapping between the namespaces and where |
| 23 | * they're located. By convention, we include the trailing |
| 24 | * slashes. The one-element array here simply makes things easy |
| 25 | * to extend in the future if (for example) the test classes |
| 26 | * begin to use one another. |
| 27 | */ |
| 28 | $namespace_map = ['MaxMind\Db\\' => __DIR__ . '/src/MaxMind/Db/']; |
| 29 | |
| 30 | foreach ($namespace_map as $prefix => $dir) { |
| 31 | // First swap out the namespace prefix with a directory... |
| 32 | $path = str_replace($prefix, $dir, $class); |
| 33 | |
| 34 | // replace the namespace separator with a directory separator... |
| 35 | $path = str_replace('\\', '/', $path); |
| 36 | |
| 37 | // and finally, add the PHP file extension to the result. |
| 38 | $path .= '.php'; |
| 39 | |
| 40 | // $path should now contain the path to a PHP file defining $class |
| 41 | if (file_exists($path)) { |
| 42 | include $path; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | spl_autoload_register('mmdb_autoload'); |
| 48 |