Skip to content

Commit 6b934aa

Browse files
committed
Use php-templates from vs-code
1 parent 9f2a32f commit 6b934aa

File tree

7 files changed

+336
-9
lines changed

7 files changed

+336
-9
lines changed

php-templates/LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Taylor Otwell
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

php-templates/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The templates here are based on the official VS Code extension by Laravel https://github.com/laravel/vs-code-extension
2+
3+
Modifications:
4+
- return instead of echo
5+
- do not serialize to JSON

php-templates/configs.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
$local = collect(glob(config_path("/*.php")))
4+
->merge(glob(config_path("**/*.php")))
5+
->map(fn ($path) => [
6+
(string) \Illuminate\Support\Str::of($path)
7+
->replace([config_path("/"), ".php"], "")
8+
->replace("/", "."),
9+
$path
10+
]);
11+
12+
$vendor = collect(glob(base_path("vendor/**/**/config/*.php")))->map(fn (
13+
$path
14+
) => [
15+
(string) \Illuminate\Support\Str::of($path)
16+
->afterLast("/config/")
17+
->replace(".php", "")
18+
->replace("/", "."),
19+
$path
20+
]);
21+
22+
$configPaths = $local
23+
->merge($vendor)
24+
->groupBy(0)
25+
->map(fn ($items)=>$items->pluck(1));
26+
27+
$cachedContents = [];
28+
$cachedParsed = [];
29+
30+
function vsCodeGetConfigValue($value, $key, $configPaths) {
31+
$parts = explode(".", $key);
32+
$toFind = $key;
33+
$found = null;
34+
35+
while (count($parts) > 0) {
36+
array_pop($parts);
37+
$toFind = implode(".", $parts);
38+
39+
if ($configPaths->has($toFind)) {
40+
$found = $toFind;
41+
break;
42+
}
43+
}
44+
45+
if ($found === null) {
46+
return null;
47+
}
48+
49+
$file = null;
50+
$line = null;
51+
52+
if ($found === $key) {
53+
$file = $configPaths->get($found)[0];
54+
} else {
55+
foreach ($configPaths->get($found) as $path) {
56+
$cachedContents[$path] ??= file_get_contents($path);
57+
$cachedParsed[$path] ??= token_get_all($cachedContents[$path]);
58+
59+
$keysToFind = \Illuminate\Support\Str::of($key)
60+
->replaceFirst($found, "")
61+
->ltrim(".")
62+
->explode(".");
63+
64+
if (is_numeric($keysToFind->last())) {
65+
$index = $keysToFind->pop();
66+
67+
if ($index !== "0") {
68+
return null;
69+
}
70+
71+
$key = collect(explode(".", $key));
72+
$key->pop();
73+
$key = $key->implode(".");
74+
$value = "array(...)";
75+
}
76+
77+
$nextKey = $keysToFind->shift();
78+
$expectedDepth = 1;
79+
80+
$depth = 0;
81+
82+
foreach ($cachedParsed[$path] as $token) {
83+
if ($token === "[") {
84+
$depth++;
85+
}
86+
87+
if ($token === "]") {
88+
$depth--;
89+
}
90+
91+
if (!is_array($token)) {
92+
continue;
93+
}
94+
95+
$str = trim($token[1], '"\'');
96+
97+
if (
98+
$str === $nextKey &&
99+
$depth === $expectedDepth &&
100+
$token[0] === T_CONSTANT_ENCAPSED_STRING
101+
) {
102+
$nextKey = $keysToFind->shift();
103+
$expectedDepth++;
104+
105+
if ($nextKey === null) {
106+
$file = $path;
107+
$line = $token[2];
108+
break;
109+
}
110+
}
111+
}
112+
113+
if ($file) {
114+
break;
115+
}
116+
}
117+
}
118+
119+
return [
120+
"name" => $key,
121+
"value" => $value,
122+
"file" => $file === null ? null : str_replace(base_path('/'), '', $file),
123+
"line" => $line
124+
];
125+
}
126+
127+
return collect(\Illuminate\Support\Arr::dot(config()->all()))
128+
->map(fn ($value, $key) => vsCodeGetConfigValue($value, $key, $configPaths))
129+
->filter()
130+
->values();

