Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 12 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Less_Autoloader | |
0.00% |
0 / 12 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| register | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| unregister | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| loadClass | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | /** |
| 5 | * Autoloader |
| 6 | */ |
| 7 | class Less_Autoloader { |
| 8 | |
| 9 | protected static bool $registered = false; |
| 10 | |
| 11 | /** |
| 12 | * Register the autoloader in the SPL autoloader |
| 13 | * |
| 14 | * @throws Exception If there was an error in registration |
| 15 | */ |
| 16 | public static function register(): void { |
| 17 | if ( self::$registered ) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | if ( !spl_autoload_register( [ __CLASS__, 'loadClass' ] ) ) { |
| 22 | throw new Exception( 'Unable to register Less_Autoloader::loadClass as an autoloading method.' ); |
| 23 | } |
| 24 | |
| 25 | self::$registered = true; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Unregister the autoloader |
| 30 | */ |
| 31 | public static function unregister(): void { |
| 32 | spl_autoload_unregister( [ __CLASS__, 'loadClass' ] ); |
| 33 | self::$registered = false; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Load the class |
| 38 | * |
| 39 | * @param string $className The class to load |
| 40 | */ |
| 41 | public static function loadClass( string $className ): void { |
| 42 | // handle only package classes |
| 43 | if ( !str_starts_with( $className, 'Less_' ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $className = substr( $className, 5 ); |
| 48 | $fileName = __DIR__ . DIRECTORY_SEPARATOR . str_replace( '_', DIRECTORY_SEPARATOR, $className ) . '.php'; |
| 49 | |
| 50 | require $fileName; |
| 51 | } |
| 52 | |
| 53 | } |