Completed
Push — update/node-12191 ( 8875a2...f69c39 )
by
unknown
147:00 queued 137:34
created

Autoloader_Handler::update_autoloader_chain()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 0
dl 0
loc 28
rs 8.8497
c 0
b 0
f 0
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\jp02f167abb8e9ed34472e5bd963af3d06;
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