spl-autoload-compat.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. if ( ! function_exists( 'spl_autoload_register' ) ) {
  12. $_wp_spl_autoloaders = array();
  13. /**
  14. * Autoloader compatibility callback.
  15. *
  16. * @since 4.6.0
  17. *
  18. * @param string $classname Class to attempt autoloading.
  19. */
  20. function __autoload( $classname ) {
  21. global $_wp_spl_autoloaders;
  22. foreach ( $_wp_spl_autoloaders as $autoloader ) {
  23. if ( ! is_callable( $autoloader ) ) {
  24. // Avoid the extra warning if the autoloader isn't callable.
  25. continue;
  26. }
  27. call_user_func( $autoloader, $classname );
  28. // If it has been autoloaded, stop processing.
  29. if ( class_exists( $classname, false ) ) {
  30. return;
  31. }
  32. }
  33. }
  34. /**
  35. * Registers a function to be autoloaded.
  36. *
  37. * @since 4.6.0
  38. *
  39. * @param callable $autoload_function The function to register.
  40. * @param bool $throw Optional. Whether the function should throw an exception
  41. * if the function isn't callable. Default true.
  42. * @param bool $prepend Whether the function should be prepended to the stack.
  43. * Default false.
  44. */
  45. function spl_autoload_register( $autoload_function, $throw = true, $prepend = false ) {
  46. if ( $throw && ! is_callable( $autoload_function ) ) {
  47. // String not translated to match PHP core.
  48. throw new Exception( 'Function not callable' );
  49. }
  50. global $_wp_spl_autoloaders;
  51. // Don't allow multiple registration.
  52. if ( in_array( $autoload_function, $_wp_spl_autoloaders ) ) {
  53. return;
  54. }
  55. if ( $prepend ) {
  56. array_unshift( $_wp_spl_autoloaders, $autoload_function );
  57. } else {
  58. $_wp_spl_autoloaders[] = $autoload_function;
  59. }
  60. }
  61. /**
  62. * Unregisters an autoloader function.
  63. *
  64. * @since 4.6.0
  65. *
  66. * @param callable $function The function to unregister.
  67. * @return bool True if the function was unregistered, false if it could not be.
  68. */
  69. function spl_autoload_unregister( $function ) {
  70. global $_wp_spl_autoloaders;
  71. foreach ( $_wp_spl_autoloaders as &$autoloader ) {
  72. if ( $autoloader === $function ) {
  73. unset( $autoloader );
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. /**
  80. * Retrieves the registered autoloader functions.
  81. *
  82. * @since 4.6.0
  83. *
  84. * @return array List of autoloader functions.
  85. */
  86. function spl_autoload_functions() {
  87. return $GLOBALS['_wp_spl_autoloaders'];
  88. }
  89. }