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