wpforms-lite
Last commit date
assets
7 years ago
includes
7 years ago
languages
7 years ago
lite
7 years ago
src
7 years ago
autoloader.php
7 years ago
changelog.txt
7 years ago
readme.txt
7 years ago
uninstall.php
7 years ago
wpforms.php
7 years ago
autoloader.php
47 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Autoloader, inspired by PSR4. |
| 4 | * |
| 5 | * @package WPForms |
| 6 | * @author WPForms |
| 7 | * @since 1.4.7 |
| 8 | * @license GPL-2.0+ |
| 9 | * @copyright Copyright (c) 2018, WPForms LLC |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Register the autoloader. |
| 14 | * |
| 15 | * @since 1.4.7 |
| 16 | * |
| 17 | * @param string $class |
| 18 | */ |
| 19 | spl_autoload_register( function ( $class ) { |
| 20 | |
| 21 | // Our base namespace for all plugin classes. |
| 22 | $prefix = 'WPForms\\'; |
| 23 | |
| 24 | // Does the class use the namespace prefix? |
| 25 | $len = strlen( $prefix ); |
| 26 | if ( strncmp( $prefix, $class, $len ) !== 0 ) { |
| 27 | // No, move to the next registered autoloader. |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | // Base directory for the namespace prefix. |
| 32 | $base_dir = __DIR__ . '/src/'; |
| 33 | |
| 34 | // Get the relative class name. |
| 35 | $relative_class = substr( $class, $len ); |
| 36 | |
| 37 | // Replace the namespace prefix with the base directory. |
| 38 | // Replace namespace separators with directory separators in the relative |
| 39 | // class name. Append with .php. |
| 40 | $file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php'; |
| 41 | |
| 42 | // Finally, require the file. |
| 43 | if ( file_exists( $file ) ) { |
| 44 | require_once $file; |
| 45 | } |
| 46 | } ); |
| 47 |