Skip to content

Commit

Permalink
feat: add support for default prefix (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhedger authored Nov 23, 2024
1 parent d1fa0cb commit cde7bf3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions src/IconRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Unicon;

use Illuminate\Support\Str;

class IconRenderer
{
public function __construct(
Expand All @@ -15,14 +13,17 @@ public function __construct(

public function render(string $name): ?string
{
if (! Str::contains($name, ':')) {
$parts = explode(':', $name, 2);

$prefix = count($parts) === 2 ? $parts[0] : config('unicon.defaults.prefix');
$name = count($parts) === 2 ? $parts[1] : $name;

if (!$prefix) {
throw new \InvalidArgumentException(
'The name must be in the format "prefix:name".',
'The prefix must be specified either in the name of the icon or in the configuration.',
);
}

[$prefix, $name] = explode(':', $name, 2);

$icon = match ($isInCache = $this->cache->has($prefix, $name)) {
true => $this->cache->pull($prefix, $name),
false => $this->downloader->download($prefix, $name),
Expand Down
16 changes: 16 additions & 0 deletions test/Feature/IconRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Unicon\Test\Feature;

use Illuminate\Support\Facades\Config;
use Unicon\IconCache;
use Unicon\IconDownloader;
use Unicon\IconRenderer;
Expand Down Expand Up @@ -46,4 +47,19 @@ public function it_retrieves_the_icon_from_the_cache_on_subsequent_renders()
$renderer->render('heroicons:clock'); // downloads the icon
$renderer->render('heroicons:clock'); // pulls the icon from the cache
}

#[Test]
public function test_it_uses_the_defaullt_prefix_if_the_prefix_is_not_specified()
{
$this->partialMock(IconDownloader::class)
->shouldReceive('download')
->once()
->with('heroicons', 'clock')
->andReturn($this->icon);

Config::set('unicon.defaults.prefix', 'heroicons');

$renderer = $this->app->make(IconRenderer::class);
$renderer->render('clock');
}
}

0 comments on commit cde7bf3

Please sign in to comment.