ORIENT/vendor/wikimedia/less.php/lib/Less/Autoloader.php

58 lines
1.1 KiB
PHP
Raw Normal View History

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