php-templates/routes.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
function vsCodeGetRouterReflection(\Illuminate\Routing\Route $route)
4+
{
5+
if ($route->getActionName() === 'Closure') {
6+
return new \ReflectionFunction($route->getAction()['uses']);
7+
}
8+
9+
if (!str_contains($route->getActionName(), '@')) {
10+
return new \ReflectionClass($route->getActionName());
11+
}
12+
13+
try {
14+
return new \ReflectionMethod($route->getControllerClass(), $route->getActionMethod());
15+
} catch (\Throwable $e) {
16+
$namespace = app(\Illuminate\Routing\UrlGenerator::class)->getRootControllerNamespace()
17+
?? (app()->getNamespace() . 'Http\Controllers');
18+
19+
return new \ReflectionMethod(
20+
$namespace . '\\' . ltrim($route->getControllerClass(), '\\'),
21+
$route->getActionMethod(),
22+
);
23+
}
24+
}
25+
26+
return collect(app('router')->getRoutes()->getRoutes())
27+
->map(function (\Illuminate\Routing\Route $route) {
28+
try {
29+
$reflection = vsCodeGetRouterReflection($route);
30+
} catch (\Throwable $e) {
31+
$reflection = null;
32+
}
33+
34+
return [
35+
'method' => collect($route->methods())->filter(function ($method) {
36+
return $method !== 'HEAD';
37+
})->implode('|'),
38+
'uri' => $route->uri(),
39+
'name' => $route->getName(),
40+
'action' => $route->getActionName(),
41+
'parameters' => $route->parameterNames(),
42+
'filename' => $reflection ? $reflection->getFileName() : null,
43+
'line' => $reflection ? $reflection->getStartLine() : null,
44+
];
45+
})
46+
;

php-templates/views.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
function vsCodeFindBladeFiles($path)
4+
{
5+
$paths = [];
6+
7+
if (!is_dir($path)) {
8+
return $paths;
9+
}
10+
11+
foreach (
12+
\Symfony\Component\Finder\Finder::create()
13+
->files()
14+
->name("*.blade.php")
15+
->in($path)
16+
as $file
17+
) {
18+
$paths[] = [
19+
"path" => str_replace(base_path(DIRECTORY_SEPARATOR), '', $file->getRealPath()),
20+
"isVendor" => str_contains($file->getRealPath(), base_path("vendor")),
21+
"key" => \Illuminate\Support\Str::of($file->getRealPath())
22+
->replace(realpath($path), "")
23+
->replace(".blade.php", "")
24+
->ltrim(DIRECTORY_SEPARATOR)
25+
->replace(DIRECTORY_SEPARATOR, ".")
26+
];
27+
}
28+
29+
return $paths;
30+
}
31+
$paths = collect(
32+
app("view")
33+
->getFinder()
34+
->getPaths()
35+
)->flatMap(function ($path) {
36+
return vsCodeFindBladeFiles($path);
37+
});
38+
39+
$hints = collect(
40+
app("view")
41+
->getFinder()
42+
->getHints()
43+
)->flatMap(function ($paths, $key) {
44+
return collect($paths)->flatMap(function ($path) use ($key) {
45+
return collect(vsCodeFindBladeFiles($path))->map(function ($value) use (
46+
$key
47+
) {
48+
return array_merge($value, ["key" => "{$key}::{$value["key"]}"]);
49+
});
50+
});
51+
});
52+
53+
[$local, $vendor] = $paths
54+
->merge($hints)
55+
->values()
56+
->partition(function ($v) {
57+
return !$v["isVendor"];
58+
});
59+
60+
return $local
61+
->sortBy("key", SORT_NATURAL)
62+
->merge($vendor->sortBy("key", SORT_NATURAL));

resources/views/meta.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
]));
2222
<?php endforeach; ?>
2323

24+
<?php foreach ($configMethods as $method) : ?>
25+
override(<?= $method ?>, map([
26+
<?php foreach ($configValues as $name => $value) : ?>
27+
'<?= $name ?>' => '<?= $value ?>',
28+
<?php endforeach; ?>
29+
]));
30+
<?php endforeach; ?>
31+
2432
<?php if (count($factories)) : ?>
2533
override(\factory(0), map([
2634
'' => '@FactoryBuilder',
@@ -65,14 +73,20 @@
6573
override(\tap(0), type(0));
6674
override(\optional(0), type(0));
6775

76+
<?php if (isset($expectedArgumentSets)): ?>
77+
<?php foreach ($expectedArgumentSets as $name => $argumentsList) : ?>
78+
registerArgumentsSet('<?= $name ?>', <?php foreach ($argumentsList as $i => $arg) : ?><?php if($i % 5 == 0) {
79+
echo "\n";
80+
} ?><?= var_export($arg, true); ?>,<?php endforeach; ?>);
81+
<?php endforeach; ?>
82+
<?php endif ?>
83+
6884
<?php if (isset($expectedArguments)) : ?>
6985
<?php foreach ($expectedArguments as $function => $arguments) : ?>
70-
<?php foreach ($arguments as $index => $argumentList) : ?>
71-
expectedArguments(\<?= $function ?>(), <?= $index ?>,<?php foreach ($argumentList as $i => $arg) : ?><?php if($i % 10 == 0) {
72-
echo "\n";
73-
} ?><?= var_export($arg, true); ?>,<?php endforeach; ?>
74-
);
86+
<?php foreach ($arguments as $index => $argumentSet) : ?>
87+
expectedArguments(<?= $function ?>, <?= $index ?>, argumentsSet('<?= $argumentSet ?>'));
7588
<?php endforeach; ?>
7689
<?php endforeach; ?>
7790
<?php endif; ?>
91+
7892
}

0 commit comments

Comments
 (0)