-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAPI.php
More file actions
127 lines (106 loc) · 3.14 KB
/
API.php
File metadata and controls
127 lines (106 loc) · 3.14 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
<?php
namespace DrupalCodeBuilder\Generator;
/**
* Component generator: api.php file for documention hooks and callbacks.
*
* This component should be requested once module code has been written. It
* looks for calls to module_invoke_all() and generates scaffold hook
* documentation for hook names that have the module's name as a prefix.
*/
class API extends PHPFile {
/**
* Return an array of subcomponent types.
*/
public function requiredComponents() {
// We have no subcomponents.
return array();
}
/**
* Build the code files.
*/
public function getFileInfo() {
$module_root_name = $this->component_data['root_component_name'];
return array(
'path' => '', // Means base folder.
'filename' => "$module_root_name.api.php",
'body' => $this->fileContents(),
'build_list_tags' => ['code', 'api'],
);
}
/**
* Return the summary line for the file docblock.
*/
function fileDocblockSummary() {
return "Hooks provided by the %readable module.";
}
/**
* Return the main body of the file code.
*/
function code_body() {
// Sanity checks already done at this point; no need to catch exception.
$mb_task_handler_analyze = \DrupalCodeBuilder\Factory::getTask('AnalyzeModule');
$hooks = $mb_task_handler_analyze->getInventedHooks($this->component_data['root_component_name']);
// Build an array of code pieces.
$code_pieces = array();
// The docblock grouping.
$code_pieces['group'] = <<<EOT
/**
* @addtogroup hooks
* @{
*/
EOT;
foreach ($hooks as $hook_short_name => $parameters) {
$code_pieces[$hook_short_name] = $this->hook_code($hook_short_name, $parameters);
}
// Add lines from child function components.
// Function data has been set by buildComponentContents().
foreach ($this->functions as $component_name => $function_lines) {
// Blank line after the function.
$function_lines[] = '';
$code_pieces[$component_name] = implode("\n", $function_lines);
}
// The docblock grouping.
$code_pieces['end_group'] = <<<EOT
/**
* @} End of "addtogroup hooks".
*/
EOT;
return $code_pieces;
}
/**
* Create the code for a single hook.
*
* @param $hook_short_name
* The short name of the hook, i.e., without the 'hook_' prefix.
* @param $parameters_string
* A string of the hook's parameters.
*
* @return
* A string of formatted code for inclusion in the api.php file.
*/
function hook_code($hook_short_name, $parameters_string) {
$parameters = explode(', ', $parameters_string);
$parameters_doc_lines = array();
foreach ($parameters as $parameter) {
$parameters_doc_lines[] = " * @param $parameter\n" .
" * TODO: document this parameter.";
}
if (!empty($parameters_doc_lines)) {
$parameters_doc = " *\n" . implode("\n", $parameters_doc_lines);
}
return <<<EOT
/**
* TODO: write summary line.
*
* TODO: longer description.
$parameters_doc
*
* @return
* TODO: Document return value if there is one.
*/
function hook_$hook_short_name($parameters_string) {
// TODO: write sample code.
}
EOT;
}
}