-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrampoline.module
More file actions
186 lines (159 loc) · 6.23 KB
/
trampoline.module
File metadata and controls
186 lines (159 loc) · 6.23 KB
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
<?php
/**
* @file
* Trampoline module to facilitate AJAX callbacks without a full Drupal bootstrap.
*/
/**
* Implementation of hook_form_FORM_ID_alter().
*
* Add a reset button to the performance page.
*/
function trampoline_form_system_performance_settings_alter(&$form, $form_state) {
$form['trampoline'] = array(
'#type' => 'fieldset',
'#title' => t('Trampoline AJAX booster'),
'#description' => t("Trampoline allows modules to invoke AJAX callbacks without requiring a full Drupal bootstrap for the callback."),
);
$form['trampoline']['trampoline_flush'] = array(
'#type' => 'submit',
'#value' => t('Rebuild trampoline file'),
'#submit' => array('trampoline_admin_flush_caches'),
);
$form['clear_cache']['#weight'] = 5;
$form['buttons']['#weight'] = 10;
}
/**
* Submit handler for the Trampoline rebuild button.
*
* @see trampoline_form_system_performance_settings_alter().
*/
function trampoline_admin_flush_caches(&$form, $form_state) {
trampoline_flush_caches();
drupal_set_message(t('Trampoline file will be rebuild on next use.'));
}
/**
* Implements hook_init()
*/
function trampoline_init() {
drupal_add_js(array('trampolinePath' => base_path() . drupal_get_path('module', 'trampoline') . '/index.php?q='), 'setting');
trampoline_build();
}
/**
* Implementation of hook_flush_caches().
*/
function trampoline_flush_caches() {
// Delete the config file
module_load_include('inc', 'trampoline', 'trampoline');
file_delete(trampoline_files_path() . '/trampoline.config.inc');
trampoline_build();
}
/**
* Build config file for callback script.
*/
function trampoline_build() {
global $language;
global $theme_key;
global $base_url;
$profile = variable_get('install_profile', 'default');
// Require common.inc to make drupal_get_path() available.
// hook_exit may be invoked without a full bootstrap and we need
// this function to generate paths properly.
require_once './includes/common.inc';
module_load_include('inc', 'trampoline', 'trampoline');
$config_file = trampoline_files_path() . '/trampoline.config.inc';
// Only build the configuration files once. Clear the cache to rebuild it,
// or use the button on admin/settings/performance.
if (!file_exists($config_file)) {
if (!file_check_directory(trampoline_files_path(), FILE_CREATE_DIRECTORY)) {
watchdog('trampoline', 'Config file @config is not writable.', array('@config' => $config_file), WATCHDOG_ERROR);
return;
}
$trampoline_config = array();
//Build base configuration
$trampoline_config['language'] = $language->language;
$trampoline_config['profile'] = $profile;
$trampoline_config['theme_path'] = drupal_get_path('theme', $theme_key);
$trampoline_config['theme_settings'] = theme_get_settings();
$trampoline_config['base_path'] = base_path();
$trampoline_config['base_url'] = $base_url;
//Build route configuration
$trampoline_routes = array();
$routes = module_invoke_all('trampoline');
// We need these basic structures to build the trampoline config
while (!lock_acquire('menu_rebuild')) {
lock_wait('menu_rebuild');
}
$menu_router = menu_router_build();
lock_release('menu_rebuild');
// NB: The theme registry is only available from this function if it has been build previously.
$theme_registry = theme_get_registry();
foreach (module_invoke_all('trampoline_variables') as $variable) {
$trampoline_config['variables'][$variable] = variable_get($variable, NULL);
}
foreach ($routes as $route => $config) {
if (isset($menu_router[$route])) {
$menu_route = $menu_router[$route];
//Build the route configuration
$route_config = array();
$route_config['module'] = $menu_route['module'];
$route_config['module_path'] = drupal_get_path('module', $menu_route['module']);
//Get the files to load
$module_path = drupal_get_path('module', $menu_route['module']);
$route_config['includes'] = array($module_path .'/'.$menu_route['module'].'.module');
if (isset($config['includes'])) {
$route_config['includes'] = array_merge($route_config['includes'], $config['includes']);
}
if ($menu_route['file']) {
$route_config['includes'][] = $module_path.'/'.$menu_route['file'];
}
//Get the callback
$route_config['callback'] = $menu_route['page callback'];
//Get the arguments
$route_config['arguments'] = $menu_route['page arguments'];
$route_config['variables'] = array();
$route_config['variables']['clean_url'] = variable_get('clean_url', '0');
if (isset($config['theme_hooks'])) {
//Get theme information
$route_config['theme'] = array();
foreach ($config['theme_hooks'] as $theme_hook) {
$route_config['theme'][$theme_hook] = $theme_registry[$theme_hook];
}
}
if (is_array($config['hooks'])) {
foreach ($config['hooks'] as $hook => $name) {
if (is_array($name)) {
$implementations = module_implements($hook);
foreach ($implementations as $i) {
if (in_array($i, $name)) {
$trampoline_config['hooks'][$hook] = $i;
}
}
}
else {
$implementations = module_implements($name);
$trampoline_config['hooks'][$name] = $implementations;
}
}
}
}
$trampoline_routes[$route] = $route_config;
}
$trampoline_config['routes'] = $trampoline_routes;
// Write the configuration to a file
// TODO: This is a nasty security hole. PHP files writeable by
// the web server, ouch.
file_put_contents($config_file, '<?php $trampoline_config = ' . var_export($trampoline_config, true) . ';');
}
}
/**
* Utility function for generation the url to a path using trampoline.
* Arguments duplicate the original url function.
*
* @see url
*/
function trampoline_url($path = NULL, $options = array()) {
// Store the original path in the q argument
$defaults = array('query' => 'q='.$path);
$options = array_merge($defaults, $options);
return url(drupal_get_path('module', 'trampoline').'/index.php', $options);
}