Completed
Push — renovate/commander-5.x ( da542f...98aca7 )
by
unknown
169:19 queued 151:48
created

Autoloader_Handler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

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