Completed
Push — add/jetpack-assistant-ui ( a6f776...33ce41 )
by Jeremy
202:03 queued 191:10
created

Autoloader_Handler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A activate_autoloader() 0 25 1
A reset_autoloader() 0 17 1
B is_initializing() 0 28 6
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
use Automattic\Jetpack\Autoloader\AutoloadGenerator;
5
6
/**
7
 * This class selects the package version for the autoloader.
8
 */
9
class Autoloader_Handler {
10
11
	/**
12
	 * The PHP_Autoloader instance.
13
	 *
14
	 * @var PHP_Autoloader
15
	 */
16
	private $php_autoloader;
17
18
	/**
19
	 * The Hook_Manager instance.
20
	 *
21
	 * @var Hook_Manager
22
	 */
23
	private $hook_manager;
24
25
	/**
26
	 * The Manifest_Reader instance.
27
	 *
28
	 * @var Manifest_Reader
29
	 */
30
	private $manifest_reader;
31
32
	/**
33
	 * The Version_Selector instance.
34
	 *
35
	 * @var Version_Selector
36
	 */
37
	private $version_selector;
38
39
	/**
40
	 * The constructor.
41
	 *
42
	 * @param PHP_Autoloader   $php_autoloader The PHP_Autoloader instance.
43
	 * @param Hook_Manager     $hook_manager The Hook_Manager instance.
44
	 * @param Manifest_Reader  $manifest_reader The Manifest_Reader instance.
45
	 * @param Version_Selector $version_selector The Version_Selector instance.
46
	 */
47
	public function __construct( $php_autoloader, $hook_manager, $manifest_reader, $version_selector ) {
48
		$this->php_autoloader   = $php_autoloader;
49
		$this->hook_manager     = $hook_manager;
50
		$this->manifest_reader  = $manifest_reader;
51
		$this->version_selector = $version_selector;
52
	}
53
54
	/**
55
	 * Checks to see whether or not an autoloader is currently in the process of initializing.
56
	 *
57
	 * @return bool
58
	 */
59
	public function is_initializing() {
60
		// If no version has been set it means that no autoloader has started initializing yet.
61
		global $jetpack_autoloader_latest_version;
62
		if ( ! isset( $jetpack_autoloader_latest_version ) ) {
63
			return false;
64
		}
65
66
		// When the version is set but the classmap is not it ALWAYS means that this is the
67
		// latest autoloader and is being included by an older one.
68
		global $jetpack_packages_classmap;
69
		if ( empty( $jetpack_packages_classmap ) ) {
70
			return true;
71
		}
72
73
		// Version 2.4.0 added a new global and altered the reset semantics. We need to check
74
		// the other global as well since it may also point at initialization.
75
		// Note: We don't need to check for the class first because every autoloader that
76
		// will set the latest version global requires this class in the classmap.
77
		$replacing_version = $jetpack_packages_classmap[ AutoloadGenerator::class ]['version'];
78
		if ( $this->version_selector->is_dev_version( $replacing_version ) || version_compare( $replacing_version, '2.4.0.0', '>=' ) ) {
79
			global $jetpack_autoloader_loader;
80
			if ( ! isset( $jetpack_autoloader_loader ) ) {
81
				return true;
82
			}
83
		}
84
85
		return false;
86
	}
87
88
	/**
89
	 * Activates an autoloader using the given plugins and activates it.
90
	 *
91
	 * @param string[] $plugins The plugins to initialize the autoloader for.
92
	 */
93
	public function activate_autoloader( $plugins ) {
94
		global $jetpack_packages_psr4;
95
		$jetpack_packages_psr4 = array();
96
		$this->manifest_reader->read_manifests( $plugins, 'vendor/composer/jetpack_autoload_psr4.php', $jetpack_packages_psr4 );
97
98
		global $jetpack_packages_classmap;
99
		$jetpack_packages_classmap = array();
100
		$this->manifest_reader->read_manifests( $plugins, 'vendor/composer/jetpack_autoload_classmap.php', $jetpack_packages_classmap );
101
102
		global $jetpack_packages_filemap;
103
		$jetpack_packages_filemap = array();
104
		$this->manifest_reader->read_manifests( $plugins, 'vendor/composer/jetpack_autoload_filemap.php', $jetpack_packages_filemap );
105
106
		$loader = new Version_Loader(
107
			$this->version_selector,
108
			$jetpack_packages_classmap,
109
			$jetpack_packages_psr4,
110
			$jetpack_packages_filemap
111
		);
112
113
		$this->php_autoloader->register_autoloader( $loader );
114
115
		// Now that the autoloader is active we can load the filemap.
116
		$loader->load_filemap();
117
	}
118
119
	/**
120
	 * Resets the active autoloader and all related global state.
121
	 */
122
	public function reset_autoloader() {
123
		$this->php_autoloader->unregister_autoloader();
124
		$this->hook_manager->reset();
125
126
		// Clear all of the autoloader globals so that older autoloaders don't do anything strange.
127
		global $jetpack_autoloader_latest_version;
128
		$jetpack_autoloader_latest_version = null;
129
130
		global $jetpack_packages_classmap;
131
		$jetpack_packages_classmap = array(); // Must be array to avoid exceptions in old autoloaders!
132
133
		global $jetpack_packages_psr4;
134
		$jetpack_packages_psr4 = array(); // Must be array to avoid exceptions in old autoloaders!
135
136
		global $jetpack_packages_filemap;
137
		$jetpack_packages_filemap = array(); // Must be array to avoid exceptions in old autoloaders!
138
	}
139
}
140