Skip to content

Commit cc5b0a6

Browse files
authored
feat(icon): introduce icon component (#1313)
1 parent 5755f80 commit cc5b0a6

27 files changed

+628
-139
lines changed

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"tempest/generation": "self.version",
8484
"tempest/http": "self.version",
8585
"tempest/http-client": "self.version",
86+
"tempest/icon": "self.version",
8687
"tempest/intl": "self.version",
8788
"tempest/log": "self.version",
8889
"tempest/mapper": "self.version",
@@ -118,6 +119,7 @@
118119
"Tempest\\Generation\\": "packages/generation/src",
119120
"Tempest\\HttpClient\\": "packages/http-client/src",
120121
"Tempest\\Http\\": "packages/http/src",
122+
"Tempest\\Icon\\": "packages/icon/src",
121123
"Tempest\\Intl\\": "packages/intl/src",
122124
"Tempest\\Log\\": "packages/log/src",
123125
"Tempest\\Mapper\\": "packages/mapper/src",
@@ -139,6 +141,7 @@
139141
"packages/datetime/src/functions.php",
140142
"packages/debug/src/functions.php",
141143
"packages/event-bus/src/functions.php",
144+
"packages/icon/src/functions.php",
142145
"packages/intl/src/Number/functions.php",
143146
"packages/intl/src/functions.php",
144147
"packages/mapper/src/functions.php",
@@ -176,6 +179,7 @@
176179
"Tempest\\Generation\\Tests\\": "packages/generation/tests",
177180
"Tempest\\HttpClient\\Tests\\": "packages/http-client/tests",
178181
"Tempest\\Http\\Tests\\": "packages/http/tests",
182+
"Tempest\\Icon\\Tests\\": "packages/icon/tests",
179183
"Tempest\\Intl\\Tests\\": "packages/intl/tests",
180184
"Tempest\\Log\\Tests\\": "packages/log/tests",
181185
"Tempest\\Mapper\\Tests\\": "packages/mapper/tests",

packages/cache/src/Commands/CacheClearCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use Tempest\Container\GenericContainer;
1616
use Tempest\Core\ConfigCache;
1717
use Tempest\Core\DiscoveryCache;
18+
use Tempest\Icon\IconCache;
1819
use Tempest\Support\Str;
19-
use Tempest\View\IconCache;
2020
use Tempest\View\ViewCache;
2121

2222
use function Tempest\Support\arr;

packages/cache/src/Commands/CacheStatusCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use Tempest\Core\AppConfig;
1717
use Tempest\Core\ConfigCache;
1818
use Tempest\Core\DiscoveryCache;
19+
use Tempest\Icon\IconCache;
1920
use Tempest\Support\Str;
20-
use Tempest\View\IconCache;
2121
use Tempest\View\ViewCache;
2222
use UnitEnum;
2323

packages/cache/src/InternalCacheInsightsProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Tempest\Core\DiscoveryCache;
77
use Tempest\Core\Insight;
88
use Tempest\Core\InsightsProvider;
9-
use Tempest\View\IconCache;
9+
use Tempest\Icon\IconCache;
1010
use Tempest\View\ViewCache;
1111

1212
final class InternalCacheInsightsProvider implements InsightsProvider

packages/icon/.gitattributes

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Exclude build/test files from the release
2+
.github/ export-ignore
3+
tests/ export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
phpunit.xml export-ignore
7+
README.md export-ignore
8+
9+
# Configure diff output
10+
*.view.php diff=html
11+
*.php diff=php
12+
*.css diff=css
13+
*.html diff=html
14+
*.md diff=markdown

packages/icon/LICENCE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024 Brent Roose [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/icon/composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "tempest/icon",
3+
"description": "A component for working with icons.",
4+
"license": "MIT",
5+
"minimum-stability": "dev",
6+
"require": {
7+
"php": "^8.4",
8+
"tempest/container": "dev-main",
9+
"tempest/http-client": "dev-main",
10+
"tempest/support": "dev-main",
11+
"symfony/cache": "^7.2"
12+
},
13+
"suggest": {
14+
"tempest/event-bus": "For events support"
15+
},
16+
"autoload": {
17+
"files": [
18+
"src/functions.php"
19+
],
20+
"psr-4": {
21+
"Tempest\\Icon\\": "src"
22+
}
23+
},
24+
"autoload-dev": {
25+
"psr-4": {
26+
"Tempest\\Icon\\Tests\\": "tests"
27+
}
28+
}
29+
}

packages/icon/phpunit.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
executionOrder="depends,defects"
7+
beStrictAboutOutputDuringTests="true"
8+
displayDetailsOnPhpunitDeprecations="true"
9+
failOnPhpunitDeprecation="false"
10+
failOnRisky="true"
11+
failOnWarning="true"
12+
>
13+
<testsuites>
14+
<testsuite name="Tempest Icon">
15+
<directory>tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
<source restrictNotices="true" restrictWarnings="true" ignoreIndirectDeprecations="true">
19+
<include>
20+
<directory>src</directory>
21+
</include>
22+
</source>
23+
</phpunit>

packages/icon/src/Icon.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Tempest\Icon;
4+
5+
use Exception;
6+
use Tempest\EventBus\EventBus;
7+
use Tempest\Http\HttpRequestFailed;
8+
use Tempest\Http\Status;
9+
use Tempest\HttpClient\HttpClient;
10+
use Tempest\Icon\IconCache;
11+
use Tempest\Support\Str;
12+
use Tempest\Support\Str\ImmutableString;
13+
14+
final class Icon
15+
{
16+
public function __construct(
17+
private IconCache $iconCache,
18+
private IconConfig $iconConfig,
19+
private HttpClient $http,
20+
private ?EventBus $eventBus = null,
21+
) {}
22+
23+
/**
24+
* Renders an icon as an SVG snippet. If the icon is not cached, it will be
25+
* downloaded on the fly and cached it for future use. If the icon is
26+
* already in the cache, it will be served from there.
27+
*
28+
* This method may return `null` if an error occurred in the process.
29+
*/
30+
public function render(string $icon): ?string
31+
{
32+
[$collection, $iconName] = $this->parseIconIdentifier($icon);
33+
34+
if ($this->iconCache->get("icon-failure-{$collection}-{$iconName}")) {
35+
return null;
36+
}
37+
38+
$svg = $this->iconCache->resolve(
39+
key: "icon-{$collection}-{$iconName}",
40+
callback: fn () => $this->fetchSvg($collection, $iconName),
41+
expiresAt: $this->iconConfig->expiresAfter,
42+
);
43+
44+
if ($this->iconCache->get("icon-failure-{$collection}-{$iconName}")) {
45+
$this->iconCache->delete("icon-{$collection}-{$iconName}");
46+
}
47+
48+
return $svg;
49+
}
50+
51+
/**
52+
* Downloads the icon's SVG file from the Iconify API
53+
*/
54+
private function fetchSvg(string $collection, string $icon): ?string
55+
{
56+
try {
57+
$url = new ImmutableString($this->iconConfig->iconifyApiUrl)
58+
->finish('/')
59+
->append("{$collection}/{$icon}.svg")
60+
->toString();
61+
62+
$response = $this->http->get($url);
63+
64+
if ($response->status !== Status::OK) {
65+
throw new HttpRequestFailed($response->status, cause: $response);
66+
}
67+
68+
$this->eventBus?->dispatch(new IconDownloaded(
69+
collection: $collection,
70+
name: $icon,
71+
icon: $response->body,
72+
));
73+
74+
return $response->body;
75+
} catch (Exception $exception) {
76+
$this->eventBus?->dispatch(new IconDownloadFailed(
77+
collection: $collection,
78+
name: $icon,
79+
exception: $exception,
80+
));
81+
82+
$this->iconCache->put(
83+
key: "icon-failure-{$collection}-{$icon}",
84+
value: true,
85+
expiresAt: $this->iconConfig->retryAfter,
86+
);
87+
88+
return null;
89+
}
90+
}
91+
92+
/**
93+
* @return array{string,string}
94+
*/
95+
private function parseIconIdentifier(string $identifier): ?array
96+
{
97+
if (! Str\contains($identifier, ':')) {
98+
$identifier = Str\replace_first($identifier, '-', ':');
99+
}
100+
101+
$parts = explode(':', $identifier, limit: 2);
102+
103+
if (count($parts) !== 2) {
104+
return null;
105+
}
106+
107+
return $parts;
108+
}
109+
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tempest\View;
3+
namespace Tempest\Icon;
44

55
use Closure;
66
use Psr\Cache\CacheItemInterface;
@@ -15,14 +15,23 @@
1515
final class IconCache
1616
{
1717
public function __construct(
18-
public bool $enabled = false,
18+
public bool $enabled = true,
1919
private ?CacheItemPoolInterface $pool = null,
2020
) {
2121
$this->pool ??= new FilesystemAdapter(
2222
directory: internal_storage_path('cache/icons'),
2323
);
2424
}
2525

26+
public function delete(string $key): void
27+
{
28+
if (! $this->enabled) {
29+
return;
30+
}
31+
32+
$this->pool->deleteItem($key);
33+
}
34+
2635
public function get(string $key): mixed
2736
{
2837
if (! $this->enabled) {

0 commit comments

Comments
 (0)