alma-gateway-for-woocommerce
Last commit date
assets
3 years ago
includes
3 years ago
languages
3 years ago
public
3 years ago
tests
3 years ago
trunk
3 years ago
vendor
3 years ago
LICENSE
4 years ago
alma-gateway-for-woocommerce.php
3 years ago
autoload.php
3 years ago
composer.json
3 years ago
composer.lock
3 years ago
phpcs.xml
3 years ago
readme.txt
3 years ago
uninstall.php
3 years ago
autoload.php
51 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Autoload. |
| 4 | * |
| 5 | * @package Alma_Gataway_For_Woocommerce |
| 6 | */ |
| 7 | |
| 8 | // Define the main autoloader. |
| 9 | spl_autoload_register( 'alma_wc_autoloader' ); |
| 10 | |
| 11 | /** |
| 12 | * Autoload the files. |
| 13 | * |
| 14 | * @param string $class_name The class name. |
| 15 | * @return void |
| 16 | */ |
| 17 | function alma_wc_autoloader( $class_name ) { |
| 18 | |
| 19 | $parent_namespace = 'Alma\Woocommerce'; |
| 20 | $classes_subfolder = 'includes'; |
| 21 | |
| 22 | if ( false !== strpos( $class_name, $parent_namespace ) ) { |
| 23 | $classes_dir = realpath( plugin_dir_path( __FILE__ ) ) . DIRECTORY_SEPARATOR . $classes_subfolder . DIRECTORY_SEPARATOR; |
| 24 | |
| 25 | // Project namespace. |
| 26 | $project_namespace = $parent_namespace . '\\'; |
| 27 | $length = strlen( $project_namespace ); |
| 28 | |
| 29 | // Remove top level namespace (that is the current dir). |
| 30 | $class_file = substr( $class_name, $length ); |
| 31 | |
| 32 | // Swap underscores for dashes and lowercase. |
| 33 | $class_file = str_replace( '_', '-', strtolower( $class_file ) ); |
| 34 | |
| 35 | // Prepend `class-` to the filename (last class part). |
| 36 | $class_parts = explode( '\\', $class_file ); |
| 37 | $last_index = count( $class_parts ) - 1; |
| 38 | $class_parts[ $last_index ] = 'class-' . $class_parts[ $last_index ]; |
| 39 | |
| 40 | // Join everything back together and add the file extension. |
| 41 | $class_file = implode( DIRECTORY_SEPARATOR, $class_parts ) . '.php'; |
| 42 | $location = $classes_dir . $class_file; |
| 43 | |
| 44 | if ( ! is_file( $location ) ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | require_once $location; |
| 49 | } |
| 50 | } |
| 51 |