Skip to content

Commit 0dba80e

Browse files
Add a version selector
1 parent fed0644 commit 0dba80e

23 files changed

+217
-155
lines changed

app/Enums/PhpVersion.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Enums;
4+
5+
use PhpParser\Parser;
6+
use PhpParser\ParserFactory;
7+
use PhpParser\PhpVersion as ParserPhpVersion;
8+
9+
enum PhpVersion: string
10+
{
11+
case Php80 = '8.0';
12+
case Php81 = '8.1';
13+
case Php82 = '8.2';
14+
case Php83 = '8.3';
15+
case Php84 = '8.4';
16+
17+
public function toDisplayable(): string
18+
{
19+
return match ($this) {
20+
self::Php80 => 'PHP 8.0',
21+
self::Php81 => 'PHP 8.1',
22+
self::Php82 => 'PHP 8.2',
23+
self::Php83 => 'PHP 8.3',
24+
self::Php84 => 'PHP 8.4',
25+
};
26+
}
27+
28+
public function toPhpParser(): Parser
29+
{
30+
return (new ParserFactory)->createForVersion(match ($this) {
31+
self::Php80 => ParserPhpVersion::fromComponents(8, 0),
32+
self::Php81 => ParserPhpVersion::fromComponents(8, 1),
33+
self::Php82 => ParserPhpVersion::fromComponents(8, 2),
34+
self::Php83 => ParserPhpVersion::fromComponents(8, 3),
35+
self::Php84 => ParserPhpVersion::fromComponents(8, 4),
36+
});
37+
}
38+
39+
public static function toOptions(): array
40+
{
41+
return collect(self::cases())
42+
->mapWithKeys(fn (PhpVersion $case) => [
43+
$case->value => $case->toDisplayable(),
44+
])
45+
->all();
46+
}
47+
}

app/Http/Controllers/Api/GenerateController.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22

33
namespace App\Http\Controllers\Api;
44

5+
use App\Enums\PhpVersion;
56
use App\Traverser\CollectSignificantNodeLocations;
67
use Illuminate\Http\Request;
78
use PhpParser\Error;
8-
use PhpParser\Lexer\Emulative;
99
use PhpParser\NodeTraverser;
10-
use PhpParser\Parser\Php8;
11-
use PhpParser\PhpVersion;
1210

1311
class GenerateController
1412
{
1513
public function __invoke(Request $request)
1614
{
1715
$code = $request->input('code');
18-
$lexer = new Emulative(PhpVersion::getNewestSupported());
19-
$parser = new Php8($lexer, PhpVersion::getNewestSupported());
16+
$version = $request->enum('version', PhpVersion::class);
2017

21-
$ast = $parser->parse($code);
22-
23-
$traverser = new NodeTraverser($collector = new CollectSignificantNodeLocations());
24-
$traverser->traverse($ast);
18+
$parser = $version->toPhpParser();
2519

2620
try {
21+
$ast = $parser->parse($code);
22+
23+
$traverser = new NodeTraverser($collector = new CollectSignificantNodeLocations());
24+
$traverser->traverse($ast);
25+
2726
return response()->json([
2827
'ast' => $ast,
2928
'significantNodeLocations' => $collector->getSignificantNodeLocations(),

app/Http/Controllers/IndexController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Enums\PhpVersion;
56
use Illuminate\Http\Request;
67

78
class IndexController
89
{
910
public function __invoke(Request $request)
1011
{
11-
return view('index');
12+
return view('index', [
13+
'phpVersions' => PhpVersion::toOptions(),
14+
]);
1215
}
1316
}

public/build/assets/app-6558df94.js renamed to public/build/assets/app-1844d7f9.js

Lines changed: 111 additions & 111 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/build/assets/app-9fb6e60c.css renamed to public/build/assets/app-af8a9983.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/build/assets/cssMode-840d9458.js renamed to public/build/assets/cssMode-50cdfe7f.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/build/assets/freemarker2-59349ab3.js renamed to public/build/assets/freemarker2-a88fb531.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/build/assets/handlebars-93800328.js renamed to public/build/assets/handlebars-84117f8e.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/build/assets/html-168ac41e.js renamed to public/build/assets/html-75950f68.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)