-
Notifications
You must be signed in to change notification settings - Fork 2
/
cw_tool.module
372 lines (315 loc) · 9.67 KB
/
cw_tool.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* @file
*
* Main CWTool module file.
*/
use CW\Factory\EntityControllerFactory;
use CW\Factory\UserControllerFactory;
use CW\Form\VariableFormGenerator;
use CW\Manager\VariableManager;
use CW\Model\EntityHandler;
use CW\Util\CronTimer;
use CW\Util\FormUtil;
use CW\Util\Functional;
use Pimple\Container;
require_once __DIR__ . '/vendor/autoload.php';
// Service names.
const CWTOOL_SERVICE_NODE_FACTORY = 'cw.node-controller.factory';
const CWTOOL_SERVICE_TAXONOMY_TERM_FACTORY = 'cw.taxonomy-term-controller.factory';
const CWTOOL_SERVICE_USER_FACTORY = 'cw.user-controller.factory';
const CWTOOL_SERVICE_FILE_FACTORY = 'cw.file-controller.factory';
const CWTOOL_SERVICE_IMAGE_FACTORY = 'cw.image-controller.factory';
const CWTOOL_SERVICE_VARIABLE_MANAGER = 'cw.variable-manager';
const CWTOOL_SERVICE_VARIABLE_ADAPTER = 'cw.variable-adapter';
const CWTOOL_SERVICE_LOGGER = 'cw.logger';
const CWTOOL_SERVICE_IDENTITY_MAP = 'cw.identity-map';
const CWTOOL_SERVICE_OBJECT_HANDLER = 'cw.object-handler.drupal';
const CWTOOL_SERVICE_BATCH_ENTITY_SAVER = 'cw.entity-batch-saver';
const CWTOOL_SERVICE_CRON_TIMER = 'cw.cron-timer';
const CWTOOL_SERVICE_DB_CACHE = 'cw.cache.db';
// Static cache falling back to database cache.
const CWTOOL_SERVICE_DOUBLE_CACHE = 'cw.cache.double';
/**
* Dependency injection configuration.
*
* @return \Pimple\Container
*/
function cw_tool_get_container() {
static $container;
if (!empty($container)) {
return $container;
}
$container = new Container();
$container[CWTOOL_SERVICE_LOGGER] = function (Container $c) {
return new \Drupal\PSR3\Logger\Watchdog();
};
$container[CWTOOL_SERVICE_IDENTITY_MAP] = function (Container $c) {
return new \CW\Util\LocalProcessIdentityMap();
};
$container[CWTOOL_SERVICE_OBJECT_HANDLER] = function (Container $c) {
return new \CW\Model\DrupalEntityHandler();
};
$container[CWTOOL_SERVICE_NODE_FACTORY] = function (Container $c) {
return new EntityControllerFactory(
$c[CWTOOL_SERVICE_IDENTITY_MAP],
$c[CWTOOL_SERVICE_OBJECT_HANDLER],
'CW\Controller\NodeController',
'node',
$c[CWTOOL_SERVICE_LOGGER]
);
};
$container[CWTOOL_SERVICE_USER_FACTORY] = function (Container $c) {
return new EntityControllerFactory(
$c[CWTOOL_SERVICE_IDENTITY_MAP],
$c[CWTOOL_SERVICE_OBJECT_HANDLER],
'CW\Controller\UserController',
'user',
$c[CWTOOL_SERVICE_LOGGER]
);
};
$container[CWTOOL_SERVICE_FILE_FACTORY] = function (Container $c) {
return new EntityControllerFactory(
$c[CWTOOL_SERVICE_IDENTITY_MAP],
$c[CWTOOL_SERVICE_OBJECT_HANDLER],
'CW\Controller\FileController',
'file',
$c[CWTOOL_SERVICE_LOGGER]
);
};
$container[CWTOOL_SERVICE_IMAGE_FACTORY] = function (Container $c) {
return new EntityControllerFactory(
$c[CWTOOL_SERVICE_IDENTITY_MAP],
$c[CWTOOL_SERVICE_OBJECT_HANDLER],
'CW\Controller\ImageController',
'file',
$c[CWTOOL_SERVICE_LOGGER]
);
};
$container[CWTOOL_SERVICE_TAXONOMY_TERM_FACTORY] = function (Container $c) {
return new EntityControllerFactory(
$c[CWTOOL_SERVICE_IDENTITY_MAP],
$c[CWTOOL_SERVICE_OBJECT_HANDLER],
'CW\Controller\TaxonomyTermController',
'taxonomy_term',
$c[CWTOOL_SERVICE_LOGGER]
);
};
$container[CWTOOL_SERVICE_BATCH_ENTITY_SAVER] = function (Container $c) {
return new CW\Util\EntityBatchSaver(
$c[CWTOOL_SERVICE_IDENTITY_MAP],
$c[CWTOOL_SERVICE_LOGGER]
);
};
$container[CWTOOL_SERVICE_VARIABLE_MANAGER] = function (Container $c) {
return new VariableManager($c[CWTOOL_SERVICE_LOGGER]);
};
$container[CWTOOL_SERVICE_VARIABLE_ADAPTER] = function (Container $c) {
return new \CW\Adapter\DrupalVariableAdapter();
};
$container[CWTOOL_SERVICE_CRON_TIMER] = function (Container $c) {
return new CronTimer($c[CWTOOL_SERVICE_VARIABLE_ADAPTER]);
};
$container[CWTOOL_SERVICE_DB_CACHE] = function (Container $c) {
$cachePool = new \Drupal\PSRCache\CachePool();
$cachePool->setCacheHandler(new \Drupal\PSRCache\Adaptor\DefaultDrupalCacheHandler());
return $cachePool;
};
$container[CWTOOL_SERVICE_DOUBLE_CACHE] = function (Container $c) {
return new \Drupal\PSRCache\StaticCacheProxy($c[CWTOOL_SERVICE_DB_CACHE]);
};
drupal_alter('cw_tool_service_container_definition', $container);
return $container;
}
/**
* Implements hook_menu().
*/
function cw_tool_menu() {
return array(
// Admin section.
'admin/config/application' => array(
'title' => 'Application settings',
'description' => 'Application settings.',
'position' => 'left',
'weight' => -10,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
),
// Variable settings page.
'admin/config/application/variables' => array(
'title' => 'Variables',
'description' => 'Environment app variables.',
'page callback' => 'drupal_get_form',
'page arguments' => array('cw_tool_core_admin_settings_form'),
'access arguments' => array('access administration pages'),
),
);
}
function cw_tool_core_admin_settings_form($form) {
/** @var VariableManager $variableManager */
$variableManager = cw_tool_get_container()[CWTOOL_SERVICE_VARIABLE_MANAGER];
$variableManager->collectAppVariables();
$variablesForm = new VariableFormGenerator($variableManager);
return $variablesForm->generateForm($form);
}
/**
* @return CronTimer
*/
function cw_tool_cron_timer() {
return cw_tool_get_container()[CWTOOL_SERVICE_CRON_TIMER];
}
/**
* @return \CW\Adapter\VariableAdapter
*/
function cw_tool_variable_adapter() {
return cw_tool_get_container()[CWTOOL_SERVICE_VARIABLE_ADAPTER];
}
/**
* @return \Psr\Log\LoggerInterface
*/
function cw_tool_logger() {
return cw_tool_get_container()[CWTOOL_SERVICE_LOGGER];
}
/**
* @return EntityControllerFactory
*/
function cw_tool_file_factory() {
return cw_tool_get_container()[CWTOOL_SERVICE_FILE_FACTORY];
}
/**
* @return EntityControllerFactory
*/
function cw_tool_image_factory() {
return cw_tool_get_container()[CWTOOL_SERVICE_IMAGE_FACTORY];
}
/**
* @return EntityControllerFactory
*/
function cw_tool_taxonomy_term_factory() {
return cw_tool_get_container()[CWTOOL_SERVICE_TAXONOMY_TERM_FACTORY];
}
/**
* @return UserControllerFactory
*/
function cw_tool_user_factory() {
return cw_tool_get_container()[CWTOOL_SERVICE_USER_FACTORY];
}
/**
* @return EntityHandler
*/
function cw_tool_entity_handler() {
return cw_tool_get_container()[CWTOOL_SERVICE_OBJECT_HANDLER];
}
/**
* @return \Psr\Cache\CacheItemPoolInterface
*/
function cw_tool_db_cache() {
return cw_tool_get_container()[CWTOOL_SERVICE_DB_CACHE];
}
/**
* @return \Psr\Cache\CacheItemPoolInterface
*/
function cw_tool_double_cache() {
return cw_tool_get_container()[CWTOOL_SERVICE_DOUBLE_CACHE];
}
/**
* CWTool form validator proxy for registered validator.
*
* @param array $form
* @param array $form_state
*/
function cw_tool_form_util_validate(&$form, &$form_state) {
FormUtil::callHook(FormUtil::HOOK_VALIDATE, $form, $form_state);
}
function cw_tool_form_util_submit(&$form, &$form_state) {
FormUtil::callHook(FormUtil::HOOK_SUBMIT, $form, $form_state);
}
function cw_tool_form_util_after_build($form, $form_state) {
return FormUtil::callAfterBuildHook($form, $form_state);
}
/**
* Adds the CWTool JS file to the page load.
*/
function cw_tool_include_js() {
drupal_add_js(drupal_get_path('module', 'cw_tool') . '/javascript/cw_tool.js');
}
/**
* Load the utility functions.
*/
function cw_tool_include_util() {
require_once __DIR__ . '/includes/cw_tool.util.inc';
}
/**
* Load the content helper functions.
*/
function cw_tool_include_content() {
require_once __DIR__ . '/includes/cw_tool.content.inc';
}
/**
* Implements hook_forms().
*/
function cw_tool_forms($form_id) {
$forms = array();
if (strpos($form_id, 'cw_tool_form') === 0) {
$formClassName = str_replace('cw_tool_form:', '', $form_id);
$forms[$form_id] = array(
'callback' => 'cw_tool_generate_form',
'callback arguments' => array($formClassName),
);
}
return $forms;
}
/**
* Central form hook for forms that are defined as CW\Form\FormBuilder classes.
*
* @param array $form
* @param array $form_state
* @param string $class
* @return mixed
*/
function cw_tool_generate_form($form, &$form_state, $class) {
$funcArgs = func_get_args();
call_user_func_array(array($class, 'attachSubmitAndValidation'), array(&$form));
$callArgs = array(&$form, &$form_state);
Functional::walk($funcArgs[3], function ($item) use (&$callArgs) { array_push($callArgs, $item); });
return call_user_func_array(array($class, 'build'), $callArgs);
}
/**
* Implements hook_library().
*/
function cw_tool_library() {
$libraries = [];
$path = drupal_get_path('module', 'cw_tool');
// Path JS plugin.
$libraries['cwtool.path'] = [
'title' => 'CWTool Path Javascript plugin.',
'version' => VERSION,
'js' => [
$path . '/javascript/cw_tool.path.js' => ['group' => JS_LIBRARY],
],
];
// Referrer JS plugin.
$libraries['cwtool.referrer'] = [
'title' => 'CWTool Referrer Javascript plugin.',
'version' => VERSION,
'js' => [
$path . '/javascript/cw_tool.referrer.js' => ['group' => JS_LIBRARY],
],
'dependencies' => [
['cw_tool', 'cwtool.path'],
],
];
return $libraries;
}
/**
* Check if the form ID is associated to the form class.
*
* @param string $class
* @param string $form_id
* @return bool
*/
function cw_tool_is_form_id_of_form_class($class, $form_id) {
return strpos($form_id, $class) !== FALSE;
}