Nessuna descrizione

class-php-autoloader.php 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * This file was automatically generated by automattic/jetpack-autoloader.
  4. *
  5. * @package automattic/jetpack-autoloader
  6. */
  7. namespace Automattic\Jetpack\Autoloader\jpf11009ded9fc4592b6a05b61ce272b3c_jetpackⓥ10_2;
  8. // phpcs:ignore
  9. /**
  10. * This class handles management of the actual PHP autoloader.
  11. */
  12. class PHP_Autoloader {
  13. /**
  14. * Registers the autoloader with PHP so that it can begin autoloading classes.
  15. *
  16. * @param Version_Loader $version_loader The class loader to use in the autoloader.
  17. */
  18. public function register_autoloader( $version_loader ) {
  19. // Make sure no other autoloaders are registered.
  20. $this->unregister_autoloader();
  21. // Set the global so that it can be used to load classes.
  22. global $jetpack_autoloader_loader;
  23. $jetpack_autoloader_loader = $version_loader;
  24. // Ensure that the autoloader is first to avoid contention with others.
  25. spl_autoload_register( array( self::class, 'load_class' ), true, true );
  26. }
  27. /**
  28. * Unregisters the active autoloader so that it will no longer autoload classes.
  29. */
  30. public function unregister_autoloader() {
  31. // Remove any v2 autoloader that we've already registered.
  32. $autoload_chain = spl_autoload_functions();
  33. foreach ( $autoload_chain as $autoloader ) {
  34. // We can identify a v2 autoloader using the namespace.
  35. $namespace_check = null;
  36. // Functions are recorded as strings.
  37. if ( is_string( $autoloader ) ) {
  38. $namespace_check = $autoloader;
  39. } elseif ( is_array( $autoloader ) && is_string( $autoloader[0] ) ) {
  40. // Static method calls have the class as the first array element.
  41. $namespace_check = $autoloader[0];
  42. } else {
  43. // Since the autoloader has only ever been a function or a static method we don't currently need to check anything else.
  44. continue;
  45. }
  46. // Check for the namespace without the generated suffix.
  47. if ( 'Automattic\\Jetpack\\Autoloader\\jp' === substr( $namespace_check, 0, 32 ) ) {
  48. spl_autoload_unregister( $autoloader );
  49. }
  50. }
  51. // Clear the global now that the autoloader has been unregistered.
  52. global $jetpack_autoloader_loader;
  53. $jetpack_autoloader_loader = null;
  54. }
  55. /**
  56. * Loads a class file if one could be found.
  57. *
  58. * Note: This function is static so that the autoloader can be easily unregistered. If
  59. * it was a class method we would have to unwrap the object to check the namespace.
  60. *
  61. * @param string $class_name The name of the class to autoload.
  62. *
  63. * @return bool Indicates whether or not a class file was loaded.
  64. */
  65. public static function load_class( $class_name ) {
  66. global $jetpack_autoloader_loader;
  67. if ( ! isset( $jetpack_autoloader_loader ) ) {
  68. return;
  69. }
  70. $file = $jetpack_autoloader_loader->find_class_file( $class_name );
  71. if ( ! isset( $file ) ) {
  72. return false;
  73. }
  74. require $file;
  75. return true;
  76. }
  77. }