|
1 <?php |
|
2 /** |
|
3 * Polyfill for SPL autoload feature. This file is separate to prevent compiler notices |
|
4 * on the deprecated __autoload() function. |
|
5 * |
|
6 * See https://core.trac.wordpress.org/ticket/41134 |
|
7 * |
|
8 * @package PHP |
|
9 * @access private |
|
10 */ |
|
11 |
|
12 if ( ! function_exists( 'spl_autoload_register' ) ) { |
|
13 $_wp_spl_autoloaders = array(); |
|
14 |
|
15 /** |
|
16 * Autoloader compatibility callback. |
|
17 * |
|
18 * @since 4.6.0 |
|
19 * |
|
20 * @param string $classname Class to attempt autoloading. |
|
21 */ |
|
22 function __autoload( $classname ) { |
|
23 global $_wp_spl_autoloaders; |
|
24 foreach ( $_wp_spl_autoloaders as $autoloader ) { |
|
25 if ( ! is_callable( $autoloader ) ) { |
|
26 // Avoid the extra warning if the autoloader isn't callable. |
|
27 continue; |
|
28 } |
|
29 |
|
30 call_user_func( $autoloader, $classname ); |
|
31 |
|
32 // If it has been autoloaded, stop processing. |
|
33 if ( class_exists( $classname, false ) ) { |
|
34 return; |
|
35 } |
|
36 } |
|
37 } |
|
38 |
|
39 /** |
|
40 * Registers a function to be autoloaded. |
|
41 * |
|
42 * @since 4.6.0 |
|
43 * |
|
44 * @param callable $autoload_function The function to register. |
|
45 * @param bool $throw Optional. Whether the function should throw an exception |
|
46 * if the function isn't callable. Default true. |
|
47 * @param bool $prepend Whether the function should be prepended to the stack. |
|
48 * Default false. |
|
49 */ |
|
50 function spl_autoload_register( $autoload_function, $throw = true, $prepend = false ) { |
|
51 if ( $throw && ! is_callable( $autoload_function ) ) { |
|
52 // String not translated to match PHP core. |
|
53 throw new Exception( 'Function not callable' ); |
|
54 } |
|
55 |
|
56 global $_wp_spl_autoloaders; |
|
57 |
|
58 // Don't allow multiple registration. |
|
59 if ( in_array( $autoload_function, $_wp_spl_autoloaders ) ) { |
|
60 return; |
|
61 } |
|
62 |
|
63 if ( $prepend ) { |
|
64 array_unshift( $_wp_spl_autoloaders, $autoload_function ); |
|
65 } else { |
|
66 $_wp_spl_autoloaders[] = $autoload_function; |
|
67 } |
|
68 } |
|
69 |
|
70 /** |
|
71 * Unregisters an autoloader function. |
|
72 * |
|
73 * @since 4.6.0 |
|
74 * |
|
75 * @param callable $function The function to unregister. |
|
76 * @return bool True if the function was unregistered, false if it could not be. |
|
77 */ |
|
78 function spl_autoload_unregister( $function ) { |
|
79 global $_wp_spl_autoloaders; |
|
80 foreach ( $_wp_spl_autoloaders as &$autoloader ) { |
|
81 if ( $autoloader === $function ) { |
|
82 unset( $autoloader ); |
|
83 return true; |
|
84 } |
|
85 } |
|
86 |
|
87 return false; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Retrieves the registered autoloader functions. |
|
92 * |
|
93 * @since 4.6.0 |
|
94 * |
|
95 * @return array List of autoloader functions. |
|
96 */ |
|
97 function spl_autoload_functions() { |
|
98 return $GLOBALS['_wp_spl_autoloaders']; |
|
99 } |
|
100 } |