Skip to content

Commit b3631cf

Browse files
committed
wip
1 parent e3cbff4 commit b3631cf

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

src/Tempest/View/src/Components/Icon.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515
use Tempest\View\Elements\ViewComponentElement;
1616
use Tempest\View\IconConfig;
1717
use Tempest\View\ViewComponent;
18-
use function Tempest\get;
1918

2019
final readonly class Icon implements ViewComponent
2120
{
21+
public function __construct(
22+
private AppConfig $appConfig,
23+
private IconCache $iconCache,
24+
private IconConfig $iconConfig,
25+
private HttpClient $http,
26+
) {}
27+
2228
public static function getName(): string
2329
{
2430
return 'x-icon';
@@ -30,7 +36,7 @@ public function compile(ViewComponentElement $element): string
3036
$class = $element->getAttribute('class');
3137

3238
return sprintf(
33-
'<?= %s::render(%s, \'%s\') ?>',
39+
'<?= \Tempest\get(%s::class)->render(%s, \'%s\') ?>',
3440
self::class,
3541
// Having to replace `<?=` is a bit of a hack and should be improved
3642
str_replace(['<?=', '?>'], '', $name),
@@ -45,14 +51,12 @@ public function compile(ViewComponentElement $element): string
4551
* in the cache, it will download it on the fly and cache it for future
4652
* use. If the icon is already in the cache, it will be served from there.
4753
*/
48-
public static function render(string $name, ?string $class): ?string
54+
public function render(string $name, ?string $class): ?string
4955
{
5056
$svg = self::svg($name);
51-
// We can't use injection because we don't have an instance of this view component at runtime. Might be worth refactoring, though.
52-
$appConfig = get(AppConfig::class);
5357

5458
if (! $svg) {
55-
return $appConfig->environment->isLocal()
59+
return $this->appConfig->environment->isLocal()
5660
? ('<!-- unknown-icon: ' . $name . ' -->')
5761
: '';
5862
}
@@ -64,10 +68,8 @@ public static function render(string $name, ?string $class): ?string
6468
return $svg;
6569
}
6670

67-
private static function svg(string $name): ?string
71+
private function svg(string $name): ?string
6872
{
69-
$iconCache = get(IconCache::class);
70-
7173
try {
7274
$parts = explode(':', $name, 2);
7375

@@ -77,12 +79,12 @@ private static function svg(string $name): ?string
7779

7880
[$prefix, $name] = $parts;
7981

80-
return $iconCache->resolve(
82+
return $this->iconCache->resolve(
8183
key: "iconify-{$prefix}-{$name}",
8284
cache: fn () => self::download($prefix, $name),
83-
expiresAt: $iconCache->cacheDuration
85+
expiresAt: $this->iconConfig->cacheDuration
8486
? new DateTimeImmutable()
85-
->add(DateInterval::createFromDateString("{$iconCache->cacheDuration} seconds"))
87+
->add(DateInterval::createFromDateString("{$this->iconConfig->cacheDuration} seconds"))
8688
: null,
8789
);
8890
} catch (Exception) {
@@ -93,18 +95,15 @@ private static function svg(string $name): ?string
9395
/**
9496
* Downloads the icon's SVG file from the Iconify API
9597
*/
96-
private static function download(string $prefix, string $name): ?string
98+
private function download(string $prefix, string $name): ?string
9799
{
98-
$iconConfig = get(IconConfig::class);
99-
$http = get(HttpClient::class);
100-
101100
try {
102-
$url = new ImmutableString($iconConfig->iconifyApiUrl)
101+
$url = new ImmutableString($this->iconConfig->iconifyApiUrl)
103102
->finish('/')
104103
->append("{$prefix}/{$name}.svg")
105104
->toString();
106105

107-
$response = $http->get($url);
106+
$response = $this->http->get($url);
108107

109108
if ($response->status !== Status::OK) {
110109
return null;
@@ -119,7 +118,7 @@ private static function download(string $prefix, string $name): ?string
119118
/**
120119
* Forwards the user-provided class attribute to the SVG element
121120
*/
122-
private static function injectClass(string $svg, string $class): string
121+
private function injectClass(string $svg, string $class): string
123122
{
124123
return new ImmutableString($svg)
125124
->replace(

src/Tempest/View/src/Parser/TempestViewCompiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function compile(string|View $view): string
4848

4949
// 5. Compile to PHP
5050
$compiled = $this->compileElements($elements);
51-
lw($compiled);
51+
5252
return $compiled;
5353
}
5454

0 commit comments

Comments
 (0)