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