-
Notifications
You must be signed in to change notification settings - Fork 7
/
coder_upgrade.module
184 lines (166 loc) · 6.09 KB
/
coder_upgrade.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
<?php
/**
* @file
* Provides primary Backdrop hook implementations.
*
* Developer module that assists a contributed module with version upgrade from
* the Drupal 7.x to Backdrop 1.x core API. The module creates new code files by
* modifying existing code files in accordance with the core API changes. The
* initial Backdrop version would be a straight port of features from the Drupal
* version.
*
* For a list of core API changes that are handled by this module, see:
* - https://api.backdropcms.org/change-records
*
* Copyright 2008-11 by Jim Berry ("solotandem", http://drupal.org/user/240748)
*/
/**
* Defines an array used to insert blank lines into created code.
*/
define('CODER_UPGRADE_WHITESPACE', array('type' => T_WHITESPACE, 'value' => 1 ));
if (function_exists('t')) {
// This code is being executed inside a process running Backdrop.
module_load_include('inc', 'coder_upgrade', 'coder_upgrade');
}
/**
* Implements hook_permission().
*/
function coder_upgrade_permission() {
return array(
'administer code conversions' => array(
'title' => t('Administer code conversions'),
'description' => t('Manage code conversion tasks for Coder Upgrade.'),
),
);
}
/**
* Implements hook_menu().
*/
function coder_upgrade_menu() {
module_load_include('inc', 'coder_upgrade', 'includes/menu');
return _coder_upgrade_menu();
}
/**
* Implements hook_menu_alter().
*/
function coder_upgrade_menu_alter(&$items) {
if (!module_exists('coder_review')) {
// Remove the Coder menu item.
unset($items['admin/config/development/coder']);
// Promote the menu items defined by this module.
$items['admin/config/development/coder-upgrade']['title'] = 'Coder Upgrade';
$items['admin/config/development/coder-upgrade']['type'] = MENU_NORMAL_ITEM;
}
}
/**
* Implements hook_config_info().
*/
function coder_upgrade_config_info() {
$prefixes['coder_upgrade.settings'] = array(
'label' => t('Coder Upgrade settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_autoload_info().
*/
function coder_upgrade_autoload_info() {
return array(
'PGPEditor' => 'grammar_parser/editor.inc',
'PGPNode' => 'grammar_parser/list.inc',
'PGPList' => 'grammar_parser/list.inc',
'PGPBody' => 'grammar_parser/list.inc',
'PGPExpression' => 'grammar_parser/list.inc',
'PGPOperand' => 'grammar_parser/list.inc',
'PGPString' => 'grammar_parser/list.inc',
'PGPBase' => 'grammar_parser/object.inc',
'PGPArray' => 'grammar_parser/object.inc',
'PGPFunctionCall' => 'grammar_parser/object.inc',
'PGPAssignment' => 'grammar_parser/object.inc',
'PGPClass' => 'grammar_parser/object.inc',
'PGPConditional' => 'grammar_parser/object.inc',
'PGPFor' => 'grammar_parser/object.inc',
'PGPForeach' => 'grammar_parser/object.inc',
'PGPCase' => 'grammar_parser/object.inc',
'PGPDeclare' => 'grammar_parser/object.inc',
'PGPTryCatch' => 'grammar_parser/object.inc',
'PGPListStatement' => 'grammar_parser/object.inc',
'PGPNamespace' => 'grammar_parser/object.inc',
'PGPUse' => 'grammar_parser/object.inc',
'PGPParser' => 'grammar_parser/parser.inc',
'PGPReader' => 'grammar_parser/reader.inc',
'PGPWriter' => 'grammar_parser/writer.inc',
);
}
/**
* Implements hook_upgrade_upgrade_file_alter().
*/
function coder_upgrade_upgrade_file_alter(&$reader) {
global $_coder_upgrade_module_name, $_coder_upgrade_filename;
cdp("inside " . __FUNCTION__);
$nodes = &$reader->getFunctions();
$config_function_exists = FALSE;
foreach ($nodes as &$node) {
$item = &$node->data;
$body = &$item->body;
if (!empty($body) && $calls = $body->searchAll('PGPFunctionCall', 'name', 'value', 'config_get')) {
$build_config_function = FALSE;
foreach ($calls as &$call) {
if ($call->printParameter(0) == "'" .$_coder_upgrade_module_name . ".settings'") {
$build_config_function = TRUE;
break;
}
}
if ($build_config_function && (strpos($_coder_upgrade_filename, '.module') !== FALSE)) {
$function = $_coder_upgrade_module_name . "_config_info";
if (!$config_function_exists) {
coder_upgrade_build_config_function($node, $reader);
$config_function_exists = TRUE;
}
}
if (count($calls) > 1) {
$create_config = FALSE;
// Create helper objects.
$editor = PGPEditor::getInstance();
// Insert config() call.
$statement = $editor->textToStatements('$config = config(\''. $_coder_upgrade_module_name . '.settings\')');
$body->insertFirst($statement->getElement(0));
foreach ($calls as &$call) {
if ($call->printParameter(0) == "'" .$_coder_upgrade_module_name . ".settings'") {
$call->name['value'] = '$config->get';
$call->deleteParameter();
}
}
}
}
// Todo same for config_set, config_clear.
}
}
function coder_upgrade_build_config_function($node, $reader) {
global $_coder_upgrade_module_name, $_coder_upgrade_module_human_name;
$editor = PGPEditor::getInstance();
// Set values for the new hook function.
$comment = array(
'type' => T_DOC_COMMENT,
'value' => "/**\n * Implements hook_config_info().\n */",
);
$name = $_coder_upgrade_module_name . '_config_info';
// Create the new hook function.
$function = new PGPClass($name);
$function->comment = $comment;
$function->type = T_FUNCTION;
$function->parameters = new PGPList();
$strings[] = '$prefixes[\'' . $_coder_upgrade_module_name . '.settings\'] = array(';
$strings[] = " 'label' => t('{$_coder_upgrade_module_human_name} settings'),";
$strings[] = " 'group' => t('Configuration'),";
$strings[] = ');';
$strings[] = 'return $prefixes;';
$function->body = $editor->textToStatements(implode("\n", $strings));
// Get the statement list the function node is part of.
$container = &$node->container;
// Insert the new function before the old function.
$container->insertBefore($node, $function, 'function');
// Insert a blank line.
$container->insertBefore($node, CODER_UPGRADE_WHITESPACE, 'whitespace');
}