Completed
Push — update/form-newsletter-integra... ( b1c995...08366f )
by
unknown
36:07 queued 27:18
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 Method

Rating   Name   Duplication   Size   Complexity  
A Autoloader_Handler::__construct() 0 6 1
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
	/**
20
	 * The current plugin path.
21
	 *
22
	 * @var string
23
	 */
24
	private $current_plugin_path;
25
26
	/**
27
	 * The paths for all of the active plugins.
28
	 *
29
	 * @var array
30
	 */
31
	private $active_plugin_paths;
32
33
	/**
34
	 * The Autoloader_Locator object.
35
	 *
36
	 * @var Autoloader_Locator
37
	 */
38
	private $autoloader_locator;
39
40
	/**
41
	 * The Version_Selector object.
42
	 *
43
	 * @var Version_Selector
44
	 */
45
	private $version_selector;
46
47
	/**
48
	 * The constructor.
49
	 *
50
	 * @param string             $current_plugin_path The current plugin path.
51
	 * @param array              $active_plugin_paths The active plugin paths.
52
	 * @param Autoloader_Locator $autoloader_locator The Autoloader_Locator object.
53
	 * @param Version_Selector   $version_selector The Version_Selector object.
54
	 */
55
	public function __construct( $current_plugin_path, $active_plugin_paths, $autoloader_locator, $version_selector ) {
56
		$this->current_plugin_path = $current_plugin_path;
57
		$this->active_plugin_paths = $active_plugin_paths;
58
		$this->autoloader_locator  = $autoloader_locator;
59
		$this->version_selector    = $version_selector;
60
	}
61
62
	/**
63
	 * Finds the latest installed autoloader.
64
	 *
65
	 * @return bool True if this autoloader is the latest, false otherwise.
66
	 */
67
	public function is_latest_autoloader() {
68
		global $jetpack_autoloader_latest_version;
69
70
		if ( isset( $jetpack_autoloader_latest_version ) ) {
71
			return $jetpack_autoloader_latest_version === $this->autoloader_locator->get_autoloader_version( $this->current_plugin_path );
72
		}
73
74
		$latest_plugin = $this->autoloader_locator->find_latest_autoloader( $this->active_plugin_paths, $jetpack_autoloader_latest_version );
75
		if ( ! isset( $latest_plugin ) ) {
76
			return true;
77
		}
78
79
		if ( $latest_plugin !== $this->current_plugin_path ) {
80
			require $this->autoloader_locator->get_autoloader_path( $latest_plugin );
81
			return false;
82
		}
83
84
		return true;
85
	}
86
87
	/**
88
	 * Checks whether the autoloader should be reset. The autoloader should be reset:
89
	 *
90
	 *  - When a plugin is activated via a method other than a request, for example using WP-CLI.
91
	 *  - When the active plugins list changes between autoloader checks, for example when filtered by a plugin.
92
	 *
93
	 * We perform this reset because the manifest files for the plugin will have been initially unknown when
94
	 * selecting versions for classes and files.
95
	 *
96
	 * If the current plugin is not already known, this method will add it to the
97
	 * $jetpack_autoloader_activating_plugins_paths global.
98
	 * The $jetpack_autoloader_cached_plugin_paths global will store a cache of the
99
	 * active plugin paths when last changed.
100
	 *
101
	 * @return boolean True if the autoloader must be reset, else false.
102
	 */
103
	public function should_autoloader_reset() {
104
		global $jetpack_autoloader_activating_plugins_paths;
105
		global $jetpack_autoloader_cached_plugin_paths;
106
107
		$plugin_unknown = ! in_array( $this->current_plugin_path, $this->active_plugin_paths, true );
108
		if ( $plugin_unknown ) {
109
			if ( ! isset( $jetpack_autoloader_activating_plugins_paths ) ) {
110
				$jetpack_autoloader_activating_plugins_paths = array();
111
			}
112
113
			// If the current plugin isn't known, add it to the activating plugins list.
114
			$jetpack_autoloader_activating_plugins_paths[] = $this->current_plugin_path;
115
			$this->active_plugin_paths[]                   = $this->current_plugin_path;
116
		}
117
118
		$cache_invalidated = $jetpack_autoloader_cached_plugin_paths !== $this->active_plugin_paths;
119
		if ( $cache_invalidated ) {
120
			$jetpack_autoloader_cached_plugin_paths = $this->active_plugin_paths;
121
		}
122
123
		return $plugin_unknown || $cache_invalidated;
124
	}
125
126
	/**
127
	 * Builds the Version_Autoloader class that is used for autoloading.
128
	 *
129
	 * @return Version_Loader
130
	 */
131
	public function build_autoloader() {
132
		$manifest_handler = new Manifest_Handler( $this->active_plugin_paths, $this->version_selector );
133
134
		global $jetpack_packages_psr4;
135
		$jetpack_packages_psr4 = array();
136
		$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_psr4.php', $jetpack_packages_psr4 );
137
138
		global $jetpack_packages_classmap;
139
		$jetpack_packages_classmap = array();
140
		$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $jetpack_packages_classmap );
141
142
		global $jetpack_packages_filemap;
143
		$jetpack_packages_filemap = array();
144
		$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_filemap.php', $jetpack_packages_filemap );
145
146
		// Store the generated autoloader data in the loader so we can use it.
147
		return new Version_Loader(
148
			$this->version_selector,
149
			$jetpack_packages_classmap,
150
			$jetpack_packages_psr4,
151
			$jetpack_packages_filemap
152
		);
153
	}
154
155
	/**
156
	 * Updates the spl autoloader chain:
157
	 *  - Registers this namespace's autoloader function.
158
	 *  - If a v1 autoloader function is registered, moves it to the end of the chain.
159
	 *  - Removes any other v2 autoloader functions that have already been registered. This
160
	 *    can occur when the autoloader is being reset by an activating plugin.
161
	 */
162
	public function update_autoloader_chain() {
163
		spl_autoload_register( __NAMESPACE__ . '\autoloader' );
164
165
		$autoload_chain = spl_autoload_functions();
166
167
		foreach ( $autoload_chain as $autoloader ) {
168
			if ( ! is_string( $autoloader ) ) {
169
				/*
170
				 * The Jetpack Autoloader functions are registered as strings, so
171
				 * just continue if $autoloader isn't a string.
172
				 */
173
				continue;
174
			}
175
176
			if ( self::V1_AUTOLOADER_NAME === $autoloader ) {
177
				// Move the v1.* autoloader function to the end of the spl autoloader chain.
178
				spl_autoload_unregister( $autoloader );
179
				spl_autoload_register( $autoloader );
180
181
			} elseif (
182
				self::V2_AUTOLOADER_BASE === substr( $autoloader, 0, strlen( self::V2_AUTOLOADER_BASE ) )
183
				&& __NAMESPACE__ !== substr( $autoloader, 0, strlen( __NAMESPACE__ ) )
184
			) {
185
				// Unregister any other v2.* autoloader functions if they're in the chain.
186
				spl_autoload_unregister( $autoloader );
187
			}
188
		}
189
	}
190
}
191