| @@ 9-82 (lines=74) @@ | ||
| 6 | /** |
|
| 7 | * This class locates autoloaders. |
|
| 8 | */ |
|
| 9 | class Autoloader_Locator { |
|
| 10 | ||
| 11 | /** |
|
| 12 | * The object for comparing autoloader versions. |
|
| 13 | * |
|
| 14 | * @var Version_Selector |
|
| 15 | */ |
|
| 16 | private $version_selector; |
|
| 17 | ||
| 18 | /** |
|
| 19 | * The constructor. |
|
| 20 | * |
|
| 21 | * @param Version_Selector $version_selector The version selector object. |
|
| 22 | */ |
|
| 23 | public function __construct( $version_selector ) { |
|
| 24 | $this->version_selector = $version_selector; |
|
| 25 | } |
|
| 26 | ||
| 27 | /** |
|
| 28 | * Finds the path to the plugin with the latest autoloader. |
|
| 29 | * |
|
| 30 | * @param array $plugin_paths An array of plugin paths. |
|
| 31 | * @param string $latest_version The latest version reference. |
|
| 32 | * |
|
| 33 | * @return string|null |
|
| 34 | */ |
|
| 35 | public function find_latest_autoloader( $plugin_paths, &$latest_version ) { |
|
| 36 | $latest_plugin = null; |
|
| 37 | ||
| 38 | foreach ( $plugin_paths as $plugin_path ) { |
|
| 39 | $version = $this->get_autoloader_version( $plugin_path ); |
|
| 40 | if ( ! $this->version_selector->is_version_update_required( $latest_version, $version ) ) { |
|
| 41 | continue; |
|
| 42 | } |
|
| 43 | ||
| 44 | $latest_version = $version; |
|
| 45 | $latest_plugin = $plugin_path; |
|
| 46 | } |
|
| 47 | ||
| 48 | return $latest_plugin; |
|
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * Gets the path to the autoloader. |
|
| 53 | * |
|
| 54 | * @param string $plugin_path The path to the plugin. |
|
| 55 | * |
|
| 56 | * @return string |
|
| 57 | */ |
|
| 58 | public function get_autoloader_path( $plugin_path ) { |
|
| 59 | return trailingslashit( $plugin_path ) . 'vendor/autoload_packages.php'; |
|
| 60 | } |
|
| 61 | ||
| 62 | /** |
|
| 63 | * Gets the version for the autoloader. |
|
| 64 | * |
|
| 65 | * @param string $plugin_path The path to the plugin. |
|
| 66 | * |
|
| 67 | * @return string|null |
|
| 68 | */ |
|
| 69 | public function get_autoloader_version( $plugin_path ) { |
|
| 70 | $classmap = trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_classmap.php'; |
|
| 71 | if ( ! file_exists( $classmap ) ) { |
|
| 72 | return null; |
|
| 73 | } |
|
| 74 | ||
| 75 | $classmap = require $classmap; |
|
| 76 | if ( isset( $classmap[ AutoloadGenerator::class ] ) ) { |
|
| 77 | return $classmap[ AutoloadGenerator::class ]['version']; |
|
| 78 | } |
|
| 79 | ||
| 80 | return null; |
|
| 81 | } |
|
| 82 | } |
|
| 83 | ||
| @@ 17-90 (lines=74) @@ | ||
| 14 | /** |
|
| 15 | * This class locates autoloaders. |
|
| 16 | */ |
|
| 17 | class Autoloader_Locator { |
|
| 18 | ||
| 19 | /** |
|
| 20 | * The object for comparing autoloader versions. |
|
| 21 | * |
|
| 22 | * @var Version_Selector |
|
| 23 | */ |
|
| 24 | private $version_selector; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * The constructor. |
|
| 28 | * |
|
| 29 | * @param Version_Selector $version_selector The version selector object. |
|
| 30 | */ |
|
| 31 | public function __construct( $version_selector ) { |
|
| 32 | $this->version_selector = $version_selector; |
|
| 33 | } |
|
| 34 | ||
| 35 | /** |
|
| 36 | * Finds the path to the plugin with the latest autoloader. |
|
| 37 | * |
|
| 38 | * @param array $plugin_paths An array of plugin paths. |
|
| 39 | * @param string $latest_version The latest version reference. |
|
| 40 | * |
|
| 41 | * @return string|null |
|
| 42 | */ |
|
| 43 | public function find_latest_autoloader( $plugin_paths, &$latest_version ) { |
|
| 44 | $latest_plugin = null; |
|
| 45 | ||
| 46 | foreach ( $plugin_paths as $plugin_path ) { |
|
| 47 | $version = $this->get_autoloader_version( $plugin_path ); |
|
| 48 | if ( ! $this->version_selector->is_version_update_required( $latest_version, $version ) ) { |
|
| 49 | continue; |
|
| 50 | } |
|
| 51 | ||
| 52 | $latest_version = $version; |
|
| 53 | $latest_plugin = $plugin_path; |
|
| 54 | } |
|
| 55 | ||
| 56 | return $latest_plugin; |
|
| 57 | } |
|
| 58 | ||
| 59 | /** |
|
| 60 | * Gets the path to the autoloader. |
|
| 61 | * |
|
| 62 | * @param string $plugin_path The path to the plugin. |
|
| 63 | * |
|
| 64 | * @return string |
|
| 65 | */ |
|
| 66 | public function get_autoloader_path( $plugin_path ) { |
|
| 67 | return trailingslashit( $plugin_path ) . 'vendor/autoload_packages.php'; |
|
| 68 | } |
|
| 69 | ||
| 70 | /** |
|
| 71 | * Gets the version for the autoloader. |
|
| 72 | * |
|
| 73 | * @param string $plugin_path The path to the plugin. |
|
| 74 | * |
|
| 75 | * @return string|null |
|
| 76 | */ |
|
| 77 | public function get_autoloader_version( $plugin_path ) { |
|
| 78 | $classmap = trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_classmap.php'; |
|
| 79 | if ( ! file_exists( $classmap ) ) { |
|
| 80 | return null; |
|
| 81 | } |
|
| 82 | ||
| 83 | $classmap = require $classmap; |
|
| 84 | if ( isset( $classmap[ AutoloadGenerator::class ] ) ) { |
|
| 85 | return $classmap[ AutoloadGenerator::class ]['version']; |
|
| 86 | } |
|
| 87 | ||
| 88 | return null; |
|
| 89 | } |
|
| 90 | } |
|
| 91 | ||