Completed
Push — fix/useBlockEditContext-usage ( 1e6b8b...d10938 )
by
unknown
199:36 queued 191:34
created

Autoloader_Handler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B find_latest_autoloader() 0 37 6
A get_current_autoloader_version() 0 6 1
B update_autoloader_chain() 0 28 6
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
54
		$selected_autoloader_version = null;
55
		$selected_autoloader_path    = null;
56
57
		$active_plugins = $this->plugins_handler->get_all_active_plugins();
58
59
		foreach ( $active_plugins as $plugin ) {
60
			$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . $plugin;
61
62
			if ( ! file_exists( $plugin_path ) ) {
63
				$plugin_path = trailingslashit( WPMU_PLUGIN_DIR ) . $plugin;
64
			}
65
66
			$classmap_path = trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_classmap.php';
67
68
			if ( file_exists( $classmap_path ) ) {
69
				$packages = require $classmap_path;
70
71
				$compare_version = $packages[ self::AUTOLOAD_GENERATOR_CLASS_NAME ]['version'];
72
				$compare_path    = trailingslashit( $plugin_path ) . 'vendor/autoload_packages.php';
73
74
				if ( $this->version_selector->is_version_update_required( $selected_autoloader_version, $compare_version ) ) {
75
					$selected_autoloader_version = $compare_version;
76
					$selected_autoloader_path    = $compare_path;
77
				}
78
			}
79
		}
80
81
		$jetpack_autoloader_latest_version = $selected_autoloader_version;
82
		if ( $current_autoloader_path !== $selected_autoloader_path ) {
83
			require $selected_autoloader_path;
84
		}
85
	}
86
87
	/**
88
	 * Get this autoloader's package version.
89
	 *
90
	 * @return String The autoloader's package version.
91
	 */
92
	public function get_current_autoloader_version() {
93
		$classmap_file       = trailingslashit( dirname( __FILE__ ) ) . 'composer/jetpack_autoload_classmap.php';
94
		$autoloader_packages = require $classmap_file;
95
96
		return $autoloader_packages[ self::AUTOLOAD_GENERATOR_CLASS_NAME ]['version'];
97
	}
98
99
100
	/**
101
	 * Updates the spl autoloader chain:
102
	 *  - Registers this namespace's autoloader function.
103
	 *  - If a v1 autoloader function is registered, moves it to the end of the chain.
104
	 *  - Removes any other v2 autoloader functions that have already been registered. This
105
	 *    can occur when the autoloader is being reset by an activating plugin.
106
	 */
107
	public function update_autoloader_chain() {
108
		spl_autoload_register( __NAMESPACE__ . '\autoloader' );
109
110
		$autoload_chain = spl_autoload_functions();
111
112
		foreach ( $autoload_chain as $autoloader ) {
113
			if ( ! is_string( $autoloader ) ) {
114
				/*
115
				 * The Jetpack Autoloader functions are registered as strings, so
116
				 * just continue if $autoloader isn't a string.
117
				 */
118
				continue;
119
			}
120
121
			if ( self::V1_AUTOLOADER_NAME === $autoloader ) {
122
				// Move the v1.* autoloader function to the end of the spl autoloader chain.
123
				spl_autoload_unregister( $autoloader );
124
				spl_autoload_register( $autoloader );
125
126
			} elseif (
127
				self::V2_AUTOLOADER_BASE === substr( $autoloader, 0, strlen( self::V2_AUTOLOADER_BASE ) )
128
				&& __NAMESPACE__ !== substr( $autoloader, 0, strlen( __NAMESPACE__ ) )
129
			) {
130
				// Unregister any other v2.* autoloader functions if they're in the chain.
131
				spl_autoload_unregister( $autoloader );
132
			}
133
		}
134
	}
135
}
136