Нет описания

class-version-loader.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\jp7853afa8732e39f60cfddd564b056bf1;
  8. // phpcs:ignore
  9. /**
  10. * This class loads other classes based on given parameters.
  11. */
  12. class Version_Loader {
  13. /**
  14. * The Version_Selector object.
  15. *
  16. * @var Version_Selector
  17. */
  18. private $version_selector;
  19. /**
  20. * A map of available classes and their version and file path.
  21. *
  22. * @var array
  23. */
  24. private $classmap;
  25. /**
  26. * A map of PSR-4 namespaces and their version and directory path.
  27. *
  28. * @var array
  29. */
  30. private $psr4_map;
  31. /**
  32. * A map of all the files that we should load.
  33. *
  34. * @var array
  35. */
  36. private $filemap;
  37. /**
  38. * The constructor.
  39. *
  40. * @param Version_Selector $version_selector The Version_Selector object.
  41. * @param array $classmap The verioned classmap to load using.
  42. * @param array $psr4_map The versioned PSR-4 map to load using.
  43. * @param array $filemap The versioned filemap to load.
  44. */
  45. public function __construct( $version_selector, $classmap, $psr4_map, $filemap ) {
  46. $this->version_selector = $version_selector;
  47. $this->classmap = $classmap;
  48. $this->psr4_map = $psr4_map;
  49. $this->filemap = $filemap;
  50. }
  51. /**
  52. * Finds the file path for the given class.
  53. *
  54. * @param string $class_name The class to find.
  55. *
  56. * @return string|null $file_path The path to the file if found, null if no class was found.
  57. */
  58. public function find_class_file( $class_name ) {
  59. $data = $this->select_newest_file(
  60. isset( $this->classmap[ $class_name ] ) ? $this->classmap[ $class_name ] : null,
  61. $this->find_psr4_file( $class_name )
  62. );
  63. if ( ! isset( $data ) ) {
  64. return null;
  65. }
  66. return $data['path'];
  67. }
  68. /**
  69. * Load all of the files in the filemap.
  70. */
  71. public function load_filemap() {
  72. if ( empty( $this->filemap ) ) {
  73. return;
  74. }
  75. foreach ( $this->filemap as $file_identifier => $file_data ) {
  76. if ( empty( $GLOBALS['__composer_autoload_files'][ $file_identifier ] ) ) {
  77. require_once $file_data['path'];
  78. $GLOBALS['__composer_autoload_files'][ $file_identifier ] = true;
  79. }
  80. }
  81. }
  82. /**
  83. * Compares different class sources and returns the newest.
  84. *
  85. * @param array|null $classmap_data The classmap class data.
  86. * @param array|null $psr4_data The PSR-4 class data.
  87. *
  88. * @return array|null $data
  89. */
  90. private function select_newest_file( $classmap_data, $psr4_data ) {
  91. if ( ! isset( $classmap_data ) ) {
  92. return $psr4_data;
  93. } elseif ( ! isset( $psr4_data ) ) {
  94. return $classmap_data;
  95. }
  96. if ( $this->version_selector->is_version_update_required( $classmap_data['version'], $psr4_data['version'] ) ) {
  97. return $psr4_data;
  98. }
  99. return $classmap_data;
  100. }
  101. /**
  102. * Finds the file for a given class in a PSR-4 namespace.
  103. *
  104. * @param string $class_name The class to find.
  105. *
  106. * @return array|null $data The version and path path to the file if found, null otherwise.
  107. */
  108. private function find_psr4_file( $class_name ) {
  109. if ( ! isset( $this->psr4_map ) ) {
  110. return null;
  111. }
  112. // Don't bother with classes that have no namespace.
  113. $class_index = strrpos( $class_name, '\\' );
  114. if ( ! $class_index ) {
  115. return null;
  116. }
  117. $class_for_path = str_replace( '\\', '/', $class_name );
  118. // Search for the namespace by iteratively cutting off the last segment until
  119. // we find a match. This allows us to check the most-specific namespaces
  120. // first as well as minimize the amount of time spent looking.
  121. for (
  122. $class_namespace = substr( $class_name, 0, $class_index );
  123. ! empty( $class_namespace );
  124. $class_namespace = substr( $class_namespace, 0, strrpos( $class_namespace, '\\' ) )
  125. ) {
  126. $namespace = $class_namespace . '\\';
  127. if ( ! isset( $this->psr4_map[ $namespace ] ) ) {
  128. continue;
  129. }
  130. $data = $this->psr4_map[ $namespace ];
  131. foreach ( $data['path'] as $path ) {
  132. $path .= '/' . substr( $class_for_path, strlen( $namespace ) ) . '.php';
  133. if ( file_exists( $path ) ) {
  134. return array(
  135. 'version' => $data['version'],
  136. 'path' => $path,
  137. );
  138. }
  139. }
  140. }
  141. return null;
  142. }
  143. }