From 0c3a6be8e29c02529485cc9eebafe8687acd547c Mon Sep 17 00:00:00 2001 From: Simon Depelchin Date: Wed, 28 Feb 2018 13:34:55 +0100 Subject: [PATCH] Moving from internal path to url so that it's easier and always works. --- README.md | 33 +++++++--------------------- config/config.php | 9 ++++---- src/Fa5Factory.php | 54 ++++++---------------------------------------- src/Icon.php | 4 ++-- 4 files changed, 20 insertions(+), 80 deletions(-) diff --git a/README.md b/README.md index b519c10..570f090 100644 --- a/README.md +++ b/README.md @@ -19,19 +19,17 @@ Publish the Blade Font Awesome 5 config file: php artisan vendor:publish --provider="Depsimon\BladeFa5\BladeFa5ServiceProvider" ``` -Go to https://fontawesome.com/ and download the latest version (Free or Pro). Unzip the package and go to `fontawesome-free-5.0.7/advanced-options/svg-sprites`. - -Copy all the files from there to you `resources/assets/svg` directory. +Download the [Font Awesome 5](https://fontawesome.com/) latest version (Free or Pro) and put the SVG sprites in your `public/svg` directory. ## Configuration -Inside `config/blade-fa5.php`, you can specify the spritesheets path, the default weight and the default classes for icons. +Inside `config/blade-fa5.php`, you can specify the spritesheets directory, the default weight and the default classes for icons. ```php 'resources/assets/svg/', + 'spritesheets_url' => 'svg/', 'weight' => 'far', 'class' => 'icon inline-block fill-current', ]; @@ -39,29 +37,14 @@ return [ ## Basic usage -First, make sure to include the spritesheets that you'll use in your template with the `fa5_spritesheets()` helper: - -```html - - - - - - - - - {{ fa5_spritesheets('far', 'fas', 'fab') }} - - -``` - -To insert a Font Awesome icon in your template, simply use the `@fa5` Blade directive, passing the name of the icon and optionally the weight and then any additional classes: +You can insert an icon anywhere in your template with the `@fa5` Blade directive. +You pass the name, then the weight, the classes and any additional classes: ```html @fa5('cog') -@fa5('facebook', 'fab') -@fa5('facebook', 'fab', 'text-blue') -@fa5('spinner', 'fal', 'text-grey', ['spin']) +@fa5('user', 'fas') +@fa5('facebook', 'fab', 'text-blue') +@fa5('spinner', 'fal', 'text-grey', ['spin']) ``` ## Credits diff --git a/config/config.php b/config/config.php index f3fe424..80833ed 100644 --- a/config/config.php +++ b/config/config.php @@ -2,16 +2,15 @@ return [ /* |-------------------------------------------------------------------------- - | Spritesheets Path + | Spritesheets Directory URI |-------------------------------------------------------------------------- | | This value is the path to the directory of the Font Awesome 5 - | spritesheets. This path is then resolved internally. Please - | ensure this value is set relative to the root directory - | and not the public directory. + | spritesheets. This path is then resolved as a URI. Please + | ensure this value is set relative to the public directory. | */ - 'spritesheets_path' => 'resources/assets/svg/', + 'spritesheets_url' => 'svg/', /* |-------------------------------------------------------------------------- diff --git a/src/Fa5Factory.php b/src/Fa5Factory.php index dd835eb..3269d5d 100644 --- a/src/Fa5Factory.php +++ b/src/Fa5Factory.php @@ -2,7 +2,6 @@ namespace Depsimon\BladeFa5; -use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Blade; use Illuminate\Support\HtmlString; @@ -10,9 +9,9 @@ class Fa5Factory { - private $files; private $config = [ - 'weight' => 'far' + 'weight' => 'far', + 'spritesheets_url' => 'svg/' ]; const WEIGHT_SPRITESHEETS = [ @@ -22,10 +21,9 @@ class Fa5Factory 'fab' => 'fa-brands.svg' ]; - public function __construct($config = [], $filesystem = null) + public function __construct($config = []) { $this->config = Collection::make(array_merge($this->config, $config)); - $this->files = $filesystem ?: new Filesystem; } public function registerBladeTag() @@ -35,50 +33,10 @@ public function registerBladeTag() }); } - public function spritesheets(...$weights) + public function spritesheetUrl($weight) { - $weights = array_flatten($weights); - - if (empty($weights)) { - $weights = [$this->config['weight']]; - } - - return new HtmlString( - sprintf( - '
%s
', - $this->spritesheetsContents($weights) - ) - ); - } - - private function spritesheetsContents($weights) - { - $contents = ''; - foreach ($weights as $weight) { - $contents .= $this->spritesheetContent($weight); - } - - return $contents; - } - - private function spritesheetContent($weight) - { - return cache()->rememberForever("fa5-{$weight}-spritesheet", function () use ($weight) { - $dom = new \DomDocument; - $dom->loadXML(file_get_contents($this->spritesheetPath($weight))); - - foreach ($dom->getElementsByTagName('symbol') as $symbol) { - $symbol->setAttribute('id', $weight . '-' . $symbol->getAttribute('id')); - } - - return $dom->saveHTML(); - }); - } - - public function spritesheetPath($weight) - { - return str_finish($this->config->get('spritesheets_path', function () { - throw new Exception('No spritesheets_path set!'); + return url($this->config->get('spritesheets_url', function () { + throw new Exception('No spritesheets_url set!'); }), '/') . $this->spritesheetName($weight); } diff --git a/src/Icon.php b/src/Icon.php index 3e36707..0d0f40d 100644 --- a/src/Icon.php +++ b/src/Icon.php @@ -38,9 +38,9 @@ public function __call($method, $args) public function renderFromSprite() { - return vsprintf('', [ + return vsprintf('', [ $this->renderAttributes(), - $this->iconWeight, + $this->factory->spritesheetUrl($this->iconWeight), $this->iconName ]); }