automattic
5 years ago
bin
5 years ago
composer
5 years ago
jetpack-autoloader
5 years ago
league
5 years ago
maxmind-db
5 years ago
pelago
6 years ago
psr
5 years ago
symfony
5 years ago
woocommerce
5 years ago
autoload.php
5 years ago
autoload_packages.php
5 years ago
class-autoloader-handler.php
5 years ago
class-classes-handler.php
5 years ago
class-files-handler.php
5 years ago
class-plugins-handler.php
5 years ago
class-version-selector.php
5 years ago
class-autoloader-handler.php
141 lines
| 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\jp5e9090adb7e03a3eeba3759a946171f6; |
| 9 | |
| 10 | // phpcs:ignore |
| 11 | |
| 12 | /** |
| 13 | * This class selects the package version for the autoloader. |
| 14 | */ |
| 15 | class Autoloader_Handler { |
| 16 | |
| 17 | // The name of the autoloader function registered by v1.* autoloaders. |
| 18 | const V1_AUTOLOADER_NAME = 'Automattic\Jetpack\Autoloader\autoloader'; |
| 19 | |
| 20 | /* |
| 21 | * The autoloader function for v2.* autoloaders is named __NAMESPACE__ . \autoloader. |
| 22 | * The namespace is defined in AutoloadGenerator as |
| 23 | * 'Automattic\Jetpack\Autoloader\jp' plus a unique suffix. |
| 24 | */ |
| 25 | const V2_AUTOLOADER_BASE = 'Automattic\Jetpack\Autoloader\jp'; |
| 26 | |
| 27 | const AUTOLOAD_GENERATOR_CLASS_NAME = 'Automattic\Jetpack\Autoloader\AutoloadGenerator'; |
| 28 | |
| 29 | /** |
| 30 | * The Plugins_Handler object. |
| 31 | * |
| 32 | * @var Plugins_Handler |
| 33 | */ |
| 34 | private $plugins_handler = null; |
| 35 | |
| 36 | /** |
| 37 | * The Version_Selector object. |
| 38 | * |
| 39 | * @var Version_Selector |
| 40 | */ |
| 41 | private $version_selector = null; |
| 42 | |
| 43 | /** |
| 44 | * The constructor. |
| 45 | * |
| 46 | * @param Plugins_Handler $plugins_handler The Plugins_Handler object. |
| 47 | * @param Version_Selector $version_selector The Version_Selector object. |
| 48 | */ |
| 49 | public function __construct( $plugins_handler, $version_selector ) { |
| 50 | $this->plugins_handler = $plugins_handler; |
| 51 | $this->version_selector = $version_selector; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Finds the latest installed autoloader. |
| 56 | */ |
| 57 | public function find_latest_autoloader() { |
| 58 | global $jetpack_autoloader_latest_version; |
| 59 | |
| 60 | $current_autoloader_path = trailingslashit( dirname( __FILE__ ) ) . 'autoload_packages.php'; |
| 61 | $current_autoloader_path = str_replace( '\\', '/', $current_autoloader_path ); |
| 62 | |
| 63 | $selected_autoloader_version = null; |
| 64 | $selected_autoloader_path = null; |
| 65 | |
| 66 | $active_plugins_paths = $this->plugins_handler->get_all_active_plugins_paths(); |
| 67 | |
| 68 | foreach ( $active_plugins_paths as $plugin_path ) { |
| 69 | $classmap_path = trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_classmap.php'; |
| 70 | |
| 71 | if ( file_exists( $classmap_path ) ) { |
| 72 | $packages = require $classmap_path; |
| 73 | |
| 74 | $compare_version = $packages[ self::AUTOLOAD_GENERATOR_CLASS_NAME ]['version']; |
| 75 | $compare_path = trailingslashit( $plugin_path ) . 'vendor/autoload_packages.php'; |
| 76 | |
| 77 | if ( $this->version_selector->is_version_update_required( $selected_autoloader_version, $compare_version ) ) { |
| 78 | $selected_autoloader_version = $compare_version; |
| 79 | $selected_autoloader_path = $compare_path; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | $jetpack_autoloader_latest_version = $selected_autoloader_version; |
| 85 | |
| 86 | // $current_autoloader_path is already loaded |
| 87 | if ( $current_autoloader_path !== $selected_autoloader_path ) { |
| 88 | require $selected_autoloader_path; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get this autoloader's package version. |
| 94 | * |
| 95 | * @return String The autoloader's package version. |
| 96 | */ |
| 97 | public function get_current_autoloader_version() { |
| 98 | $classmap_file = trailingslashit( dirname( __FILE__ ) ) . 'composer/jetpack_autoload_classmap.php'; |
| 99 | $autoloader_packages = require $classmap_file; |
| 100 | |
| 101 | return $autoloader_packages[ self::AUTOLOAD_GENERATOR_CLASS_NAME ]['version']; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Updates the spl autoloader chain: |
| 107 | * - Registers this namespace's autoloader function. |
| 108 | * - If a v1 autoloader function is registered, moves it to the end of the chain. |
| 109 | * - Removes any other v2 autoloader functions that have already been registered. This |
| 110 | * can occur when the autoloader is being reset by an activating plugin. |
| 111 | */ |
| 112 | public function update_autoloader_chain() { |
| 113 | spl_autoload_register( __NAMESPACE__ . '\autoloader' ); |
| 114 | |
| 115 | $autoload_chain = spl_autoload_functions(); |
| 116 | |
| 117 | foreach ( $autoload_chain as $autoloader ) { |
| 118 | if ( ! is_string( $autoloader ) ) { |
| 119 | /* |
| 120 | * The Jetpack Autoloader functions are registered as strings, so |
| 121 | * just continue if $autoloader isn't a string. |
| 122 | */ |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | if ( self::V1_AUTOLOADER_NAME === $autoloader ) { |
| 127 | // Move the v1.* autoloader function to the end of the spl autoloader chain. |
| 128 | spl_autoload_unregister( $autoloader ); |
| 129 | spl_autoload_register( $autoloader ); |
| 130 | |
| 131 | } elseif ( |
| 132 | self::V2_AUTOLOADER_BASE === substr( $autoloader, 0, strlen( self::V2_AUTOLOADER_BASE ) ) |
| 133 | && __NAMESPACE__ !== substr( $autoloader, 0, strlen( __NAMESPACE__ ) ) |
| 134 | ) { |
| 135 | // Unregister any other v2.* autoloader functions if they're in the chain. |
| 136 | spl_autoload_unregister( $autoloader ); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 |