diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | composer.json | 17 | ||||
| -rw-r--r-- | config/svg.php | 13 | ||||
| -rw-r--r-- | resources/views/components/svg.blade.php | 8 | ||||
| -rw-r--r-- | src/Http/Controllers/SvgController.php | 17 | ||||
| -rw-r--r-- | src/SvgCollection.php | 30 | ||||
| -rw-r--r-- | src/SvgService.php | 53 | ||||
| -rw-r--r-- | src/SvgServiceProvider.php | 39 | 
8 files changed, 178 insertions, 0 deletions
| diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22d0d82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8ddfbd8 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ +    "name": "lightscale/laralight-svg", +    "description": "Laravel SVG sprites", +    "type": "library", +    "autoload": { +        "psr-4": { +            "Lightscale\\LaralightSvg\\": "src/" +        } +    }, +    "authors": [ +        { +            "name": "Sam Light", +            "email": "sam@lightscale.co.uk" +        } +    ], +    "require": {} +} diff --git a/config/svg.php b/config/svg.php new file mode 100644 index 0000000..dc40c50 --- /dev/null +++ b/config/svg.php @@ -0,0 +1,13 @@ +<?php + +return [ +    'default_collection' => 'default', +    'svg_route' => 'svg/{collection}.svg', + +    'collections' => [ +        'default' => [ +            'paths' => []. +        ], +    ], + +]; diff --git a/resources/views/components/svg.blade.php b/resources/views/components/svg.blade.php new file mode 100644 index 0000000..7bbbebf --- /dev/null +++ b/resources/views/components/svg.blade.php @@ -0,0 +1,8 @@ +@inject('svg', 'Lightscale\LaralightSvg\SvgService') +@props([ +    'collection', +    'name', +]) +<svg {{ $attributes }}> +    <use href="{{ $svg->getCollectionUrl($collection, $name) }}" /> +</svg> diff --git a/src/Http/Controllers/SvgController.php b/src/Http/Controllers/SvgController.php new file mode 100644 index 0000000..5296024 --- /dev/null +++ b/src/Http/Controllers/SvgController.php @@ -0,0 +1,17 @@ +<?php + +namespace Lightscale\LaralightSvg\Http\Controllers; + +use Lightscale\LaralightSvg\SvgService; + +use Illuminate\Http\Controller; + +class SvgController extends Controller +{ + +    public function serveSvg(SvgService $svg, string $collection) +    { +        \Log::debug($collection); +    } + +} diff --git a/src/SvgCollection.php b/src/SvgCollection.php new file mode 100644 index 0000000..6d7407f --- /dev/null +++ b/src/SvgCollection.php @@ -0,0 +1,30 @@ +<?php + +namespace Lightscale\LaralightSvg; + + +class SvgCollection +{ +    private array $paths = []; + +    public function __construct( +        private string $name +    ) +    {} + +    public function addPath(string $path) +    { +        $this->paths[] = $path; +    } + +    public function getName(): string +    { +        return $this->name; +    } + +    public function getSvgUrl(string $svg): string +    { +        return route('laralight-svg.serve-svg', $this->getName()); +    } + +} diff --git a/src/SvgService.php b/src/SvgService.php new file mode 100644 index 0000000..436e9ec --- /dev/null +++ b/src/SvgService.php @@ -0,0 +1,53 @@ +<?php + +namespace Lightscale\LaralightSvg; + +class SvgService +{ +    protected array $collections = []; + +    public function __construct() +    { +        $collections = config('svg.collections'); + +        foreach ($collections as $k => $collection) { +            $this->addCollectionConfig($k, $collection); +        } +    } + +    protected function addCollectionFromConfig( +        string $collectionName, +        array $collectionConfig +    ): void +    { +        $c = new SvgCollection($collectionName); + +        foreach ($collectionConfig['paths'] as $p) { +            $c->addPath($p); +        } + +        $this->addCollection($c); +    } + + +    public function addCollection(SvgCollection $collection): void +    { +        $this->collections[$collection->getName()] = $collection; +    } + +    public function registerCollection(string $collection): void +    { +        $this->addCollection(new SvgCollection($collection)); +    } + +    public function addCollectionPath(string $collection, string $path): void +    { +        $this->collections[$collection]->addPath($path); +    } + +    public function getSvgUrl(string $collection, string $svg): string +    { +        return $this->collections[$collection]->getSvgUrl($collection, $svg); +    } + +} diff --git a/src/SvgServiceProvider.php b/src/SvgServiceProvider.php new file mode 100644 index 0000000..a05effe --- /dev/null +++ b/src/SvgServiceProvider.php @@ -0,0 +1,39 @@ +<?php + +namespace Lightscale\LaralightSvg; + +use Lightscale\LaralightSvg\Http\Controllers\SvgController; + +use Illuminate\Foundation\Console\AboutCommand; +use Illuminate\Support\ServiceProvider; +use Illuminate\Support\Facades\Route; + +class SvgServiceProvider extends ServiceProvider +{ +    const NAMESPACE = 'laralight-svg'; +    const ROOT_PATH = __DIR__ . '../'; + +    public $singletons = [ +        SvgService::class, +    ]; + +    public function boot() +    { +        AboutCommand::add('Laralight SVG', fn() => [ +            'Version' => 'dev' +        ]); + +        $this->mergeConfigFrom(static::ROOT_PATH . '/config/svg.php', 'svg'); +        $this->loadViewsFrom(Service::ROOT_PATH . '/resources/views', static::NAMESPACE); + +        $this->publishes([ +            static::ROOT_PATH . '/config/svg.php' => config_path('svg.php'), +        ], static::NAMESPACE . '-config'); + +        Route::get($this->app['config']->get('svg.svg_route'), [SvgController::class, 'serveSvg']) +             ->name('laralight-svg.serve-svg'); + + +    } + +} | 
