-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathThemesManager.php
223 lines (190 loc) · 5.7 KB
/
ThemesManager.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
<?php
declare(strict_types=1);
namespace Hexadog\ThemesManager;
use Hexadog\ThemesManager\Exceptions\ThemeNotFoundException;
use Hexadog\ThemesManager\Traits\HasCache;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
class ThemesManager
{
use HasCache;
/**
* Scanned themes.
*/
private Collection $themes;
/**
* The constructor.
*/
public function __construct()
{
if (Config::get('themes-manager.cache.enabled', false)) {
$this->themes = $this->getCache();
} else {
$this->themes = ThemeFinder::find();
}
}
/**
* Get all themes.
*/
public function all(): Collection
{
return $this->themes;
}
/**
* Check if theme with given name exists.
*/
public function has(string $name = null): bool
{
return ! is_null($this->findByName($name, null));
}
/**
* Get theme by name (or return all themes if no name given).
*/
public function get(string $name = null): ?Theme
{
return $this->findByName($name, null);
}
/**
* Set current active theme.
*/
public function set(string $name): ThemesManager
{
if (! $this->has($name)) {
throw new ThemeNotFoundException($name);
}
optional($this->current())->disable();
$this->enable($name);
return $this;
}
/**
* Get current theme.
*/
public function current(): ?Theme
{
return $this->themes
->filter(function ($theme) {
return $theme->enabled();
})->first();
}
/**
* Enable a Theme from its name.
*/
public function enable(string $name, bool $withEvent = true): ThemesManager
{
if ($theme = $this->get($name)) {
$theme->enable($withEvent);
}
return $this;
}
/**
* Disable a Theme from its name.
*/
public function disable(string $name, bool $withEvent = true): ThemesManager
{
if ($theme = $this->get($name)) {
$theme->disable($withEvent);
}
return $this;
}
/**
* Get current theme's asset url.
*/
public function asset(string $asset, bool $absolute = true): string
{
return $this->url($asset, $absolute);
}
/**
* Get current theme's style HTML tag for given asset.
*/
public function style(string $asset, bool $absolute = true): string
{
return sprintf(
'<link media="all" type="text/css" rel="stylesheet" href="%s">',
$this->url($asset, $absolute)
);
}
/**
* Get current theme's script HTML tag for given asset.
*/
public function script(string $asset, string $mode = '', bool $absolute = true, string $type = 'text/javascript', string $level = 'functionality'): string
{
return sprintf(
'<script %s src="%s" data-type="%s" data-level="%s"></script>',
$mode,
$this->url($asset, $absolute),
$type,
$level
);
}
/**
* Get current theme's image HTML tag for given asset.
*/
public function image(string $asset, string $alt = '', string $class = '', array $attributes = [], bool $absolute = true): string
{
return sprintf(
'<img src="%s" alt="%s" class="%s" %s>',
$this->url($asset, $absolute),
$alt,
$class,
$this->htmlAttributes($attributes)
);
}
/**
* Get the current theme path to a versioned Mix file.
*/
public function mix(string $asset, string $manifestDirectory = ''): string
{
return mix($this->url($asset), $manifestDirectory);
}
/**
* Get theme's asset url.
*/
public function url(string $asset, bool $absolute = true): ?string
{
// Split asset name to find concerned theme name
if (Str::contains($asset, '::')) {
$assetParts = explode('::', $asset);
return optional($this->findByName($assetParts[0]))->url($assetParts[1], $absolute);
}
// If no Theme set, return /$asset
if (! $this->current()) {
return Str::start($asset, '/');
}
return $this->current()->url($asset, $absolute);
}
/**
* Find a theme by given name and vendor (optional)
* name can include vendor prefix (ie: hexadog/default)
* If no vendor provided and name not prefixed by vendor
* the first theme with given name is returned.
*/
public function findByName(string $name, string $vendor = null): ?Theme
{
// normalize theme name
$name = str_replace(['-theme', 'theme-'], '', $name);
// Try to find vendor in name
if (($pos = strpos($name, '/')) !== false) {
$vendor = substr($name, 0, $pos);
$name = substr($name, $pos + 1, strlen($name));
}
return $this->themes->first(function ($theme) use ($name, $vendor) {
if (is_null($vendor)) {
return Str::lower($theme->getName()) === Str::lower($name);
}
return Str::lower($theme->getName()) === Str::lower($name) && Str::lower($theme->getVendor()) === Str::lower($vendor);
});
}
/**
* Return attributes in html format.
*/
private function htmlAttributes(array $attributes): string
{
return implode(' ', array_map(function ($key) use ($attributes) {
if (is_bool($attributes[$key])) {
return $attributes[$key] ? $key : '';
}
return $key . '="' . $attributes[$key] . '"';
}, array_keys($attributes)));
}
}