-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemesController.php
More file actions
202 lines (167 loc) · 6.43 KB
/
Copy pathThemesController.php
File metadata and controls
202 lines (167 loc) · 6.43 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace App\Http\Controllers;
use App\Models\Theme;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Inertia\Inertia;
use Spatie\Tags\Tag;
class ThemesController extends Controller
{
public function create()
{
return Inertia::render('themes/create');
}
public function store(Request $request)
{
$request->validate([
'url' => ['required', 'url'],
]);
$response = Http::get($request->url);
if ($response->failed()) {
return back()->withErrors(['url' => 'Could not fetch registry from the provided URL.']);
}
$data = $response->json();
if (empty($data) || ! is_array($data)) {
return back()->withErrors(['url' => 'Invalid registry JSON format.']);
}
$errors = [];
if (! is_string($data['name'] ?? null) || $data['name'] === '') {
$errors[] = 'The registry must contain a non-empty "name" field.';
} elseif (! preg_match('/^[a-z0-9\-]+$/i', $data['name'])) {
$errors[] = 'The theme name must only contain letters, numbers, and hyphens.';
}
if (isset($data['cssVars'])) {
if (! is_array($data['cssVars'])) {
$errors[] = '"cssVars" must be an object.';
} else {
if (isset($data['cssVars']['light']) && ! is_array($data['cssVars']['light'])) {
$errors[] = '"cssVars.light" must be an object.';
}
if (isset($data['cssVars']['dark']) && ! is_array($data['cssVars']['dark'])) {
$errors[] = '"cssVars.dark" must be an object.';
}
}
}
if (isset($data['files'])) {
if (! is_array($data['files'])) {
$errors[] = '"files" must be an array.';
} else {
$fileErrors = Theme::validateFiles($data['files']);
foreach ($fileErrors as $error) {
$errors[] = $error;
}
}
}
if (isset($data['font'])) {
if (! is_array($data['font'])) {
$errors[] = '"font" must be an object.';
} else {
foreach (['family', 'provider', 'import', 'variable', 'selector', 'dependency'] as $field) {
if (isset($data['font'][$field]) && ! is_string($data['font'][$field])) {
$errors[] = "\"font.{$field}\" must be a string.";
}
}
foreach (['weight', 'subsets'] as $field) {
if (isset($data['font'][$field]) && ! is_array($data['font'][$field])) {
$errors[] = "\"font.{$field}\" must be an array.";
}
}
}
}
if (isset($data['tags'])) {
if (! is_array($data['tags'])) {
$errors[] = '"tags" must be an array.';
} else {
foreach ($data['tags'] as $i => $tag) {
if (! is_string($tag)) {
$errors[] = "\"tags.{$i}\" must be a string.";
}
}
}
}
if (! empty($errors)) {
return back()->withErrors(['url' => implode(' ', $errors)]);
}
$data['name'] = Str::kebab($data['name']);
$data['type'] = 'registry:theme';
if (! ($data['author'] ?? null)) {
$host = Str::of(parse_url($request->url, PHP_URL_HOST))
->replaceFirst('www.', '')
->before('.')
->toString();
$data['author'] = $host;
}
if (Theme::where('name', $data['name'])->exists()) {
return back()->withErrors(['url' => "A theme named [{$data['name']}] already exists."]);
}
$theme = Theme::fromRegistry($data);
$theme->user_id = auth()->id();
$theme->save();
if (isset($data['tags'])) {
$theme->attachTags($data['tags']);
}
Cache::forget('themes:total_count');
Cache::forget('themes:available_tags');
return redirect()->route('themes.show', $theme->name)
->with('success', 'Theme created successfully.');
}
public function index()
{
$availableTags = Cache::remember('themes:available_tags', 3600, function () {
return Tag::query()
->whereExists(function ($query) {
$query->select(\Illuminate\Support\Facades\DB::raw(1))
->from('taggables')
->whereColumn('taggables.tag_id', 'tags.id')
->where('taggables.taggable_type', Theme::class);
})
->get()
->pluck('name')
->sort()
->values()
->all();
});
$query = Theme::query()->with('tags');
if ($search = request('search')) {
$query->where(function ($q) use ($search) {
$q->where('name', 'like', "%{$search}%")
->orWhere('title', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%");
});
}
if ($tag = request('tag')) {
$query->withAnyTags([$tag]);
}
$themes = $query->paginate(12)->withQueryString();
$themes->getCollection()->transform(function ($theme) {
$data = $theme->toArray();
$data['tags'] = $theme->tags->pluck('name')->toArray();
return $data;
});
return Inertia::render('themes/index', [
'themes' => Inertia::scroll($themes),
'filters' => request()->only(['search', 'tag']),
'availableTags' => $availableTags,
'totalThemesCount' => Cache::remember('themes:total_count', 3600, fn () => Theme::count()),
]);
}
public function show(Theme $theme)
{
$theme->load('tags');
$data = $theme->toArray();
$data['tags'] = $theme->tags->pluck('name')->toArray();
return Inertia::render('themes/show', [
'theme' => $data,
'css' => $theme->toCss(),
]);
}
public function css(string $name)
{
return response(
Theme::where('name', $name)->firstOrFail()->toCss(),
200
)->header('Content-Type', 'text/css');
}
}