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