Completed
Push — add/engagement-search-results-... ( e442e5...0f247d )
by
unknown
23:53 queued 10:20
created

Autoloader_Handler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
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
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
			$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
		if ( $current_autoloader_path !== $selected_autoloader_path ) {
78
			require $selected_autoloader_path;
79
		}
80
	}
81
82
	/**
83
	 * Get this autoloader's package version.
84
	 *
85
	 * @return String The autoloader's package version.
86
	 */
87
	public function get_current_autoloader_version() {
88
		$classmap_file       = trailingslashit( dirname( __FILE__ ) ) . 'composer/jetpack_autoload_classmap.php';
89
		$autoloader_packages = require $classmap_file;
90
91
		return $autoloader_packages[ self::AUTOLOAD_GENERATOR_CLASS_NAME ]['version'];
92
	}
93
94
95
	/**
96
	 * Updates the spl autoloader chain:
97
	 *  - Registers this namespace's autoloader function.
98
	 *  - If a v1 autoloader function is registered, moves it to the end of the chain.
99
	 *  - Removes any other v2 autoloader functions that have already been registered. This
100
	 *    can occur when the autoloader is being reset by an activating plugin.
101
	 */
102
	public function update_autoloader_chain() {
103
		spl_autoload_register( __NAMESPACE__ . '\autoloader' );
104
105
		$autoload_chain = spl_autoload_functions();
106
107
		foreach ( $autoload_chain as $autoloader ) {
108
			if ( ! is_string( $autoloader ) ) {
109
				/*
110
				 * The Jetpack Autoloader functions are registered as strings, so
111
				 * just continue if $autoloader isn't a string.
112
				 */
113
				continue;
114
			}
115
116
			if ( self::V1_AUTOLOADER_NAME === $autoloader ) {
117
				// Move the v1.* autoloader function to the end of the spl autoloader chain.
118
				spl_autoload_unregister( $autoloader );
119
				spl_autoload_register( $autoloader );
120
121
			} elseif (
122
				self::V2_AUTOLOADER_BASE === substr( $autoloader, 0, strlen( self::V2_AUTOLOADER_BASE ) )
123
				&& __NAMESPACE__ !== substr( $autoloader, 0, strlen( __NAMESPACE__ ) )
124
			) {
125
				// Unregister any other v2.* autoloader functions if they're in the chain.
126
				spl_autoload_unregister( $autoloader );
127
			}
128
		}
129
	}
130
}
131