-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModular.php
190 lines (171 loc) · 3.57 KB
/
Modular.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* Modular base class for a WordPress plugin or theme
*
* @author Mike Ems
* @package Mvied
*/
class Mvied_Modular extends Mvied_Base {
/**
* Module directory
*
* @var string
*/
protected $_module_directory;
/**
* Loaded Modules
*
* @var array
*/
protected $_modules = array();
/**
* Set Module Directory
*
* @param string $module_directory
* @return object $this
*/
public function setModuleDirectory( $module_directory ) {
$this->_module_directory = $module_directory;
return $this;
}
/**
* Get Module Directory
*
* @param none
* @return string
*/
public function getModuleDirectory() {
return $this->_module_directory;
}
/**
* Get Available Modules
*
* @param none
* @return array $modules
*/
public function getAvailableModules() {
$modules = array();
if ( is_dir($this->getModuleDirectory()) && $module_directory = opendir($this->getModuleDirectory()) ) {
while ( false !== ($entry = readdir($module_directory)) ) {
if ( strpos($entry, '.') !== 0 && strpos($entry, '.php') !== false ) {
$module = str_replace('.php', '', $entry);
if ( $module != 'Interface' ) {
$modules[] = $module;
if ( is_dir($this->getModuleDirectory() . $module) && $sub_module_directory = opendir($this->getModuleDirectory() . $module) ) {
while ( false !== ($entry = readdir($sub_module_directory)) ) {
if ( $entry != '.' && $entry != '..' ) {
$sub_module = str_replace('.php', '', $entry);
$modules[] = $module . '\\' . $sub_module;
}
}
}
}
}
}
}
return $modules;
}
/**
* Get Module
*
* @param string $module
* @return object
*/
public function getModule( $module ) {
$module = 'Module\\' . $module;
if ( isset($module) ) {
if ( isset($this->_modules[$module]) ) {
return $this->_modules[$module];
}
}
die('Module not found: \'' . $module . '\'.');
}
/**
* Get Modules
*
* Returns an array of all loaded modules
*
* @param none
* @return array $modules
*/
public function getModules() {
$modules = array();
if ( isset($this->_modules) ) {
$modules = $this->_modules;
}
return $modules;
}
/**
* Set Module
*
* @param string $module
* @param object $object
* @return $this
*/
public function setModule( $module, $object ) {
$this->_modules[$module] = $object;
return $this;
}
/**
* Init
*
* Initializes all of the modules.
*
* @param none
* @return $this
*/
public function init() {
$modules = $this->getModules();
foreach( $modules as $module ) {
$module->init();
}
if ( isset($this->_slug) ) {
do_action($this->_slug . '_init');
}
return $this;
}
/**
* Is Module Loaded?
*
* @param string $module
* @return boolean
*/
public function isModuleLoaded( $module ) {
if ( is_object($this->getModule($module)) ) {
return true;
} else {
return false;
}
}
/**
* Load Modules
*
* Load specified modules. If no modules are specified, all modules are loaded.
*
* @param array $modules
* @return $this
*/
public function loadModules( $modules = array() ) {
if ( sizeof($modules) == 0 ) {
$modules = $this->getAvailableModules();
}
foreach( $modules as $module ) {
$this->loadModule( $module );
}
return $this;
}
/**
* Unload Module
*
* @param string $module
* @return $this
*/
public function unloadModule( $module ) {
$base_class = get_class($this);
$module = 'Module\\' . $module;
$modules = $this->getModules();
unset($modules[$module]);
$this->_modules = $modules;
return $this;
}
}