-
-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy pathStatamic.php
471 lines (370 loc) · 11.9 KB
/
Statamic.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
namespace Statamic;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Vite;
use Laravel\Nova\Nova;
use Statamic\Facades\File;
use Statamic\Facades\Preference;
use Statamic\Facades\URL;
use Statamic\Modifiers\Modify;
use Statamic\Support\Arr;
use Statamic\Support\DateFormat;
use Statamic\Support\Str;
use Statamic\Support\TextDirection;
use Statamic\Tags\FluentTag;
use Stringy\StaticStringy;
class Statamic
{
const CORE_SLUG = 'statamic';
const PACKAGE = 'statamic/cms';
protected static $scripts = [];
protected static $externalScripts = [];
protected static $inlineScripts = [];
protected static $styles = [];
protected static $externalStyles = [];
protected static $vites = [];
protected static $cpRoutes = [];
protected static $webRoutes = [];
protected static $actionRoutes = [];
protected static $jsonVariables = [];
protected static $bootedCallbacks = [];
protected static $afterInstalledCallbacks = [];
public static function version()
{
return \Facades\Statamic\Version::get();
}
public static function pro()
{
return config('statamic.editions.pro');
}
public static function enablePro()
{
Artisan::call('statamic:pro:enable', ['--update-config' => true]);
}
public static function availableScripts(Request $request)
{
return static::$scripts;
}
public static function availableExternalScripts(Request $request)
{
return static::$externalScripts;
}
public static function script($name, $path)
{
static::$scripts[$name][] = self::createVersionedAssetPath($name, $path, 'js');
return new static;
}
public static function externalScript($url)
{
static::$externalScripts[] = $url;
return new static;
}
public static function inlineScript($html)
{
static::$inlineScripts[] = $html;
return new static;
}
public static function availableInlineScripts(Request $request)
{
return static::$inlineScripts;
}
public static function availableStyles(Request $request)
{
return static::$styles;
}
public static function availableExternalStyles(Request $request)
{
return static::$externalStyles;
}
public static function style($name, $path)
{
static::$styles[$name][] = self::createVersionedAssetPath($name, $path, 'css');
return new static;
}
public static function externalStyle($url)
{
static::$externalStyles[] = $url;
return new static;
}
public static function vite($name, $config)
{
if (is_string($config) || ! Arr::isAssoc($config)) {
$config = ['input' => $config];
}
static::$vites[$name] = array_merge([
'hotFile' => null,
'buildDirectory' => 'build',
], $config);
return new static;
}
public static function availableVites(Request $request)
{
return static::$vites;
}
public static function pushWebRoutes(Closure $routes)
{
static::$webRoutes[] = $routes;
return new static;
}
public static function pushCpRoutes(Closure $routes)
{
static::$cpRoutes[] = $routes;
return new static;
}
public static function pushActionRoutes(Closure $routes)
{
static::$actionRoutes[] = $routes;
return new static;
}
public static function additionalCpRoutes()
{
foreach (static::$cpRoutes as $routes) {
$routes();
}
}
public static function additionalWebRoutes()
{
foreach (static::$webRoutes as $routes) {
$routes();
}
}
public static function additionalActionRoutes()
{
foreach (static::$actionRoutes as $routes) {
$routes();
}
}
public static function isCpRoute()
{
if (! config('statamic.cp.enabled')) {
return false;
}
$cp = config('statamic.cp.route');
$path = request()->path();
return $path === $cp
|| Str::startsWith($path, Str::finish($cp, '/'));
}
public static function cpRoute($route, $params = [])
{
if (! config('statamic.cp.enabled')) {
return null;
}
$route = route('statamic.cp.'.$route, $params);
// TODO: This is a temporary workaround to routes like
// `route('assets.browse.edit', 'some/image.jpg')` outputting two slashes.
// Can it be fixed with route regex, or is it a laravel bug?
$route = preg_replace('/(?<!:)\/\//', '/', $route);
return $route;
}
public static function isApiRoute()
{
if (! config('statamic.api.enabled') || ! static::pro()) {
return false;
}
return Str::startsWith(request()->path(), config('statamic.api.route'));
}
public static function apiRoute($route, $params = [])
{
if (! config('statamic.api.enabled') || ! static::pro()) {
return null;
}
$route = route('statamic.api.'.$route, $params);
// TODO: This is a temporary workaround to routes like
// `route('assets.browse.edit', 'some/image.jpg')` outputting two slashes.
// Can it be fixed with route regex, or is it a laravel bug?
$route = preg_replace('/(?<!:)\/\//', '/', $route);
return $route;
}
public static function jsonVariables(Request $request)
{
return collect(static::$jsonVariables)->map(function ($variable) use ($request) {
return is_callable($variable) && ! is_string($variable) ? $variable($request) : $variable;
})->all();
}
public static function provideToScript(array $variables)
{
static::$jsonVariables = array_merge(static::$jsonVariables, $variables);
return new static;
}
public static function svg($name, $attrs = null, $fallback = null)
{
if ($attrs) {
$attrs = " class=\"{$attrs}\"";
}
$path = statamic_path("resources/svg/{$name}.svg");
if ($fallback && ! File::exists($path)) {
$path = statamic_path("resources/svg/{$fallback}.svg");
}
$svg = StaticStringy::collapseWhitespace(File::get($path));
return str_replace('<svg', sprintf('<svg%s', $attrs), $svg);
}
public static function vendorAssetUrl($url = '/')
{
return asset(URL::tidy('vendor/'.$url));
}
public static function vendorPackageAssetUrl($package, $url = null, $type = null)
{
// If a vendor URL has already been provided, bypass the rest of the logic.
if (Str::startsWith($url, ['vendor', '/vendor'])) {
return self::vendorAssetUrl(Str::after($url, 'vendor/'));
}
return self::vendorAssetUrl($package.'/'.$type.'/'.$url);
}
public static function cpAssetUrl($url = '/')
{
return static::vendorPackageAssetUrl('statamic/cp', $url);
}
public static function cpViteAsset($asset)
{
return static::cpVite()->asset('resources/'.$asset);
}
public static function cpViteScripts()
{
return static::cpVite()->withEntryPoints([
'resources/js/app.js',
'resources/css/tailwind.css',
]);
}
private static function cpVite()
{
return Vite::getFacadeRoot()
->useHotFile('vendor/statamic/cp/hot')
->useBuildDirectory('vendor/statamic/cp/build');
}
public static function cpDateFormat()
{
return Preference::get('date_format', config('statamic.cp.date_format'));
}
public static function cpDateTimeFormat()
{
$format = self::cpDateFormat();
return DateFormat::containsTime($format) ? $format : $format.' H:i';
}
public static function dateFormat()
{
return config('statamic.system.date_format');
}
public static function dateTimeFormat()
{
$format = self::dateFormat();
return DateFormat::containsTime($format) ? $format : $format.' H:i';
}
public static function flash()
{
if ($success = session('success')) {
$messages[] = ['type' => 'success', 'message' => $success];
}
if ($error = session('error')) {
$messages[] = ['type' => 'error', 'message' => $error];
}
if ($info = session('info')) {
$messages[] = ['type' => 'info', 'message' => $info];
}
return $messages ?? [];
}
public static function crumb(...$values)
{
$arrow = Statamic::cpDirection() === 'ltr' ? ' ‹ ' : ' › ';
return implode($arrow, array_map(fn ($str) => Statamic::trans($str), $values));
}
public static function docsUrl($url)
{
return URL::tidy('https://statamic.dev/'.$url);
}
public static function booted(Closure $callback)
{
static::$bootedCallbacks[] = $callback;
}
public static function runBootedCallbacks()
{
foreach (static::$bootedCallbacks as $callback) {
$callback();
}
static::$bootedCallbacks = [];
}
public static function afterInstalled(Closure $callback)
{
static::$afterInstalledCallbacks[] = $callback;
}
public static function runAfterInstalledCallbacks($command)
{
foreach (static::$afterInstalledCallbacks as $callback) {
$callback($command);
}
static::$afterInstalledCallbacks = [];
}
public static function repository($abstract, $concrete)
{
app()->singleton($abstract, $concrete);
foreach ($concrete::bindings() as $abstract => $concrete) {
app()->bind($abstract, $concrete);
}
}
public static function frontendRouteSegmentRegex()
{
$prefix = '';
if (class_exists(Nova::class)) {
$prefix = '(?!'.trim(Nova::path(), '/').')';
}
return $prefix.'.*';
}
public static function tag($name)
{
return FluentTag::make($name);
}
public static function modify($value)
{
return Modify::value($value);
}
public static function query($name)
{
return app()->make('statamic.queries.'.$name);
}
public static function trans($key, $replace = [], $locale = null)
{
$line = __($key, $replace, $locale);
if (is_array($line)) {
return $key;
}
return $line;
}
public static function isWorker()
{
if (! App::runningInConsole()) {
return false;
}
return Str::startsWith(Arr::get(request()->server(), 'argv.1') ?? '', ['queue:', 'horizon:']);
}
private static function createVersionedAssetPath($name, $path, $extension)
{
// If passing a path versioned by laravel mix, it will contain ?id=
// Do nothing and return that path.
if (Str::contains($path, '?id=')) {
return (string) $path;
}
return Cache::rememberForever("statamic-{$extension}-{$name}-{md5($path)}", function () use ($path, $extension) {
// In case a file without any version will be passed,
// a random version number will be created.
if (! Str::contains($path, '?v=')) {
$version = Str::random();
// Add the file extension if not provided.
$path = Str::finish($path, ".{$extension}");
// Add the version to the path.
$path = Str::finish($path, "?v={$version}");
}
return $path;
});
}
public static function cpLocale(): string
{
return config('app.locale');
}
public static function cpDirection()
{
return TextDirection::of(static::cpLocale());
}
}