Açıklama Yok

class-path-processor.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 handles dealing with paths for the autoloader.
  11. */
  12. class Path_Processor {
  13. /**
  14. * Given a path this will replace any of the path constants with a token to represent it.
  15. *
  16. * @param string $path The path we want to process.
  17. *
  18. * @return string The tokenized path.
  19. */
  20. public function tokenize_path_constants( $path ) {
  21. $path = wp_normalize_path( $path );
  22. $constants = $this->get_normalized_constants();
  23. foreach ( $constants as $constant => $constant_path ) {
  24. $len = strlen( $constant_path );
  25. if ( substr( $path, 0, $len ) !== $constant_path ) {
  26. continue;
  27. }
  28. return substr_replace( $path, '{{' . $constant . '}}', 0, $len );
  29. }
  30. return $path;
  31. }
  32. /**
  33. * Given a path this will replace any of the path constant tokens with the expanded path.
  34. *
  35. * @param string $tokenized_path The path we want to process.
  36. *
  37. * @return string The expanded path.
  38. */
  39. public function untokenize_path_constants( $tokenized_path ) {
  40. $tokenized_path = wp_normalize_path( $tokenized_path );
  41. $constants = $this->get_normalized_constants();
  42. foreach ( $constants as $constant => $constant_path ) {
  43. $constant = '{{' . $constant . '}}';
  44. $len = strlen( $constant );
  45. if ( substr( $tokenized_path, 0, $len ) !== $constant ) {
  46. continue;
  47. }
  48. return $this->get_real_path( substr_replace( $tokenized_path, $constant_path, 0, $len ) );
  49. }
  50. return $tokenized_path;
  51. }
  52. /**
  53. * Given a file and an array of places it might be, this will find the absolute path and return it.
  54. *
  55. * @param string $file The plugin or theme file to resolve.
  56. * @param array $directories_to_check The directories we should check for the file if it isn't an absolute path.
  57. *
  58. * @return string|false Returns the absolute path to the directory, otherwise false.
  59. */
  60. public function find_directory_with_autoloader( $file, $directories_to_check ) {
  61. $file = wp_normalize_path( $file );
  62. if ( ! $this->is_absolute_path( $file ) ) {
  63. $file = $this->find_absolute_plugin_path( $file, $directories_to_check );
  64. if ( ! isset( $file ) ) {
  65. return false;
  66. }
  67. }
  68. // We need the real path for consistency with __DIR__ paths.
  69. $file = $this->get_real_path( $file );
  70. // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
  71. $directory = @is_file( $file ) ? dirname( $file ) : $file;
  72. if ( ! @is_file( $directory . '/vendor/composer/jetpack_autoload_classmap.php' ) ) {
  73. return false;
  74. }
  75. // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
  76. return $directory;
  77. }
  78. /**
  79. * Fetches an array of normalized paths keyed by the constant they came from.
  80. *
  81. * @return string[] The normalized paths keyed by the constant.
  82. */
  83. private function get_normalized_constants() {
  84. $raw_constants = array(
  85. // Order the constants from most-specific to least-specific.
  86. 'WP_PLUGIN_DIR',
  87. 'WPMU_PLUGIN_DIR',
  88. 'WP_CONTENT_DIR',
  89. 'ABSPATH',
  90. );
  91. $constants = array();
  92. foreach ( $raw_constants as $raw ) {
  93. if ( ! defined( $raw ) ) {
  94. continue;
  95. }
  96. $path = wp_normalize_path( constant( $raw ) );
  97. if ( isset( $path ) ) {
  98. $constants[ $raw ] = $path;
  99. }
  100. }
  101. return $constants;
  102. }
  103. /**
  104. * Indicates whether or not a path is absolute.
  105. *
  106. * @param string $path The path to check.
  107. *
  108. * @return bool True if the path is absolute, otherwise false.
  109. */
  110. private function is_absolute_path( $path ) {
  111. if ( 0 === strlen( $path ) || '.' === $path[0] ) {
  112. return false;
  113. }
  114. // Absolute paths on Windows may begin with a drive letter.
  115. if ( preg_match( '/^[a-zA-Z]:[\/\\\\]/', $path ) ) {
  116. return true;
  117. }
  118. // A path starting with / or \ is absolute; anything else is relative.
  119. return ( '/' === $path[0] || '\\' === $path[0] );
  120. }
  121. /**
  122. * Given a file and a list of directories to check, this method will try to figure out
  123. * the absolute path to the file in question.
  124. *
  125. * @param string $normalized_path The normalized path to the plugin or theme file to resolve.
  126. * @param array $directories_to_check The directories we should check for the file if it isn't an absolute path.
  127. *
  128. * @return string|null The absolute path to the plugin file, otherwise null.
  129. */
  130. private function find_absolute_plugin_path( $normalized_path, $directories_to_check ) {
  131. // We're only able to find the absolute path for plugin/theme PHP files.
  132. if ( ! is_string( $normalized_path ) || '.php' !== substr( $normalized_path, -4 ) ) {
  133. return null;
  134. }
  135. foreach ( $directories_to_check as $directory ) {
  136. $normalized_check = wp_normalize_path( trailingslashit( $directory ) ) . $normalized_path;
  137. // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
  138. if ( @is_file( $normalized_check ) ) {
  139. return $normalized_check;
  140. }
  141. }
  142. return null;
  143. }
  144. /**
  145. * Given a path this will figure out the real path that we should be using.
  146. *
  147. * @param string $path The path to resolve.
  148. *
  149. * @return string The resolved path.
  150. */
  151. private function get_real_path( $path ) {
  152. // We want to resolve symbolic links for consistency with __DIR__ paths.
  153. // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
  154. $real_path = @realpath( $path );
  155. if ( false === $real_path ) {
  156. // Let the autoloader deal with paths that don't exist.
  157. $real_path = $path;
  158. }
  159. // Using realpath will make it platform-specific so we must normalize it after.
  160. if ( $path !== $real_path ) {
  161. $real_path = wp_normalize_path( $real_path );
  162. }
  163. return $real_path;
  164. }
  165. }