Completed
Push — update/show-recurring-payments... ( 106a5e...b030b6 )
by
unknown
415:44 queued 407:29
created

Latest_Autoloader_Guard::should_stop_init()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 12
nop 2
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class ensures that we're only executing the latest autoloader.
6
 */
7
class Latest_Autoloader_Guard {
8
9
	/**
10
	 * The Plugins_Handler instance.
11
	 *
12
	 * @var Plugins_Handler
13
	 */
14
	private $plugins_handler;
15
16
	/**
17
	 * The Autoloader_Handler instance.
18
	 *
19
	 * @var Autoloader_Handler
20
	 */
21
	private $autoloader_handler;
22
23
	/**
24
	 * The Autoloader_locator instance.
25
	 *
26
	 * @var Autoloader_Locator
27
	 */
28
	private $autoloader_locator;
29
30
	/**
31
	 * The constructor.
32
	 *
33
	 * @param Plugins_Handler    $plugins_handler    The Plugins_Handler instance.
34
	 * @param Autoloader_Handler $autoloader_handler The Autoloader_Handler instance.
35
	 * @param Autoloader_Locator $autoloader_locator The Autoloader_Locator instance.
36
	 */
37
	public function __construct( $plugins_handler, $autoloader_handler, $autoloader_locator ) {
38
		$this->plugins_handler    = $plugins_handler;
39
		$this->autoloader_handler = $autoloader_handler;
40
		$this->autoloader_locator = $autoloader_locator;
41
	}
42
43
	/**
44
	 * Indicates whether or not the autoloader should be initialized. Note that this function
45
	 * has the side-effect of actually loading the latest autoloader in the event that this
46
	 * is not it.
47
	 *
48
	 * @param string   $current_plugin The current plugin we're checking.
49
	 * @param string[] $plugins        The active plugins to check for autoloaders in.
50
	 *
51
	 * @return bool True if we should stop initialization, otherwise false.
52
	 */
53
	public function should_stop_init( $current_plugin, $plugins ) {
54
		global $jetpack_autoloader_including_latest;
55
		global $jetpack_autoloader_latest_version;
56
57
		// When we're being included from an older autoloader we need to
58
		// reset the latest version so that the new autoloader can look
59
		// for the latest autoloader again.
60
		if ( $jetpack_autoloader_including_latest ) {
61
			$jetpack_autoloader_latest_version = null;
62
		}
63
64
		// We need to reset the autoloader when the plugins change because
65
		// that means the autoloader was generated with a different list.
66
		if ( $this->plugins_handler->have_plugins_changed( $plugins ) ) {
67
			$this->autoloader_handler->reset_autoloader();
68
		}
69
70
		// Don't bother initializing the autoloader if it already has been.
71
		if ( isset( $jetpack_autoloader_latest_version ) ) {
72
			return true;
73
		}
74
75
		$latest_plugin = $this->autoloader_locator->find_latest_autoloader( $plugins, $jetpack_autoloader_latest_version );
76
		if ( isset( $latest_plugin ) && $latest_plugin !== $current_plugin ) {
77
			$jetpack_autoloader_including_latest = true;
78
			require $this->autoloader_locator->get_autoloader_path( $latest_plugin );
79
			$jetpack_autoloader_including_latest = false;
80
			return true;
81
		}
82
83
		return false;
84
	}
85
}
86