-
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathFileCreator.php
More file actions
150 lines (130 loc) · 4.89 KB
/
FileCreator.php
File metadata and controls
150 lines (130 loc) · 4.89 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
namespace Safe\Generator;
use Safe\Templating\Engine;
use Safe\Templating\Filesystem;
use Safe\XmlDocParser\ErrorType;
use Safe\XmlDocParser\Scanner;
use Safe\XmlDocParser\Method;
use function array_merge;
use function file_exists;
class FileCreator
{
private Engine $engine;
public function __construct(?Engine $engine = null)
{
$this->engine = $engine ?? new Engine();
}
/**
* This function generate an improved php lib function in a php file
*
* @param Method[] $functions
*/
public function generatePhpFile(
// These aren't actually sensitive, they just fill the
// stack traces with tons of useless information.
#[\SensitiveParameter] array $functions,
string $path
): void {
$path = rtrim($path, '/').'/';
$phpFunctionsByModule = [];
foreach ($functions as $function) {
$writePhpFunction = new WritePhpFunction($function);
$phpFunctionsByModule[$function->getModuleName()][] = $writePhpFunction->getPhpFunctionalFunction();
}
foreach ($phpFunctionsByModule as $module => $phpFunctions) {
$lcModule = \lcfirst($module);
if (!\is_dir($path)) {
\mkdir($path);
}
Filesystem::dumpFile($path . $lcModule . '.php', $this->engine->generate('Module.php.tpl', [
'{{exceptionName}}' => self::toExceptionName($module),
'{{functions}}' => \implode(PHP_EOL, $phpFunctions),
]));
}
}
/**
* @param string[] $versions
*/
public function generateVersionSplitters(string $module, string $path, array $versions, bool $return = false): void
{
$lcModule = \lcfirst($module);
$stream = \fopen($path.$lcModule.'.php', 'w');
if ($stream === false) {
throw new \RuntimeException('Unable to write to '.$path);
}
$return = $return ? "return " : "";
\fwrite($stream, "<?php\n");
foreach ($versions as $version) {
if (file_exists("$path/$version/$lcModule.php")) {
\fwrite($stream, "\nif (str_starts_with(PHP_VERSION, \"$version.\")) {");
\fwrite($stream, "\n {$return}require_once __DIR__ . '/$version/$lcModule.php';");
\fwrite($stream, "\n}");
}
}
\fwrite($stream, "\n");
\fclose($stream);
}
/**
* @param Method[] $functions
* @return string[]
*/
private function getFunctionsNameList(array $functions): array
{
$functions = array_filter(
$functions,
fn($function) => $function->getErrorType() !== ErrorType::UNKNOWN
);
$functionNames = array_map(function (Method $function) {
return $function->getFunctionName();
}, $functions);
$functionNames = array_merge($functionNames, Scanner::getSpecialCases());
$functionNames = array_diff($functionNames, Scanner::getHiddenFunctions());
natcasesort($functionNames);
return $functionNames;
}
/**
* This function generate a PHP file containing the list of functions we can handle.
*
* @param Method[] $functions
*/
public function generateFunctionsList(array $functions, string $path): void
{
Filesystem::dumpFile($path, $this->engine->generate('FunctionList.php.tpl', [
'{{functionNames}}' => \implode(PHP_EOL, \array_map(static fn(string $name): string => \sprintf('\'%s\',', $name), $this->getFunctionsNameList($functions))),
]));
}
/**
* Generates a configuration file for replacing all functions when using rector/rector.
*
* @param Method[] $functions
*/
public function generateRectorFile(array $functions, string $path): void
{
Filesystem::dumpFile($path, $this->engine->generate('RectorConfig.php.tpl', [
'{{functionNames}}' => \implode(PHP_EOL, \array_map(static fn(string $name): string => \sprintf('\'%1$s\' => \'Safe\\%1$s\',', $name), $this->getFunctionsNameList($functions))),
]));
}
public function createExceptionFile(string $moduleName): void
{
$exceptionName = self::toExceptionName($moduleName);
Filesystem::dumpFile(Filesystem::outputDir() . '/Exceptions/'.$exceptionName.'.php', $this->engine->generate('Exception.php.tpl', [
'{{exceptionName}}' => $exceptionName,
]));
}
public static function getSafeRootDir(): string
{
$path = realpath(__DIR__ . '/../../..');
if (false === $path) {
throw new \RuntimeException('Unable to locate root directory');
}
return $path;
}
/**
* Generates the name of the exception class
*/
public static function toExceptionName(string $moduleName): string
{
return str_replace('-', '', \ucfirst($moduleName)).'Exception';
}
}