1
|
|
|
<?php |
2
|
|
|
/* HEADER */ // phpcs:ignore |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This class manages the files and dependencies of the autoloader. |
6
|
|
|
*/ |
7
|
|
|
class Container { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Since each autoloader's class files exist within their own namespace we need a map to |
11
|
|
|
* convert between the local class and a shared key. Note that no version checking is |
12
|
|
|
* performed on these dependencies and the first autoloader to register will be the |
13
|
|
|
* one that is utilized. |
14
|
|
|
*/ |
15
|
|
|
const SHARED_DEPENDENCY_KEYS = array( |
16
|
|
|
Hook_Manager::class => 'Hook_Manager', |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A map of all the dependencies we've registered with the container and created. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $dependencies; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The constructor. |
28
|
|
|
*/ |
29
|
|
|
public function __construct() { |
30
|
|
|
$this->dependencies = array(); |
31
|
|
|
|
32
|
|
|
$this->register_shared_dependencies(); |
33
|
|
|
$this->register_dependencies(); |
34
|
|
|
$this->initialize_globals(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Gets a dependency out of the container. |
39
|
|
|
* |
40
|
|
|
* @param string $class The class to fetch. |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
* @throws \InvalidArgumentException When a class that isn't registered with the container is fetched. |
44
|
|
|
*/ |
45
|
|
|
public function get( $class ) { |
46
|
|
|
if ( ! isset( $this->dependencies[ $class ] ) ) { |
47
|
|
|
throw new \InvalidArgumentException( "Class '$class' is not registered with the container." ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->dependencies[ $class ]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Registers all of the dependencies that are shared between all instances of the autoloader. |
55
|
|
|
*/ |
56
|
|
|
private function register_shared_dependencies() { |
57
|
|
|
global $jetpack_autoloader_container_shared; |
58
|
|
|
if ( ! isset( $jetpack_autoloader_container_shared ) ) { |
59
|
|
|
$jetpack_autoloader_container_shared = array(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$key = self::SHARED_DEPENDENCY_KEYS[ Hook_Manager::class ]; |
63
|
|
|
if ( ! isset( $jetpack_autoloader_container_shared[ $key ] ) ) { |
64
|
|
|
require_once __DIR__ . '/class-hook-manager.php'; |
65
|
|
|
$jetpack_autoloader_container_shared[ $key ] = new Hook_Manager(); |
66
|
|
|
} |
67
|
|
|
$this->dependencies[ Hook_Manager::class ] = &$jetpack_autoloader_container_shared[ $key ]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Registers all of the dependencies with the container. |
72
|
|
|
*/ |
73
|
|
|
private function register_dependencies() { |
74
|
|
|
require_once __DIR__ . '/class-path-processor.php'; |
75
|
|
|
$this->dependencies[ Path_Processor::class ] = new Path_Processor(); |
76
|
|
|
|
77
|
|
|
require_once __DIR__ . '/class-plugin-locator.php'; |
78
|
|
|
$this->dependencies[ Plugin_Locator::class ] = new Plugin_Locator( |
79
|
|
|
$this->get( Path_Processor::class ) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
require_once __DIR__ . '/class-version-selector.php'; |
83
|
|
|
$this->dependencies[ Version_Selector::class ] = new Version_Selector(); |
84
|
|
|
|
85
|
|
|
require_once __DIR__ . '/class-autoloader-locator.php'; |
86
|
|
|
$this->dependencies[ Autoloader_Locator::class ] = new Autoloader_Locator( |
87
|
|
|
$this->get( Version_Selector::class ) |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
require_once __DIR__ . '/class-manifest-reader.php'; |
91
|
|
|
$this->dependencies[ Manifest_Reader::class ] = new Manifest_Reader( |
92
|
|
|
$this->get( Version_Selector::class ) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
require_once __DIR__ . '/class-plugins-handler.php'; |
96
|
|
|
$this->dependencies[ Plugins_Handler::class ] = new Plugins_Handler( |
97
|
|
|
$this->get( Plugin_Locator::class ), |
98
|
|
|
$this->get( Path_Processor::class ) |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
require_once __DIR__ . '/class-autoloader-handler.php'; |
102
|
|
|
$this->dependencies[ Autoloader_Handler::class ] = new Autoloader_Handler( |
103
|
|
|
$this->get( Hook_Manager::class ), |
104
|
|
|
$this->get( Manifest_Reader::class ), |
105
|
|
|
$this->get( Version_Selector::class ) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
require_once __DIR__ . '/class-latest-autoloader-guard.php'; |
109
|
|
|
$this->dependencies[ Latest_Autoloader_Guard::class ] = new Latest_Autoloader_Guard( |
110
|
|
|
$this->get( Plugins_Handler::class ), |
111
|
|
|
$this->get( Autoloader_Handler::class ), |
112
|
|
|
$this->get( Autoloader_Locator::class ) |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
// Register any classes that we will use elsewhere. |
116
|
|
|
require_once __DIR__ . '/class-version-loader.php'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Initializes any of the globals needed by the autoloader. |
121
|
|
|
*/ |
122
|
|
|
private function initialize_globals() { |
123
|
|
|
// Not all plugins can be found using the locator. In cases where a plugin loads the autoloader |
124
|
|
|
// but was not discoverable, we will record them in this array to track them as "active". |
125
|
|
|
global $jetpack_autoloader_activating_plugins_paths; |
126
|
|
|
if ( ! isset( $jetpack_autoloader_activating_plugins_paths ) ) { |
127
|
|
|
$jetpack_autoloader_activating_plugins_paths = array(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Since older autoloaders include newer ones, we need to be able to tell the difference between an |
131
|
|
|
// inclusion via autoloader and an inclusion via plugin file. This allows the autoloader to |
132
|
|
|
// perform special tasks for each kind of inclusion. |
133
|
|
|
global $jetpack_autoloader_including_latest; |
134
|
|
|
if ( ! isset( $jetpack_autoloader_including_latest ) ) { |
135
|
|
|
// If the latest version global has been set but the including latest hasn't, it means that an |
136
|
|
|
// older autoloader without support for the global is including us. |
137
|
|
|
global $jetpack_autoloader_latest_version; |
138
|
|
|
$jetpack_autoloader_including_latest = isset( $jetpack_autoloader_latest_version ); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|