Skip to content

Commit e32e819

Browse files
committed
0.1.1
1 parent ec27d90 commit e32e819

File tree

9 files changed

+122
-15
lines changed

9 files changed

+122
-15
lines changed

app/Commands/ParseCommand.php renamed to app/Commands/AutocompleteCommand.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
use Illuminate\Support\Facades\File;
77
use LaravelZero\Framework\Commands\Command;
88

9-
class ParseCommand extends Command
9+
class AutocompleteCommand extends Command
1010
{
11-
protected $signature = 'parse {code} {--debug} {--from-file=}';
11+
protected $signature = 'autocomplete {code} {--debug} {--from-file=}';
1212

13-
protected $description = 'Parse the given PHP code';
13+
protected $description = 'Parse the given PHP code and return the autocomplete results';
1414

1515
public function handle(): void
1616
{
@@ -23,20 +23,21 @@ public function handle(): void
2323
$walker = new Walker($code, (bool) $this->option('debug'));
2424
$result = $walker->walk();
2525

26+
$autocompleting = $result->findAutocompleting();
27+
2628
if (app()->isLocal()) {
27-
$dir = 'local-results/parse';
29+
$dir = 'local-results/autocomplete';
2830
File::ensureDirectoryExists(storage_path($dir));
2931
$now = now()->format('Y-m-d-H-i-s');
3032

31-
File::put(storage_path("{$dir}/result-{$now}.json"), $result->toJson(JSON_PRETTY_PRINT));
33+
File::put(storage_path("{$dir}/full-{$now}.json"), $result->toJson(JSON_PRETTY_PRINT));
34+
File::put(storage_path("{$dir}/autocomplete-{$now}.json"), json_encode($autocompleting->flip(), JSON_PRETTY_PRINT));
3235

3336
if (!$this->option('from-file')) {
34-
File::put(storage_path("{$dir}/result-{$now}.php"), $code);
37+
File::put(storage_path("{$dir}/autocomplete-{$now}.php"), $code);
3538
}
3639
}
3740

38-
$autocompleting = $result->findAutocompleting();
39-
4041
echo json_encode($autocompleting->flip(), $this->option('debug') ? JSON_PRETTY_PRINT : 0);
4142

4243
// dd($autocompleting->flip(), 'Autocompleting');

app/Commands/CompileBinaryCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function handle(): void
3131
confirm('Continue?', true);
3232
}
3333

34+
exec('mv ' . base_path('.env') . ' ' . base_path('.env.bak'));
35+
3436
$extensions = collect([
3537
'bcmath',
3638
'calendar',
@@ -75,5 +77,7 @@ public function handle(): void
7577
echo $output;
7678
});
7779
});
80+
81+
exec('mv ' . base_path('.env.bak') . ' ' . base_path('.env'));
7882
}
7983
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace App\Commands;
4+
5+
use App\Parser\DetectWalker;
6+
use Illuminate\Support\Facades\File;
7+
use LaravelZero\Framework\Commands\Command;
8+
9+
use function Laravel\Prompts\info;
10+
11+
class ContextTypeScriptGeneratorCommand extends Command
12+
{
13+
protected $signature = 'generate-ts';
14+
15+
protected $description = 'Generate TS for Context classes';
16+
17+
public function handle(): void
18+
{
19+
$this->line('namespace AutocompleteParsingResult {');
20+
21+
$classes = collect(glob(base_path('app/Contexts/*.php')))
22+
->filter(fn($file) => !str_contains($file, 'Abstract'));
23+
24+
$this->line("type Result = " . $classes->map(fn($file) => pathinfo($file, PATHINFO_FILENAME))->join(' | ') . ';');
25+
26+
$this->newLine();
27+
28+
$classes->each(function ($file) {
29+
$className = pathinfo($file, PATHINFO_FILENAME);
30+
$namespace = 'App\\Contexts\\' . $className;
31+
32+
$this->line("export interface {$className} {");
33+
34+
$inst = new $namespace;
35+
36+
$reflection = new \ReflectionClass($inst);
37+
38+
$this->line("type: '{$inst->type()}';");
39+
$this->line("parent: Result | null;");
40+
41+
if ($reflection->getProperty('hasChildren')->getValue($inst)) {
42+
$this->line("children: Result[];");
43+
}
44+
45+
$properties = collect($reflection->getProperties(\ReflectionProperty::IS_PUBLIC))
46+
->filter(fn($prop) => !in_array($prop->getName(), [
47+
'children',
48+
'autocompleting',
49+
'freshObject',
50+
'hasChildren',
51+
'parent',
52+
'label'
53+
]))
54+
->map(fn($prop) => [
55+
'name' => $prop->getName(),
56+
'type' => str_replace('App\Contexts\\', '', $prop->getType()?->getName() ?? 'any'),
57+
'default' => $prop->getValue($inst)
58+
])
59+
->each(function ($prop) {
60+
$addon = ($prop['default'] === null) ? ' | null' : '';
61+
$this->line("{$prop['name']}: {$prop['type']}{$addon};");
62+
});
63+
64+
65+
66+
$this->line("}");
67+
$this->newLine();
68+
});
69+
70+
$this->line('}');
71+
}
72+
}

