summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Light <samlight1994@gmail.com>2024-06-15 23:03:44 +0100
committerSam Light <samlight1994@gmail.com>2024-06-15 23:03:44 +0100
commit2cc8c1a8ea904ec2de857ede865a7bccc0000de4 (patch)
treeb8613eff0d5c55920252898fd89d287a8c57011c
parentf07af8db3b58bafb840ddcbc23cda5e6644feaff (diff)
Implemented everything
-rw-r--r--config/svg.php4
-rw-r--r--resources/views/components/svg.blade.php2
-rw-r--r--src/Http/Controllers/SvgController.php19
-rw-r--r--src/SvgCollection.php66
-rw-r--r--src/SvgService.php11
-rw-r--r--src/SvgServiceProvider.php11
6 files changed, 98 insertions, 15 deletions
diff --git a/config/svg.php b/config/svg.php
index dc40c50..9e9c33d 100644
--- a/config/svg.php
+++ b/config/svg.php
@@ -1,12 +1,14 @@
<?php
return [
+
'default_collection' => 'default',
'svg_route' => 'svg/{collection}.svg',
+ 'cache_store' => env('SVG_CACHE_DRIVER', config('cache.default')),
'collections' => [
'default' => [
- 'paths' => [].
+ 'paths' => [],
],
],
diff --git a/resources/views/components/svg.blade.php b/resources/views/components/svg.blade.php
index 7bbbebf..940fd62 100644
--- a/resources/views/components/svg.blade.php
+++ b/resources/views/components/svg.blade.php
@@ -4,5 +4,5 @@
'name',
])
<svg {{ $attributes }}>
- <use href="{{ $svg->getCollectionUrl($collection, $name) }}" />
+ <use href="{{ $svg->getSvgUrl($collection, $name) }}" />
</svg>
diff --git a/src/Http/Controllers/SvgController.php b/src/Http/Controllers/SvgController.php
index 5296024..9e9c550 100644
--- a/src/Http/Controllers/SvgController.php
+++ b/src/Http/Controllers/SvgController.php
@@ -4,14 +4,27 @@ namespace Lightscale\LaralightSvg\Http\Controllers;
use Lightscale\LaralightSvg\SvgService;
-use Illuminate\Http\Controller;
+use Illuminate\Routing\Controller;
+use Illuminate\Http\Request;
class SvgController extends Controller
{
- public function serveSvg(SvgService $svg, string $collection)
+ public function serveSvg(SvgService $svg, Request $request, string $collection)
{
- \Log::debug($collection);
+ $collection = $svg->getCollection($collection);
+ if ($collection === null) abort(404);
+
+ $svgContent = $collection->getSvg();
+ if ($svgContent === null) abort(404);
+
+ return response(
+ $svgContent,
+ 200,
+ [
+ 'Content-Type' => 'image/svg+xml',
+ ]
+ );
}
}
diff --git a/src/SvgCollection.php b/src/SvgCollection.php
index 6d7407f..63d7508 100644
--- a/src/SvgCollection.php
+++ b/src/SvgCollection.php
@@ -2,13 +2,16 @@
namespace Lightscale\LaralightSvg;
+use Illuminate\Support\Facades\Storage;
class SvgCollection
{
- private array $paths = [];
+ protected array $paths = [];
+
+ private ?SvgCollectionState $state = null;
public function __construct(
- private string $name
+ private readonly string $name
)
{}
@@ -24,7 +27,64 @@ class SvgCollection
public function getSvgUrl(string $svg): string
{
- return route('laralight-svg.serve-svg', $this->getName());
+ $this->getState()->addSvg($svg);
+ return route('laralight-svg.serve-svg', $this->getName()) . "#{$svg}";
+ }
+
+ protected function getState(): SvgCollectionState
+ {
+ if ($this->state === null) {
+ $this->state = new SvgCollectionState($this->getName());
+ }
+ return $this->state;
+ }
+
+ public function getSvg(): ?string
+ {
+ $state = $this->getState();
+ if ($state->hasPending()) {
+ $this->compileSvg();
+ }
+ return $state->getFullSvg();
+ }
+
+ protected function compileSvg(): void
+ {
+ $state = $this->getState();
+ $svg = $state->getFullSvg();
+ $compiler = new SvgSpriteCompiler($svg);
+
+ if ($compiler->isNew()) {
+ $state->clearExisting(false);
+ }
+
+ $pending = $state->getPending();
+ $added = [];
+ $disks = array_map(
+ fn($p) => Storage::build(['driver' => 'local', 'root' => $p]),
+ array_reverse($this->paths)
+ );
+
+ foreach ($pending as $pendingSvg) {
+ $file = $this->getFile($disks, $pendingSvg);
+ if ($file !== null && $compiler->addSvg($pendingSvg, $file)) {
+ $added[] = $pendingSvg;
+ }
+ }
+
+ $state->clearPending(false);
+ $state->concatExisting($added, false);
+ $state->setFullSvg($compiler->getSvg(), false);
+ $state->push();
+ }
+
+ protected function getFile(array $disks, string $svg): ?string
+ {
+ foreach ($disks as $disk) {
+ $file = $disk->get("{$svg}.svg");
+ if ($file !== null) return $file;
+ }
+ return null;
}
}
diff --git a/src/SvgService.php b/src/SvgService.php
index 436e9ec..4d8afe0 100644
--- a/src/SvgService.php
+++ b/src/SvgService.php
@@ -11,7 +11,7 @@ class SvgService
$collections = config('svg.collections');
foreach ($collections as $k => $collection) {
- $this->addCollectionConfig($k, $collection);
+ $this->addCollectionFromConfig($k, $collection);
}
}
@@ -29,7 +29,6 @@ class SvgService
$this->addCollection($c);
}
-
public function addCollection(SvgCollection $collection): void
{
$this->collections[$collection->getName()] = $collection;
@@ -47,7 +46,13 @@ class SvgService
public function getSvgUrl(string $collection, string $svg): string
{
- return $this->collections[$collection]->getSvgUrl($collection, $svg);
+ return $this->collections[$collection]->getSvgUrl($svg);
}
+ public function getCollection(string $collection): ?SvgCollection
+ {
+ return $this->collections[$collection] ?? null;
+ }
+
+
}
diff --git a/src/SvgServiceProvider.php b/src/SvgServiceProvider.php
index a05effe..2fa6a16 100644
--- a/src/SvgServiceProvider.php
+++ b/src/SvgServiceProvider.php
@@ -11,7 +11,7 @@ use Illuminate\Support\Facades\Route;
class SvgServiceProvider extends ServiceProvider
{
const NAMESPACE = 'laralight-svg';
- const ROOT_PATH = __DIR__ . '../';
+ const ROOT_PATH = __DIR__ . '/..';
public $singletons = [
SvgService::class,
@@ -24,16 +24,19 @@ class SvgServiceProvider extends ServiceProvider
]);
$this->mergeConfigFrom(static::ROOT_PATH . '/config/svg.php', 'svg');
- $this->loadViewsFrom(Service::ROOT_PATH . '/resources/views', static::NAMESPACE);
+ $this->loadViewsFrom(static::ROOT_PATH . '/resources/views', static::NAMESPACE);
$this->publishes([
static::ROOT_PATH . '/config/svg.php' => config_path('svg.php'),
], static::NAMESPACE . '-config');
+ $this->registerRoutes();
+ }
+
+ public function registerRoutes()
+ {
Route::get($this->app['config']->get('svg.svg_route'), [SvgController::class, 'serveSvg'])
->name('laralight-svg.serve-svg');
-
-
}
}