-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhap.module
238 lines (195 loc) · 6.03 KB
/
hap.module
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* @file
* hap.module
*/
define('HAP_API_VERSION', '1.0');
include_once('hap.hooks.inc');
/**
* Implements hook_ctools_plugin_directory().
*/
function hap_ctools_plugin_directory($owner, $api) {
if (in_array($owner, array('ctools', 'panels', 'views'))) {
return "plugins/$owner/$api";
}
}
/**
* Implements hook_views_api().
*/
function hap_views_api() {
return array(
'api' => '3',
'path' => drupal_get_path('module', 'hap') . '/plugins/views',
);
}
/******************************************************************************
* Utility functions you should know about.
*****************************************************************************/
/**
* Includes a specified "hap" include file. If called without parameters, it
* will only include core.inc. The core.inc will always be included, regardless
* of what tool is specified.
*
* @param $file
* The machine name of the tool to include.
*/
function hap_include($file = NULL) {
// We must statically cache which files have already been loaded since PHP is
// very slow at managing this itself.
static $core, $files;
// core.inc is always included.
if (!$core) {
require_once DRUPAL_ROOT . '/' . hap_path() . "/core.inc";
$files['core'] = TRUE;
$core = TRUE;
}
if ($file && !isset($files[$file])) {
$path = DRUPAL_ROOT . '/' . hap_path() . "/$file.inc";
// Make sure a file exists before trying to include it, but still track that
// we at least looked.
if (file_exists($path)) {
require_once $path;
}
else {
watchdog('hap', 'The file %file.inc does not exist in Tools for Highly Attractive People.', array('%file' => $file), WATCHDOG_WARNING);
}
$files[$file] = TRUE;
}
}
/**
* Enable tools. Wrapper for hap_installer_status_set().
*
* @param $tool
* The name of the tool to enable. Can also be an array of tools.
*/
function hap_turn_on($tools) {
hap_include('installer');
hap_installer_status_set($tools, TRUE);
}
/**
* Disable tools. Wrapper for hap_installer_status_set().
*
* @param $tools
* The name of the tool to disable. Can also be an array of tools.
*/
function hap_turn_off($tools) {
hap_include('installer');
hap_installer_status_set($tools, FALSE);
}
/**
* Get the path of a specified directory in the hap project. It is
* recommended to use this instead of calling drupal_get_path() directly.
*
* @param $dir
* The name of the directory specified.
*
* @return
* The drupal path to the specified directory.
*/
function hap_path($dir = 'includes') {
// We statically cache these results so we don't have to execute
// drupal_get_path() multiple times for the same path during a page request.
// This shaves off approximately 75% of execution time.
static $paths;
if (!isset($paths[$dir])) {
$paths[$dir] = drupal_get_path('module', 'hap') . "/$dir";
}
return $paths[$dir];
}
/**
* Get information registered by each tool. Warning: this will load all tools.
* Note that this does not represent all tools available, but just the tools
* that require implementing drupal hooks.
*/
function hap_info() {
$keys = &drupal_static(__FUNCTION__);
if (empty($keys)) {
$dir = hap_path();
$mask = '/\w+.inc$/';
$options['recurse'] = FALSE;
$options['key'] = 'name';
$files = file_scan_directory($dir, $mask, $options);
$keys = array();
$defaults = array(
'core' => DRUPAL_CORE_COMPATIBILITY,
'version' => DRUPAL_CORE_COMPATIBILITY . '-' . HAP_API_VERSION,
'php' => DRUPAL_MINIMUM_PHP,
'always enabled' => FALSE,
'hooks' => array(),
'dependencies' => array(
'modules' => array('hap'),
'tools' => array(),
),
);
// Load all include files so we can invoke hook_hap_info() on each of them.
foreach (array_keys($files) as $file) {
hap_include($file);
$function = 'hap_' . $file . '_hap_info';
if (function_exists($function)) {
$keys[$file] = drupal_array_merge_deep($defaults, $function());
}
}
}
return $keys;
}
/**
* Returns an array of all tools' status. Keyed by tool's machine name.
*/
function hap_list() {
if (!($enabled = variable_get('hap_enabled', NULL))) {
hap_include('installer');
$enabled = hap_installer_enabled_revert_to_default();
}
return $enabled;
}
/**
* Kind of a big deal for implementing drupal hooks from within tools.
*
* This function allows each tool to implement their OWN hooks from within
* their respective include files, and not clutter the .module file or
* intermingle with other tools.
*
* @param $hook
* The name of the hook we are invoking. Not including the 'hook_' part.
*
* @param $arg1, $arg2, etc..
* Arguments to pass into the hook.
*/
function hap_proxy($hook, &$arg1 = NULL, &$arg2 = NULL, &$arg3 = NULL, &$arg4 = NULL, &$arg5 = NULL) {
static $hooks, $included_tools;
$return = array();
if (!$hooks) {
$hooks = variable_get('hap_hooks', array());
}
foreach ($hooks[$hook] as $tool) {
// Include implementing tools and track whether it's already included.
// Although include_once will already track this, we do it just to avoid
// calling hap_include() each time this function is ran.
// @todo test if we really still need this since hap_include() caches
// this already.
if (!isset($included_tools[$tool])) {
hap_include($tool);
$included_tools[$tool] = TRUE;
}
$function = 'hap_' . $tool . '_' . $hook;
if (function_exists($function)) {
$result = $function($arg1, $arg2, $arg3, $arg4, $arg5);
}
// @fixme this is an aweful approach to arbitrary number of arguments for
// hook implementations.
if (isset($result) && is_array($result)) {
$return = array_merge_recursive($return, $result);
}
elseif (isset($result)) {
$return[] = $result;
}
}
return $return;
}
/**
* Simple conditional wrapper to determine if a hook is utilized in currently
* enabled tools.
*/
function hap_uses_hook($hook) {
return in_array($hook, variable_get('hap_hooks_used', array()));
}