Skip to content

Commit

Permalink
Moving from internal path to url so that it's easier and always works.
Browse files Browse the repository at this point in the history
  • Loading branch information
depsimon committed Feb 28, 2018
1 parent a24cd33 commit 0c3a6be
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 80 deletions.
33 changes: 8 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,32 @@ 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
<?php

return [
'spritesheets_path' => 'resources/assets/svg/',
'spritesheets_url' => 'svg/',
'weight' => 'far',
'class' => 'icon inline-block fill-current',
];
```

## Basic usage

First, make sure to include the spritesheets that you'll use in your template with the `fa5_spritesheets()` helper:

```html
<!-- layout.blade.php -->

<!DOCTYPE html>
<html lang="en">
<head><!-- ... --></head>
<body>
<!-- ... -->

{{ fa5_spritesheets('far', 'fas', 'fab') }}
</body>
</html>
```

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') <!-- Weight is "solid" -->
@fa5('facebook', 'fab', 'text-blue') <!-- Weight is "brands" and apply "text-blue" class -->
@fa5('spinner', 'fal', 'text-grey', ['spin']) <!-- Add the "spin" attribute -->
```

## Credits
Expand Down
9 changes: 4 additions & 5 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/',

/*
|--------------------------------------------------------------------------
Expand Down
54 changes: 6 additions & 48 deletions src/Fa5Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

namespace Depsimon\BladeFa5;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\HtmlString;

class Fa5Factory
{

private $files;
private $config = [
'weight' => 'far'
'weight' => 'far',
'spritesheets_url' => 'svg/'
];

const WEIGHT_SPRITESHEETS = [
Expand All @@ -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()
Expand All @@ -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(
'<div style="height: 0; width: 0; position: absolute; visibility: hidden;">%s</div>',
$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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function __call($method, $args)

public function renderFromSprite()
{
return vsprintf('<svg%s><use xmlns:xlink="http://www.w3.org/1999/xlink" href="#%s-%s"></use></svg>', [
return vsprintf('<svg%s><use xmlns:xlink="http://www.w3.org/1999/xlink" href="%s#%s"></use></svg>', [
$this->renderAttributes(),
$this->iconWeight,
$this->factory->spritesheetUrl($this->iconWeight),
$this->iconName
]);
}
Expand Down

0 comments on commit 0c3a6be

Please sign in to comment.