-
Notifications
You must be signed in to change notification settings - Fork 1
/
collect.php
127 lines (109 loc) · 3.16 KB
/
collect.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
<?php
declare(strict_types=1);
use App\LibraryReview\ComposerLockFile;
use App\LibraryReview\TwigExtensions;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
require_once __DIR__ . '/vendor/autoload.php';
echo "Scanning packages ...\n";
$composerFile = new ComposerLockFile(__DIR__ . '/composer.lock');
$packages = $composerFile->getPackageInfo('joomla/');
$target = 'Laravel';
$samples = parseSampleFiles(getSampleFiles(__DIR__ . '/samples/Joomla'));
$loader = new FilesystemLoader(__DIR__ . '/template');
$twig = new Environment(
$loader,
[
'cache' => __DIR__ . '/cache',
'debug' => true,
]
);
TwigExtensions::extend($twig);
foreach ($packages as $package) {
echo "Processing package {$package['name']} ...\n";
$path = getStorageLocation($package, $target);
$template = $twig->load('package.twig');
file_put_contents(
$path . '.md',
trim(preg_replace("~(\s*\n){3,}~", "\n\n", $template->render(
[
'package' => $package,
'replacement' => $target,
'samples' => $samples,
]
))) . "\n"
);
}
echo "Done.\n";
/**
* @param array $package
* @param string $target
*
* @return string
*/
function getStorageLocation(array $package, string $target): string
{
$parts = array_map('ucfirst', explode('/', $package['name']));
array_shift($parts);
$path = __DIR__ . '/docs/' . $target . '/' . implode('/', $parts);
$dir = dirname($path);
if (!is_dir($dir)) {
createDir($dir);
}
return $path;
}
/**
* @param string $dir
*/
function createDir(string $dir): void
{
if (!mkdir($dir, 0777, true) && !is_dir($dir)) {
throw new \RuntimeException(sprintf('Directory "%s" could not be created', $dir));
}
}
/**
* @param string $path
*
* @return array
*/
function getSampleFiles(string $path): array
{
$files = [];
/** @var \SplFileInfo $fileInfo */
foreach (
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path)
) as $fileInfo
) {
$pathname = $fileInfo->getPathname();
if (preg_match('~\.php$~', $pathname)) {
$files[] = $pathname;
}
}
return $files;
}
/**
* @param array $files
*
* @return array
*/
function parseSampleFiles(array $files): array
{
$samples = [];
foreach ($files as $file) {
$code = file_get_contents($file);
$docblock = preg_replace('~^.*?(/\*\*.*?\*/).*$~sm', '$1', $code);
$autoload = preg_replace('~^.*?(require.*?autoload.php\';).*$~sm', '$1', $code);
$print_r = preg_replace('~^.*?(print_r\(.*?\);).*$~sm', '$1', $code);
$code = trim(str_replace(['<?php', $docblock, $autoload, $print_r], '', $code));
$title = preg_replace('~^.*?/[*\s]+(.*?)\s*\n.*$~sm', '$1', $docblock);
$method = preg_replace('~^.*?@covers\s+(.*?)\s*\n.*$~sm', '$1', $docblock);
$output = preg_replace('~^.*?print_r\(\s*(.*?)[,)].*$~sm', '$1', $print_r);
$samples[$method][] = [
'title' => $title,
'code' => $code,
'output' => $output,
];
}
return $samples;
}