├── .gitignore
├── phpstan.neon
├── phpunit.xml
├── .styleci.yml
├── .travis.yml
├── tests
├── TestCase.php
├── MiddlewareTest.php
├── IconRendererTest.php
└── BladeRendererTest.php
├── LICENSE
├── src
├── FontAwesomeServiceProvider.php
├── SvgParser.php
├── Middleware
│ └── InjectStyleSheet.php
├── config
│ └── fontawesome.php
├── Models
│ ├── Svg.php
│ └── Icon.php
├── BladeRenderer.php
└── IconRenderer.php
├── composer.json
├── readme.md
└── composer.lock
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor/
2 | .phpunit.result.cache
3 |
--------------------------------------------------------------------------------
/phpstan.neon:
--------------------------------------------------------------------------------
1 | parameters:
2 | level: 5
3 | paths:
4 | - src
5 | - tests
6 | ignoreErrors:
7 | # Ignore Laravel/Lumen incompatibilites
8 | - '#Cannot access offset [^\s]+ on Illuminate\\Contracts\\Foundation\\Application#'
9 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | ./tests
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.styleci.yml:
--------------------------------------------------------------------------------
1 | preset: recommended
2 |
3 | risky: true
4 |
5 | disabled:
6 | - align_double_arrow
7 | - concat_without_spaces
8 | - elseif
9 | - phpdoc_align
10 |
11 | enabled:
12 | - native_constant_invocation
13 | - native_function_invocation
14 | - static_lambda
15 |
16 | finder:
17 | exclude:
18 | - vendor
19 | name: "*.php"
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | matrix:
4 | fast_finish: true
5 | include:
6 | - php: 7.2
7 | - php: 7.3
8 | - php: 7.4
9 |
10 | cache:
11 | directories:
12 | - $HOME/.composer/cache
13 |
14 | before_install:
15 | - travis_retry composer self-update
16 |
17 | install:
18 | - travis_retry composer update --no-interaction --prefer-dist --no-suggest
19 |
20 | script:
21 | - vendor/bin/phpunit
22 | - vendor/bin/phpstan analyse
23 |
--------------------------------------------------------------------------------
/tests/TestCase.php:
--------------------------------------------------------------------------------
1 | set('fontawesome.icon_path', __DIR__ . '/../vendor/fortawesome/font-awesome/svgs/');
14 |
15 | // Tests assume svg_href is on by default
16 | $app['config']->set('fontawesome.svg_href', true);
17 | }
18 |
19 | protected function getPackageProviders($app)
20 | {
21 | return [
22 | FontAwesomeServiceProvider::class,
23 | ];
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 - 2020 Jeroen Deviaene
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/src/FontAwesomeServiceProvider.php:
--------------------------------------------------------------------------------
1 | publishes([
12 | __DIR__ . '/config/fontawesome.php' => \config_path('fontawesome.php'),
13 | ]);
14 | $this->mergeConfigFrom(
15 | __DIR__ . '/config/fontawesome.php', 'fontawesome'
16 | );
17 |
18 | $this->registerBladeDirectives();
19 | $this->registerIconRenderer();
20 | }
21 |
22 | private function registerBladeDirectives()
23 | {
24 | $this->app['blade.compiler']->directive('fa', static function ($expression) {
25 | return BladeRenderer::renderGeneric($expression);
26 | });
27 |
28 | foreach (\config('fontawesome.libraries') as $library) {
29 | $this->app['blade.compiler']->directive('fa' . $library[0], static function ($expression) use ($library) {
30 | return BladeRenderer::renderGeneric($expression, $library);
31 | });
32 | }
33 | }
34 |
35 | private function registerIconRenderer()
36 | {
37 | $this->app->singleton(IconRenderer::class);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/SvgParser.php:
--------------------------------------------------------------------------------
1 | view_box = $this->parseViewBox(
14 | $this->getAttribute($xml, 'viewBox')
15 | );
16 | $svg->path = $this->getAttribute($xml, 'd');
17 |
18 | return $svg;
19 | }
20 |
21 | private function parseViewBox(string $str): array
22 | {
23 | $view_box = \array_map(static function (string $part) {
24 | return \intval($part);
25 | }, \explode(' ', $str));
26 |
27 | if (\count($view_box) !== 4) {
28 | throw new Exception("ViewBox should contain 4 numeric values split by spaces. Got \"{$str}\"");
29 | }
30 |
31 | return $view_box;
32 | }
33 |
34 | private function getAttribute(string $xml, string $attribute): ?string
35 | {
36 | $start = \strpos($xml, "{$attribute}=\"") + \strlen($attribute) + 2;
37 | if ($start === -1) {
38 | return null;
39 | }
40 |
41 | $end = \strpos($xml, '"', $start);
42 |
43 | return \substr($xml, $start, $end - $start);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/Middleware/InjectStyleSheet.php:
--------------------------------------------------------------------------------
1 | headers->has('Content-Type') && \strpos($response->headers->get('Content-Type'), 'html') === false)
19 | || $request->getRequestFormat() !== 'html'
20 | || $response->getContent() === false
21 | || $request->isXmlHttpRequest()
22 | ) {
23 | return $response;
24 | }
25 |
26 | return $this->injectStyleSheet($response);
27 | }
28 |
29 | private function injectStyleSheet(SymfonyBaseResponse $response)
30 | {
31 | $content = $response->getContent();
32 | $content = \str_replace('', '', $content);
33 | $response->setContent($content);
34 |
35 | return $response;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jerodev/laravel-font-awesome",
3 | "description": "A Laravel package that renders Font Awesome icons server-side",
4 | "type": "library",
5 | "license": "MIT",
6 | "keywords": [
7 | "Laravel",
8 | "FontAwesome",
9 | "svg"
10 | ],
11 | "homepage": "https://github.com/jerodev/laravel-font-awesome",
12 | "authors": [
13 | {
14 | "name": "Jeroen Deviaene",
15 | "email": "jeroen@deviaene.eu",
16 | "homepage": "https://www.deviaene.eu/",
17 | "role": "Developer"
18 | }
19 | ],
20 | "minimum-stability": "dev",
21 | "prefer-stable" : true,
22 | "autoload": {
23 | "psr-4": {
24 | "Jerodev\\LaraFontAwesome\\": "src/"
25 | }
26 | },
27 | "autoload-dev": {
28 | "psr-4": {
29 | "Jerodev\\LaraFontAwesome\\Tests\\": "tests/"
30 | }
31 | },
32 | "extra": {
33 | "laravel": {
34 | "providers": [
35 | "Jerodev\\LaraFontAwesome\\FontAwesomeServiceProvider"
36 | ]
37 | }
38 | },
39 | "require": {
40 | "illuminate/support": "6.x",
41 | "fortawesome/font-awesome": "^5.12"
42 | },
43 | "require-dev": {
44 | "phpstan/phpstan": "^0.12",
45 | "phpunit/phpunit": "^8.0",
46 | "orchestra/testbench": "4.x"
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/config/fontawesome.php:
--------------------------------------------------------------------------------
1 | \base_path('/vendor/fortawesome/font-awesome/svgs/'),
16 |
17 | /*
18 | |--------------------------------------------------------------------------
19 | | Font Awesome Libraries
20 | |--------------------------------------------------------------------------
21 | |
22 | | These are the font awesome libraries that will be available.
23 | | The order defined here is the order the libraries will be explored in
24 | | when using the @fa() directive.
25 | |
26 | */
27 |
28 | 'libraries' => [
29 | 'regular',
30 | 'brands',
31 | 'solid',
32 | ],
33 |
34 | /*
35 | |--------------------------------------------------------------------------
36 | | Use svg href attribute
37 | |--------------------------------------------------------------------------
38 | |
39 | | The href attribute can be used to link to an already existing svg in the
40 | | DOM, this way the svg code only needs to be loaded once if used multiple
41 | | times.
42 | |
43 | | Very old browser might not support this feature:
44 | | https://caniuse.com/#feat=mdn-svg_elements_use_href
45 | |
46 | */
47 |
48 | 'svg_href' => true,
49 |
50 | ];
51 |
--------------------------------------------------------------------------------
/src/Models/Svg.php:
--------------------------------------------------------------------------------
1 | icon_id = $icon_id;
22 | $this->css_classes = $css_classes;
23 |
24 | $this->view_box = null;
25 | }
26 |
27 | public function render(): string
28 | {
29 | $svg_viewBox = '';
30 | $symbol_start = '';
31 | $symbol_end = '';
32 | if (\config('fontawesome.svg_href')) {
33 | $symbol_start = "icon_id}\" viewBox=\"" . \implode(' ', $this->view_box) . '">';
34 | $symbol_end = "