PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/vendor/automattic/jetpack-autoloader/src/class-path-processor.php

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