-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.php
130 lines (101 loc) · 3.93 KB
/
generate.php
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
<?php
use Gajus\Dindent\Indenter;
use phpDocumentor\Reflection\DocBlockFactory;
require 'vendor/autoload.php';
define('DOCBLOCK_REGEX_ACTIONS', '#(?>(?P<docblock>/\*\*.*?\*/))\s*Action::call\((?P<quote>[\'"])(?P<name>.*?)\g{quote}#sm');
define('DOCBLOCK_REGEX_FILTERS', '#(?>(?P<docblock>/\*\*.*?\*/))[^;]*Filter::call\((?P<quote>[\'"])(?P<name>.*?)\g{quote}#sm');
if (isset($argv[1])) {
$codicePath = $argv[1];
} else {
echo "Codice Documentation Index Generator\n";
echo "$ generate.php [pathToCodiceSources]\n\n";
echo "Output will be stored in /output\n";
die;
}
if (!is_dir('./output')) {
mkdir('output');
}
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($codicePath . '/app', RecursiveDirectoryIterator::SKIP_DOTS));
$hookables = [
'actions' => [],
'filters' => [],
];
foreach ($iterator as $fileinfo) {
if (substr($filename = $fileinfo->getPathname(), -4) != '.php') {
continue;
}
$contents = file_get_contents($filename);
preg_match_all(DOCBLOCK_REGEX_ACTIONS, $contents, $fileActions, PREG_SET_ORDER);
preg_match_all(DOCBLOCK_REGEX_FILTERS, $contents, $fileFilters, PREG_SET_ORDER);
// Inject current file path into preg_match_all() results and save to temporary variables
$hookables['actions'][] = injectFilePathIntoMatches(normalizeFilePath($filename, $codicePath), filterEmptyMatches($fileActions));
$hookables['filters'][] = injectFilePathIntoMatches(normalizeFilePath($filename, $codicePath), filterEmptyMatches($fileFilters));
}
$factory = DocBlockFactory::createInstance();
// Generate array, pass to template, render it and save results...
// for each type of hookable
foreach (['actions', 'filters'] as $hookableType) {
$data[$hookableType] = [];
foreach ($hookables[$hookableType] as $hookablesInFile) {
foreach ($hookablesInFile as $hookable) {
$docblock = $factory->create($hookable['docblock']);
$params = [];
foreach ($docblock->getTagsByName('param') as $param) {
$params[] = [
'name' => $param->getVariableName(),
'type' => $param->getType(),
'description' => $param->getDescription()
];
}
// @todo: refactor (yeah, for sure I will...)
$fileDefined = explode('/', $hookable['fileDefined']);
$fileDefined = end($fileDefined);
$data[$hookableType][] = [
'name' => $hookable['name'],
'description' => $docblock->getSummary(),
'parameters' => $params,
'returns' => $docblock->getTagsByName('return') ? $docblock->getTagsByName('return')[0]->getType() : null,
'fileDefined' => $fileDefined,
'pathDefined' => $hookable['fileDefined'],
];
}
}
$data[$hookableType] = sortData($data[$hookableType]);
$output = renderIndex($data[$hookableType], $hookableType);
file_put_contents("./output/{$hookableType}-list.md", $output);
echo "Generated {$hookableType} index\n";
}
function filterEmptyMatches($matches)
{
return array_filter($matches, function($match) {
return !empty($match);
});
}
function renderIndex($data, $template)
{
$indenter = new Indenter();
ob_start();
require "templates/$template.php";
return $indenter->indent(ob_get_clean());
}
function injectFilePathIntoMatches($filePath, $matches)
{
return array_map(function ($match) use ($filePath) {
$match['fileDefined'] = $filePath;
return $match;
}, $matches);
}
function normalizeFilePath($filename, $codicePath)
{
return str_replace('\\', '/', substr($filename, strlen($codicePath) + 1));
}
function sortData($data)
{
usort($data, function($a, $b) {
if ($a['name'] == $b['name']) {
return 0;
}
return $a['name'] < $b['name'] ? -1 : 1;
});
return $data;
}