2021-02-15 21:29:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Autoloader
|
|
|
|
*/
|
|
|
|
class Less_Autoloader {
|
|
|
|
|
2023-07-07 22:33:10 +02:00
|
|
|
/** @var bool */
|
2021-02-15 21:29:38 +01:00
|
|
|
protected static $registered = false;
|
|
|
|
|
|
|
|
/**
|
2023-07-07 22:33:10 +02:00
|
|
|
* Register the autoloader in the SPL autoloader
|
2021-02-15 21:29:38 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @throws Exception If there was an error in registration
|
|
|
|
*/
|
|
|
|
public static function register() {
|
|
|
|
if ( self::$registered ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-07 22:33:10 +02:00
|
|
|
if ( !spl_autoload_register( [ 'Less_Autoloader', 'loadClass' ] ) ) {
|
2021-02-15 21:29:38 +01:00
|
|
|
throw new Exception( 'Unable to register Less_Autoloader::loadClass as an autoloading method.' );
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$registered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-07 22:33:10 +02:00
|
|
|
* Unregister the autoloader
|
2021-02-15 21:29:38 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function unregister() {
|
2023-07-07 22:33:10 +02:00
|
|
|
spl_autoload_unregister( [ 'Less_Autoloader', 'loadClass' ] );
|
2021-02-15 21:29:38 +01:00
|
|
|
self::$registered = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-07 22:33:10 +02:00
|
|
|
* Load the class
|
2021-02-15 21:29:38 +01:00
|
|
|
*
|
|
|
|
* @param string $className The class to load
|
|
|
|
*/
|
|
|
|
public static function loadClass( $className ) {
|
|
|
|
// handle only package classes
|
|
|
|
if ( strpos( $className, 'Less_' ) !== 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = substr( $className, 5 );
|
2023-07-07 22:33:10 +02:00
|
|
|
$fileName = __DIR__ . DIRECTORY_SEPARATOR . str_replace( '_', DIRECTORY_SEPARATOR, $className ) . '.php';
|
2021-02-15 21:29:38 +01:00
|
|
|
|
2023-07-07 22:33:10 +02:00
|
|
|
require $fileName;
|
|
|
|
return true;
|
2021-02-15 21:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|