Completed
Branch FET-9046-messages-queue (bdca24)
by
unknown
547:25 queued 529:16
created

EE_Dependency_Map   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2
Metric Value
wmc 15
lcom 2
cbo 2
dl 0
loc 180
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A instance() 0 7 2
A dependency_map() 0 3 1
A register_dependencies() 0 7 2
A class_loader() 0 3 2
A register_class_loader() 0 16 4
B _register_core_dependencies() 0 36 1
A _register_core_class_loaders() 0 17 1
A reset() 0 4 1
1
<?php
2
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
3
	exit( 'No direct script access allowed' );
4
}
5
6
7
8
/**
9
 * Class EE_Dependency_Map
10
 *
11
 * info about how to load classes required by other classes
12
 *
13
 * @package       Event Espresso
14
 * @subpackage    core
15
 * @author        Brent Christensen
16
 * @since         4.9.0
17
 *
18
 */
19
class EE_Dependency_Map {
20
21
22
	/**
23
	 * @type EE_Dependency_Map $_instance
24
	 */
25
	protected static $_instance = null;
26
27
	/**
28
	 * @type array $_dependency_map
29
	 */
30
	protected static $_dependency_map = array();
31
32
	/**
33
	 * @type array $_class_loaders
34
	 */
35
	protected static $_class_loaders = array();
36
37
38
39
	/**
40
	 * EE_Dependency_Map constructor.
41
	 */
42
	protected function __construct() {
43
		$this->_register_core_dependencies();
44
		$this->_register_core_class_loaders();
45
		do_action( 'EE_Dependency_Map____construct' );
46
	}
47
48
49
50
	/**
51
	 * @singleton method used to instantiate class object
52
	 * @access    public
53
	 * @return \EE_Dependency_Map instance
54
	 */
55
	public static function instance() {
56
		// check if class object is instantiated, and instantiated properly
57
		if ( ! self::$_instance instanceof EE_Dependency_Map ) {
58
			self::$_instance = new EE_Dependency_Map();
59
		}
60
		return self::$_instance;
61
	}
62
63
64
65
	/**
66
	 * @return array
67
	 */
68
	public static function dependency_map() {
69
		return self::$_dependency_map;
70
	}
71
72
73
74
	/**
75
	 * @param string $class
76
	 * @param array $dependencies
77
	 * @return boolean
78
	 */
79
	public static function register_dependencies( $class, $dependencies ) {
80
		if ( ! isset( self::$_dependency_map[ $class ] ) ) {
81
			self::$_dependency_map[ $class ] = (array)$dependencies;
82
			return true;
83
		}
84
		return false;
85
	}
86
87
88
89
	/**
90
	 * @param string $class_name
91
	 * @return array
92
	 */
93
	public static function class_loader( $class_name ) {
94
		return isset( self::$_class_loaders[ $class_name ] ) ? self::$_class_loaders[ $class_name ] : '';
95
	}
96
97
98
99
	/**
100
	 * @param string $class_name
101
	 * @param string $loader
102
	 * @return bool
103
	 * @throws \EE_Error
104
	 */
105
	public static function register_class_loader( $class_name, $loader = 'load_core' ) {
106
		// check that loader method starts with "load_" and exists in EE_Registry
107
		if ( strpos( $loader, 'load_' ) !== 0 || ! method_exists( EE_Registry::instance(), $loader ) ) {
108
			throw new EE_Error(
109
				sprintf(
110
					__( '"%1$s" is not a valid loader method on EE_Registry.', 'event_espresso' ),
111
					$loader
112
				)
113
			);
114
		}
115
		if ( ! isset( self::$_class_loaders[ $class_name ] ) ) {
116
			self::$_class_loaders[ $class_name ] = $loader;
117
			return true;
118
		}
119
		return false;
120
	}
121
122
123
124
	/**
125
	 * Registers the core dependencies
126
	 */
127
	protected function _register_core_dependencies() {
128
		self::$_dependency_map = array(
129
			'EE_Session' => array(
130
				'EE_Encryption'
131
			),
132
			'EE_Cart' => array(
133
				null,
134
				'EE_Session',
135
			),
136
			'EE_Front_Controller' => array(
137
				'EE_Registry',
138
				'EE_Request_Handler',
139
				'EE_Module_Request_Router',
140
			),
141
			'EE_Messenger_Collection_Loader' => array(
142
				'EE_Messenger_Collection',
143
			),
144
			'EE_Message_Type_Collection_Loader' => array(
145
				'EE_Message_Type_Collection',
146
			),
147
			'EE_Message_Resource_Manager' => array(
148
				'EE_Messenger_Collection_Loader',
149
				'EE_Message_Type_Collection_Loader',
150
				'EEM_Message_Template_Group',
151
			),
152
			'EE_Message_Factory' => array(
153
				'EE_Message_Resource_Manager',
154
			),
155
			'EE_Messages' => array(
156
				'EE_Message_Resource_Manager',
157
			),
158
			'EE_messages' => array(
159
				'EE_Message_Resource_Manager',
160
			)
161
		);
162
	}
163
164
165
166
	/**
167
	 * Registers the core class loaders.
168
	 */
169
	protected function _register_core_class_loaders() {
170
		self::$_class_loaders = array(
171
			//load_core
172
			'EE_Encryption' => 'load_core',
173
			'EE_Module_Request_Router' => 'load_core',
174
			'EE_Registry' => 'load_core',
175
			'EE_Request_Handler' => 'load_core',
176
			'EE_Session' => 'load_core',
177
			//load_lib
178
			'EE_Message_Type_Collection' => 'load_lib',
179
			'EE_Message_Resource_Manager' => 'load_lib',
180
			'EE_Message_Type_Collection_Loader', 'load_lib',
181
			'EE_Messenger_Collection' => 'load_lib',
182
			'EE_Messenger_Collection_Loader' => 'load_lib',
183
			'EEM_Message_Template_Group', 'load_lib'
184
		);
185
	}
186
187
188
	/**
189
	 * This is used to reset the internal map and class_loaders to their original default state at the beginning of the request
190
	 * Primarily used by unit tests.
191
	 */
192
	public function reset() {
193
		self::_register_core_class_loaders();
194
		self::_register_core_dependencies();
195
	}
196
197
198
}
199
// End of file EE_Dependency_Map.core.php
200
// Location: /EE_Dependency_Map.core.php