Completed
Push — fix/idc-same-heal ( 5f98ee...a3787f )
by
unknown
08:15
created

Autoloader_Handler::find_latest_autoloader()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

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