-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathhelpers.php
More file actions
188 lines (150 loc) · 4.61 KB
/
Copy pathhelpers.php
File metadata and controls
188 lines (150 loc) · 4.61 KB
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
<?php
use Illuminate\Container\Container;
use Illuminate\Support\Arr;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Symfony\Component\VarDumper\VarDumper;
use TightenCo\Jigsaw\Support\Vite;
if (! function_exists('app')) {
/**
* Get the available container instance.
*
* @template TClass of object
*
* @param string|class-string<TClass>|null $abstract
* @return ($abstract is class-string<TClass> ? TClass : ($abstract is null ? TightenCo\Jigsaw\Container : mixed))
*/
function app(?string $abstract = null, array $parameters = []): mixed
{
if (is_null($abstract)) {
return TightenCo\Jigsaw\Container::getInstance();
}
return TightenCo\Jigsaw\Container::getInstance()->make($abstract, $parameters);
}
}
function leftTrimPath($path)
{
return ltrim($path, ' \\/');
}
function rightTrimPath($path)
{
return rtrim($path ?? '', ' .\\/');
}
function trimPath($path)
{
return rightTrimPath(leftTrimPath($path));
}
function resolvePath($path)
{
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
$segments = [];
collect(explode(DIRECTORY_SEPARATOR, $path))
->filter(fn ($part) => (string) $part !== '')
->each(function ($part) use (&$segments) {
if ($part == '..') {
array_pop($segments);
} elseif ($part != '.') {
$segments[] = $part;
}
});
return implode(DIRECTORY_SEPARATOR, $segments);
}
/**
* Get the path to the public folder.
*/
function public_path($path = '')
{
$c = Container::getInstance();
$source = Arr::get($c['config'], 'build.source', 'source');
return $source . ($path ? '/' . ltrim($path, '/') : $path);
}
/**
* Get the full path to the source folder.
*/
function source_path($path = '')
{
$c = Container::getInstance();
$source = Arr::get($c['buildPath'], 'source', 'source');
return $source . ($path ? '/' . ltrim($path, '/') : $path);
}
/**
* Get the path to a versioned Elixir file.
*/
function elixir($file, $buildDirectory = 'build')
{
static $manifest;
static $manifestPath;
if (is_null($manifest) || $manifestPath !== $buildDirectory) {
$manifest = json_decode(file_get_contents(public_path($buildDirectory . '/rev-manifest.json')), true);
$manifestPath = $buildDirectory;
}
if (isset($manifest[$file])) {
return '/' . trim($buildDirectory . '/' . $manifest[$file], '/');
}
throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
}
/**
* Get the path to a versioned Mix file.
*/
function mix($path, $manifestDirectory = 'assets')
{
static $manifests = [];
if (! Str::startsWith($path, '/')) {
$path = "/{$path}";
}
if ($manifestDirectory && ! Str::startsWith($manifestDirectory, '/')) {
$manifestDirectory = "/{$manifestDirectory}";
}
if (file_exists(public_path($manifestDirectory . '/hot'))) {
return new HtmlString("//localhost:8080{$path}");
}
$manifestPath = public_path($manifestDirectory . '/mix-manifest.json');
if (! isset($manifests[$manifestPath])) {
if (! file_exists($manifestPath)) {
throw new Exception('The Mix manifest does not exist.');
}
$manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true);
}
$manifest = $manifests[$manifestPath];
if (! isset($manifest[$path])) {
throw new InvalidArgumentException("Unable to locate Mix file: {$path}.");
}
return new HtmlString($manifestDirectory . $manifest[$path]);
}
if (! function_exists('url')) {
function url(string $path): string
{
$c = Container::getInstance();
return trim($c['config']['baseUrl'], '/') . '/' . trim($path, '/');
}
}
if (! function_exists('dd')) {
function dd(...$args)
{
foreach ($args as $x) {
(new VarDumper)->dump($x);
}
exit(1);
}
}
function inline($assetPath)
{
preg_match('/^\/assets\/build\/(css|js)\/.*\.(css|js)/', $assetPath, $matches);
if (! count($matches)) {
throw new InvalidArgumentException("Given asset path is not valid: {$assetPath}");
}
$pathParts = explode('?', $assetPath);
return new HtmlString(file_get_contents("source{$pathParts[0]}"));
}
if (! function_exists('vite_refresh')) {
function vite_refresh()
{
return app(Vite::class)->devServer();
}
}
if (! function_exists('vite')) {
function vite(string $asset, string $assetPath = '/assets/build'): string
{
return app(Vite::class)->url($asset, $assetPath);
}
}