| 1 |
<?php |
| 2 |
/** |
| 3 |
* PSR-4 Autoloader for the GutSlider namespace. |
| 4 |
* |
| 5 |
* Maps GutSlider\Foo\Bar to includes/Foo/Bar.php. |
| 6 |
* |
| 7 |
* @package GutSlider |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class GutSlider_Autoloader |
| 17 |
* |
| 18 |
* Handles PSR-4 autoloading for all classes within the GutSlider namespace. |
| 19 |
* This class is intentionally not namespaced because it must be loaded via |
| 20 |
* require_once before the autoloader is registered. |
| 21 |
* |
| 22 |
* @since 3.0.0 |
| 23 |
*/ |
| 24 |
final class GutSlider_Autoloader { |
| 25 |
|
| 26 |
/** |
| 27 |
* The namespace prefix for GutSlider classes. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private const NAMESPACE_PREFIX = 'GutSlider\\'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Base directory for the namespace prefix. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private string $base_dir; |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor. |
| 42 |
* |
| 43 |
* @since 3.0.0 |
| 44 |
* |
| 45 |
* @param string $base_dir The base directory where namespaced classes reside. |
| 46 |
*/ |
| 47 |
public function __construct( string $base_dir ) { |
| 48 |
$this->base_dir = rtrim( $base_dir, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Register the autoloader with spl_autoload_register. |
| 53 |
* |
| 54 |
* @since 3.0.0 |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function register(): void { |
| 59 |
spl_autoload_register( array( $this, 'autoload' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Autoload a class file based on its fully-qualified class name. |
| 64 |
* |
| 65 |
* @since 3.0.0 |
| 66 |
* |
| 67 |
* @param string $class The fully-qualified class name. |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function autoload( string $class ): void { |
| 71 |
if ( strpos( $class, self::NAMESPACE_PREFIX ) !== 0 ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$relative_class = substr( $class, strlen( self::NAMESPACE_PREFIX ) ); |
| 76 |
$file = $this->base_dir . str_replace( '\\', DIRECTORY_SEPARATOR, $relative_class ) . '.php'; |
| 77 |
|
| 78 |
if ( file_exists( $file ) ) { |
| 79 |
require_once $file; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|