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