-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dumper.php
180 lines (151 loc) · 5.84 KB
/
Dumper.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
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
<?php
declare(strict_types=1);
namespace Brainshaker95\PhpToTsBundle\Service;
use Brainshaker95\PhpToTsBundle\Interface\Config;
use Brainshaker95\PhpToTsBundle\Model\Config\FileType;
use Brainshaker95\PhpToTsBundle\Model\TsEnum;
use Brainshaker95\PhpToTsBundle\Model\TsInterface;
use Brainshaker95\PhpToTsBundle\Service\Traits\HasConfiguration;
use Brainshaker95\PhpToTsBundle\Tool\Str;
use PhpParser\ErrorHandler\Collecting;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use const DIRECTORY_SEPARATOR;
use const PHP_EOL;
use function is_string;
final class Dumper
{
use HasConfiguration;
private Parser $parser;
public function __construct(
private readonly Filesystem $filesystem,
private readonly Visitor $visitor,
) {
$this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
}
/**
* Dumps all TypeScriptables in the given directory.
* - When no config is given the global bundle config will be used.
*
* @param Config|string|null $configOrDir directory to dump or config used for dumping
* @param ?Config $config config used for dumping
* @param ?callable(string $path, TsInterface|TsEnum $tsInterface): void $successCallback callback to run for dumped file
*
* @throws FileNotFoundException
*/
public function dumpDir(
Config|string|null $configOrDir = null,
?Config $config = null,
?callable $successCallback = null,
): void {
$config = $this->config->merge($configOrDir instanceof Config ? $configOrDir : $config);
$dir = $this->filesystem->makeAbsolute(
is_string($configOrDir) ? $configOrDir : $config->getInputDir(),
);
$this->filesystem->assertDir($dir);
$this->dumpFiles([$dir], $config, $successCallback);
}
/**
* Dumps all TypeScriptables in the given files and directories.
* - When no config is given the global bundle config will be used.
*
* @param array<SplFileInfo|string> $files array of files to dump
* @param ?Config $config config used for dumping
* @param ?callable(string $path, TsInterface|TsEnum $tsInterface): void $successCallback callback to run for dumped file
*
* @throws FileNotFoundException
*/
public function dumpFiles(
array $files,
?Config $config = null,
?callable $successCallback = null,
): void {
foreach ($this->filesystem->getSplFileInfoArray($files) as $file) {
if ($file->isDir()) {
$this->dumpFiles([...(new Finder())->depth(0)->in($file->getPathname())], $config, $successCallback);
} else {
$this->dumpFile($file, $config, $successCallback);
}
}
}
/**
* Dumps all TypeScriptables in the given file.
* - When no config is given the global bundle config will be used.
*
* @param SplFileInfo|string $file file to dump
* @param ?Config $config config used for dumping
* @param ?callable(string $path, TsInterface|TsEnum $tsInterface): void $successCallback callback to run for dumped file
*
* @throws FileNotFoundException
*/
public function dumpFile(
SplFileInfo|string $file,
?Config $config = null,
?callable $successCallback = null,
): void {
$config = $this->config->merge($config);
$tsInterfaces = $this->getTsInterfacesFromFile($file, $config);
if (!$tsInterfaces) {
return;
}
$pathPrefix = $config->getOutputDir() . DIRECTORY_SEPARATOR;
$doRequireValueOf = false;
foreach ($tsInterfaces as $tsInterface) {
$fileName = $tsInterface->getFileName();
$path = $this->filesystem->makeAbsolute($pathPrefix . $fileName);
$this->filesystem->dumpFile($path, $tsInterface->toString() . PHP_EOL);
if (!$doRequireValueOf) {
foreach ($tsInterface->properties as $property) {
if ($property->doesRequireValueOf) {
$doRequireValueOf = true;
break;
}
}
}
if ($successCallback) {
$successCallback($path, $tsInterface);
}
}
if (!$doRequireValueOf) {
return;
}
$valueOfPath = $pathPrefix . (new ($config->getFileNameStrategy())())->getName('valueOf') . '.ts';
$isModule = $config->getFileType() === FileType::TYPE_MODULE;
$this->filesystem->dumpFile(
$valueOfPath,
($isModule ? 'export' : 'declare')
. ' type ValueOf<T> = T[keyof T];'
. PHP_EOL,
);
}
/**
* Creates TsInterface instances from all classes in the given file.
*
* @param SplFileInfo|string $file file to extract TsInterface instances from
*
* @return (TsInterface|TsEnum)[]
*
* @throws FileNotFoundException
*/
public function getTsInterfacesFromFile(SplFileInfo|string $file, ?Config $config = null): array
{
$file = $this->filesystem->getSplFileInfo($file);
$this->filesystem->assertFile($file->getRealPath());
if (Str::toLower($file->getExtension()) !== 'php') {
return [];
}
$statements = $this->parser->parse($file->getContents(), new Collecting()) ?? [];
$traverser = new NodeTraverser();
$this->visitor->config = $config;
$traverser->addVisitor($this->visitor);
$traverser->traverse($statements);
return [
...$this->visitor->getTsInterfaces(),
...$this->visitor->getTsEnums(),
];
}
}