-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCodeGenerator.php
222 lines (191 loc) · 5.76 KB
/
CodeGenerator.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
/*
* This file is part of Class Preloader.
*
* (c) Graham Campbell <hello@gjcampbell.co.uk>
* (c) Michael Dowling <mtdowling@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ClassPreloader;
use ClassPreloader\Exception\IOException;
use ClassPreloader\File\FileUtils;
use ClassPreloader\Parser\DirVisitor;
use ClassPreloader\Parser\FileVisitor;
use ClassPreloader\Parser\NodeTraverser;
use ClassPreloader\Parser\StrictTypesVisitor;
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
/**
* This is the code generator class.
*/
final class CodeGenerator
{
/**
* The printer.
*
* @var \PhpParser\PrettyPrinter\Standard
*/
private $printer;
/**
* The parser.
*
* @var \PhpParser\Parser
*/
private $parser;
/**
* The traverser.
*
* @var \ClassPreloader\Parser\NodeTraverser
*/
private $traverser;
/**
* Create a new class preloader instance.
*
* @param \PhpParser\PrettyPrinter\Standard $printer
* @param \PhpParser\Parser $parser
* @param \ClassPreloader\Parser\NodeTraverser $traverser
*
* @return void
*/
public function __construct(PrettyPrinter $printer, Parser $parser, NodeTraverser $traverser)
{
$this->printer = $printer;
$this->parser = $parser;
$this->traverser = $traverser;
}
/**
* Create a new class preloader instance.
*
* Any options provided determine how the node traverser is setup.
*
* @param bool[] $options
* @param \PhpParser\Parser|null $parser
*
* @return self
*/
public static function create(array $options = [], ?Parser $parser = null)
{
$printer = new PrettyPrinter();
$parser = $parser === null ? self::getParser() : $parser;
$options = array_merge(['dir' => true, 'file' => true, 'skip' => false, 'strict' => false], $options);
$traverser = self::getTraverser($options['dir'], $options['file'], $options['skip'], $options['strict']);
return new self($printer, $parser, $traverser);
}
/**
* Get the parser to use.
*
* @return \PhpParser\Parser
*/
private static function getParser()
{
return (new ParserFactory())->createForNewestSupportedVersion();
}
/**
* Get the node traverser to use.
*
* @param bool $dir
* @param bool $file
* @param bool $skip
* @param bool $strict
*
* @return \ClassPreloader\Parser\NodeTraverser
*/
private static function getTraverser(bool $dir, bool $file, bool $skip, bool $strict)
{
$traverser = new NodeTraverser();
if ($dir) {
$traverser->addVisitor(new DirVisitor($skip));
}
if ($file) {
$traverser->addVisitor(new FileVisitor($skip));
}
if (!$strict) {
$traverser->addVisitor(new StrictTypesVisitor());
}
return $traverser;
}
/**
* Get a pretty printed string of code from a file while applying visitors.
*
* @param string $filePath
* @param bool $withComments
*
* @throws \ClassPreloader\Exception\IOException
* @throws \PhpParser\Error
*
* @return string
*/
public function getCode(string $filePath, bool $withComments = true)
{
$content = FileUtils::readPhpFile($filePath, $withComments);
if ($content === false) {
throw new IOException("Cannot open $filePath for reading.");
}
$parsed = $this->parser->parse($content);
assert($parsed !== null);
$stmts = $this->traverser->traverseFile($parsed, $filePath);
$pretty = $this->printer->prettyPrint($stmts);
$pretty = self::pregReplace(
'#^(<\?php)?[\s]*(/\*\*?.*?\*/)?[\s]*(declare[\s]*\([\s]*strict_types[\s]*=[\s]*1[\s]*\);)?#s',
'',
$pretty
);
return self::getCodeWrappedIntoNamespace($parsed, $pretty);
}
/**
* Perform a PREG replace, failing with an exception on error.
*
* @param string $pattern
* @param string $replacement
* @param string $subject
* @param int|null $limit
*
* @return string
*/
private static function pregReplace(string $pattern, string $replacement, string $subject, int $limit = null)
{
$output = @preg_replace($pattern, $replacement, $subject, $limit === null ? -1 : $limit);
assert($output !== null);
return $output;
}
/**
* Wrap the code into a namespace.
*
* @param \PhpParser\Node\Stmt[] $parsed
* @param string $pretty
*
* @return string
*/
private static function getCodeWrappedIntoNamespace(array $parsed, $pretty)
{
if (self::parsedCodeHasNamespaces($parsed)) {
$pretty = self::pregReplace('/^\s*(namespace.*);/im', '${1} {', $pretty, 1)."\n}\n";
} else {
$pretty = sprintf("namespace {\n%s\n}\n", $pretty);
}
return self::pregReplace('/(?<!.)[\r\n]+/', '', $pretty);
}
/**
* Check parsed code for having namespaces.
*
* @param \PhpParser\Node\Stmt[] $parsed
*
* @return bool
*/
private static function parsedCodeHasNamespaces(array $parsed)
{
// Namespaces can only be on first level in the code,
// so we make only check on it.
foreach ($parsed as $node) {
if ($node instanceof NamespaceNode) {
return true;
}
}
return false;
}
}