AutoloadFileWriter.php
11 months ago
AutoloadGenerator.php
11 months ago
AutoloadProcessor.php
11 months ago
CustomAutoloaderPlugin.php
11 months ago
ManifestGenerator.php
11 months ago
autoload.php
11 months ago
class-autoloader-handler.php
11 months ago
class-autoloader-locator.php
11 months ago
class-autoloader.php
11 months ago
class-container.php
11 months ago
class-hook-manager.php
11 months ago
class-latest-autoloader-guard.php
11 months ago
class-manifest-reader.php
11 months ago
class-path-processor.php
11 months ago
class-php-autoloader.php
11 months ago
class-plugin-locator.php
11 months ago
class-plugins-handler.php
11 months ago
class-shutdown-handler.php
11 months ago
class-version-loader.php
11 months ago
class-version-selector.php
11 months ago
index.php
11 months ago
class-version-loader.php
89 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | // phpcs:ignore |
| 4 | class Version_Loader { |
| 5 | private $version_selector; |
| 6 | private $classmap; |
| 7 | private $psr4_map; |
| 8 | private $filemap; |
| 9 | public function __construct( $version_selector, $classmap, $psr4_map, $filemap ) { |
| 10 | $this->version_selector = $version_selector; |
| 11 | $this->classmap = $classmap; |
| 12 | $this->psr4_map = $psr4_map; |
| 13 | $this->filemap = $filemap; |
| 14 | } |
| 15 | public function get_class_map() { |
| 16 | return $this->classmap; |
| 17 | } |
| 18 | public function get_psr4_map() { |
| 19 | return $this->psr4_map; |
| 20 | } |
| 21 | public function find_class_file( $class_name ) { |
| 22 | $data = $this->select_newest_file( |
| 23 | $this->classmap[ $class_name ] ?? null, |
| 24 | $this->find_psr4_file( $class_name ) |
| 25 | ); |
| 26 | if ( ! isset( $data ) ) { |
| 27 | return null; |
| 28 | } |
| 29 | return $data['path']; |
| 30 | } |
| 31 | public function load_filemap() { |
| 32 | if ( empty( $this->filemap ) ) { |
| 33 | return; |
| 34 | } |
| 35 | foreach ( $this->filemap as $file_identifier => $file_data ) { |
| 36 | if ( empty( $GLOBALS['__composer_autoload_files'][ $file_identifier ] ) ) { |
| 37 | require_once $file_data['path']; |
| 38 | $GLOBALS['__composer_autoload_files'][ $file_identifier ] = true; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | private function select_newest_file( $classmap_data, $psr4_data ) { |
| 43 | if ( ! isset( $classmap_data ) ) { |
| 44 | return $psr4_data; |
| 45 | } elseif ( ! isset( $psr4_data ) ) { |
| 46 | return $classmap_data; |
| 47 | } |
| 48 | if ( $this->version_selector->is_version_update_required( $classmap_data['version'], $psr4_data['version'] ) ) { |
| 49 | return $psr4_data; |
| 50 | } |
| 51 | return $classmap_data; |
| 52 | } |
| 53 | private function find_psr4_file( $class_name ) { |
| 54 | if ( empty( $this->psr4_map ) ) { |
| 55 | return null; |
| 56 | } |
| 57 | // Don't bother with classes that have no namespace. |
| 58 | $class_index = strrpos( $class_name, '\\' ); |
| 59 | if ( ! $class_index ) { |
| 60 | return null; |
| 61 | } |
| 62 | $class_for_path = str_replace( '\\', '/', $class_name ); |
| 63 | // Search for the namespace by iteratively cutting off the last segment until |
| 64 | // we find a match. This allows us to check the most-specific namespaces |
| 65 | // first as well as minimize the amount of time spent looking. |
| 66 | for ( |
| 67 | $class_namespace = substr( $class_name, 0, $class_index ); |
| 68 | ! empty( $class_namespace ); |
| 69 | $class_namespace = substr( $class_namespace, 0, strrpos( $class_namespace, '\\' ) ) |
| 70 | ) { |
| 71 | $namespace = $class_namespace . '\\'; |
| 72 | if ( ! isset( $this->psr4_map[ $namespace ] ) ) { |
| 73 | continue; |
| 74 | } |
| 75 | $data = $this->psr4_map[ $namespace ]; |
| 76 | foreach ( $data['path'] as $path ) { |
| 77 | $path .= '/' . substr( $class_for_path, strlen( $namespace ) ) . '.php'; |
| 78 | if ( file_exists( $path ) ) { |
| 79 | return array( |
| 80 | 'version' => $data['version'], |
| 81 | 'path' => $path, |
| 82 | ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | return null; |
| 87 | } |
| 88 | } |
| 89 |