WPML_Auto_Loader

Class WPML_Auto_Loader This class is designed to be either included or instantiated (as singleton and as long as it's loaded somewhere else) to handle class auto loading.

Defined (1)

The class is defined in the following location(s).

/vendor/wpml/commons/src/wpml-auto-loader.php  
  1. class WPML_Auto_Loader { 
  2. private $accepted_prefixes = array(); 
  3. private $base_dirs = array(); 
  4. private $classes_base_folder; 
  5. private $include_root; 
  6. private static $instance; 
  7. private $known_classes = array(); 
  8. private $glob_cache = array(); 
  9.  
  10.  
  11. /** 
  12. * @return WPML_Auto_Loader 
  13. */ 
  14. public static function get_instance() { 
  15. if ( null === self::$instance ) { 
  16. self::$instance = new WPML_Auto_Loader(); 
  17.  
  18. return self::$instance; 
  19.  
  20. public function add_known_class( $class, $file ) { 
  21. $this->known_classes[ $class ] = $file; 
  22.  
  23. public function autoload( $class ) { 
  24. $file = $this->get_file( $class ); 
  25.  
  26. if ( $file ) { 
  27. /** @noinspection PhpIncludeInspection */ 
  28. require_once $file; 
  29.  
  30. /** 
  31. * @param $class 
  32. * @return mixed|null|string 
  33. */ 
  34. private function get_file( $class ) { 
  35. $file = null; 
  36. if ( $this->is_accepted_class( $class ) ) { 
  37. $file = $this->get_file_from_known_classes( $class ); 
  38. if ( null === $file ) { 
  39. $file = $this->get_file_from_name( $class, 'class' ); 
  40. if ( $this->is_accepted_interface( $class ) ) { 
  41. $file = $this->get_file_from_name( $class, 'interface' ); 
  42. }  
  43.  
  44. return $file; 
  45.  
  46. private function is_accepted_class( $class ) { 
  47. foreach ( $this->accepted_prefixes as $accepted_prefix ) { 
  48. if ( 0 === strpos( $class, $accepted_prefix ) ) { 
  49. return true; 
  50.  
  51. return false; 
  52.  
  53. private function is_accepted_interface( $class ) { 
  54. foreach ( $this->accepted_prefixes as $accepted_prefix ) { 
  55. if ( 0 === strpos( $class, 'I' . $accepted_prefix ) ) { 
  56. return true; 
  57.  
  58. return false; 
  59. }  
  60.  
  61. /** 
  62. * @param string $class 
  63. * @return string|null 
  64. */ 
  65. private function get_file_from_known_classes( $class ) { 
  66. $file = null; 
  67. if ( array_key_exists( $class, $this->known_classes ) && is_file( $this->known_classes[ $class ] ) ) { 
  68. $file = $this->known_classes[ $class ]; 
  69.  
  70. return $file; 
  71.  
  72. /** 
  73. * @param string $class 
  74. * @param string $prefix 
  75. * @return null|string 
  76. */ 
  77. private function get_file_from_name( $class, $prefix ) { 
  78. $file = null; 
  79. $file_name = $prefix . '-' . strtolower( str_replace( array( '_', "\0" ), array( '-', '' ), $class ) . '.php' ); 
  80.  
  81. if ( $this->include_root ) { 
  82. $base_dirs = $this->get_base_dirs(); 
  83. foreach ( $base_dirs as $base_dir ) { 
  84. $current_dir = $this->build_dir( $base_dir, null, false ); 
  85. $possible_full_path = $current_dir . $file_name; 
  86. if ( is_file( $possible_full_path ) ) { 
  87. $file = $possible_full_path; 
  88.  
  89. if ( ! $file ) { 
  90. $possible_file = $this->get_file_from_path( $file_name, null, false ); 
  91. if ( is_file( $possible_file ) ) { 
  92. $file = $possible_file; 
  93.  
  94. return $file; 
  95.  
  96. private function build_dir( $base_dir, $path = null, $with_base_folder = true ) { 
  97. if ( $with_base_folder ) { 
  98. $base_dir .= $this->classes_base_folder; 
  99. if ( $path ) { 
  100. $base_dir .= $path; 
  101.  
  102. return $base_dir; 
  103.  
  104. private function get_file_from_path( $file_name, $path = null, $deep = false ) { 
  105. $file = null; 
  106. if ( $path ) { 
  107. $base_dirs = array( $path ); 
  108. $path = null; 
  109. } else { 
  110. $base_dirs = $this->get_base_dirs(); 
  111. $found_file = false; 
  112.  
  113. foreach ( $base_dirs as $base_dir ) { 
  114. $current_dir = $base_dir; 
  115. if ( ! $deep ) { 
  116. $current_dir = $this->build_dir( $base_dir, $path, true ); 
  117. $possible_full_path = $current_dir . '/' . $file_name; 
  118. if ( is_file( $possible_full_path ) ) { 
  119. $file = $possible_full_path; 
  120. } else { 
  121. $current_dir = $this->escape_path( $current_dir ); 
  122. if ( ! isset( $this->glob_cache[ $current_dir ] ) ) { 
  123. $this->glob_cache[ $current_dir ] = glob( $current_dir . '/*', GLOB_ONLYDIR ); 
  124. $this->glob_cache[ $current_dir ] = false === $this->glob_cache[ $current_dir ] ? array() : $this->glob_cache[ $current_dir ]; 
  125. foreach ( (array) $this->glob_cache[ $current_dir ] as $sub_folder_path ) { 
  126. $found_file = $this->get_file_from_path( $file_name, $sub_folder_path, true ); 
  127. if ( null !== $found_file ) { 
  128. $file = $found_file; 
  129. break; 
  130. if ( $found_file ) { 
  131. break; 
  132.   
  133. return $file; 
  134.   
  135. public function register( $base_dir, array $accepted_prefixes = array( 'WPML' ), $classes_base_folder = 'classes', $include_root = false, $prepend = false ) { 
  136. $this->add_base_dir( $base_dir ); 
  137. $this->accepted_prefixes = $accepted_prefixes; 
  138. $this->classes_base_folder = $classes_base_folder; 
  139. $this->known_classes = array(); 
  140. $this->include_root = $include_root; 
  141.   
  142. if ( version_compare( phpversion(), '5.3.0', '>=' ) ) { 
  143. spl_autoload_register( array( $this, 'autoload' ), true, $prepend ); 
  144. } else { 
  145. spl_autoload_register( array( $this, 'autoload' ) ); 
  146.   
  147. private function add_base_dir( $base_dir ) { 
  148. if ( $this->is_base_dir_registered( $base_dir ) ) { 
  149. $this->base_dirs[] = $base_dir; 
  150.   
  151. private function get_base_dirs() { 
  152. return $this->base_dirs; 
  153.   
  154. /** 
  155. * @param $base_dir 
  156. * @return bool 
  157. */ 
  158. private function is_base_dir_registered( $base_dir ) { 
  159. return ! in_array( $base_dir, $this->base_dirs, true ); 
  160.  
  161. /** 
  162. * @param string $current_dir 
  163. * @return string 
  164. */ 
  165. private function escape_path( $current_dir ) { 
  166. return preg_replace( '/([\[\]<>?|+;="])/', '\\\\$1', $current_dir );