app/Commands/DetectCommand.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public function handle(): void
2020
$code = file_get_contents(__DIR__ . '/../../tests/snippets/detect/' . $this->option('from-file') . '.php');
2121
}
2222

23-
// file_put_contents(__DIR__ . '/code.txt', $this->argument('code'));
24-
2523
$walker = new DetectWalker($code, (bool) $this->option('debug'));
2624
$result = $walker->walk();
2725

app/Contexts/Argument.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,28 @@
44

55
class Argument extends AbstractContext
66
{
7+
public ?string $name = null;
8+
79
public function type(): string
810
{
911
return 'argument';
1012
}
1113

14+
public function castToArray(): array
15+
{
16+
return [
17+
'name' => $this->name,
18+
];
19+
}
20+
1221
public function isAutoCompleting(): bool
1322
{
1423
if ($this->autocompleting) {
1524
return true;
1625
}
1726

1827
return collect($this->children)->first(
19-
fn ($child) => $child->autocompleting
28+
fn($child) => $child->autocompleting
2029
) !== null;
2130
}
2231
}

app/Parser/DetectWalker.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Microsoft\PhpParser\MissingToken;
88
use Microsoft\PhpParser\Node;
99
use Microsoft\PhpParser\Node\Expression\AnonymousFunctionCreationExpression;
10+
use Microsoft\PhpParser\Node\Expression\ArgumentExpression;
1011
use Microsoft\PhpParser\Node\Expression\ArrayCreationExpression;
1112
use Microsoft\PhpParser\Node\Expression\ArrowFunctionCreationExpression;
1213
use Microsoft\PhpParser\Node\Expression\CallExpression;
@@ -66,10 +67,15 @@ public function walk(?Node $node = null)
6667
$this->debug('CALL EXPRESSION', $child::class, $child->getText());
6768
$this->parseCallExpression($child);
6869
}
70+
71+
if ($child instanceof ObjectCreationExpression) {
72+
$this->debug('OBJECT CREATION EXPRESSION', $child::class, $child->getText());
73+
$this->parseObjectCreationExpression($child);
74+
}
6975
}
7076

7177
// TODO: These results are not unique maybe?
72-
return collect($this->items)->unique(fn ($item) => json_encode($item))->values();
78+
return collect($this->items)->unique(fn($item) => json_encode($item))->values();
7379
}
7480

7581
protected function parsePotentialBlade(InlineHtml $node)
@@ -197,6 +203,21 @@ protected function parseCallExpression(CallExpression $node)
197203
}
198204
}
199205

206+
protected function parseObjectCreationExpression(ObjectCreationExpression $node)
207+
{
208+
$item = new DetectedItem;
209+
210+
$item->classUsed = (string) $node->classTypeDesignator->getResolvedName();
211+
212+
foreach ($node->argumentExpressionList->getElements() as $child) {
213+
foreach ($child->getChildNodes() as $argument) {
214+
$item->params[] = $this->parseArgument($argument);
215+
}
216+
}
217+
218+
$this->items[] = $item->toArray();
219+
}
220+
200221
protected function parseQualifiedCallExpression(CallExpression $node)
201222
{
202223
$item = new DetectedItem;
@@ -400,7 +421,7 @@ protected function parseArgument($argument)
400421
return [
401422
'type' => 'string',
402423
'value' => $argument->getStringContentsText(),
403-
// 'index' => $this->context->paramIndex,
424+
'name' => $argument->parent instanceof ArgumentExpression ? $argument->parent->name?->getText($this->sourceFile->getFullText()) : null,
404425
'start' => [
405426
'line' => $range->start->line,
406427
'column' => $range->start->character,

app/Parsers/ArgumentExpressionParser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
class ArgumentExpressionParser extends AbstractParser
1111
{
1212
/**
13-
* @var MethodCall
13+
* @var Argument
1414
*/
1515
protected AbstractContext $context;
1616

1717
public function parse(ArgumentExpression $node)
1818
{
19+
$this->context->name = $node->name?->getText($node->getFileContents());
20+
1921
return $this->context;
2022
}
2123

bin/php-parser-0.1.1

47.2 MB
Binary file not shown.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"homepage": "https://laravel-zero.com",
1212
"type": "project",
1313
"license": "MIT",
14-
"version": "0.1.0",
14+
"version": "0.1.1",
1515
"support": {
1616
"issues": "https://github.com/laravel-zero/laravel-zero/issues",
1717
"source": "https://github.com/laravel-zero/laravel-zero"

0 commit comments

Comments
 (0)