1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of ocubom/twig-svg-extension |
5
|
|
|
* |
6
|
|
|
* © Oscar Cubo Medina <https://ocubom.github.io> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ocubom\Twig\Extension\Svg\Provider\FontAwesome; |
13
|
|
|
|
14
|
|
|
use Ocubom\Twig\Extension\Svg\Exception\LoaderException; |
15
|
|
|
use Ocubom\Twig\Extension\Svg\Provider\FileSystem\FileSystemLoader; |
16
|
|
|
use Ocubom\Twig\Extension\Svg\Svg; |
17
|
|
|
use Ocubom\Twig\Extension\Svg\Util\PathCollection; |
18
|
|
|
|
19
|
|
|
class FontAwesomeLoader extends FileSystemLoader |
20
|
|
|
{ |
21
|
18 |
|
public function __construct(PathCollection $searchPath) |
22
|
|
|
{ |
23
|
18 |
|
parent::__construct($searchPath); |
24
|
|
|
} |
25
|
|
|
|
26
|
5 |
|
public function resolve(string $ident, iterable $options = null): Svg |
27
|
|
|
{ |
28
|
5 |
|
$tokens = preg_split('@\s+@', mb_strtolower($ident)); |
29
|
5 |
|
$tokens[] = FontAwesome::DEFAULT_PREFIX; // Add default prefix as fallback |
30
|
|
|
|
31
|
5 |
|
$errors = []; |
32
|
|
|
|
33
|
5 |
|
foreach ($tokens as $prefix) { |
34
|
5 |
|
$style = FontAwesome::PREFIXES[$prefix] ?? null; |
35
|
5 |
|
if (null === $style) { |
36
|
4 |
|
continue; // Ignore tokens that are *not* a known prefix |
37
|
|
|
} |
38
|
|
|
|
39
|
5 |
|
foreach ($tokens as $name) { |
40
|
5 |
|
if (isset(FontAwesome::PREFIXES[$name])) { |
41
|
4 |
|
continue; // Ignore tokens that are a known prefix |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
$name = (0 === mb_strpos($name, 'fa-')) ? mb_substr($name, 3) : $name; |
45
|
5 |
|
if (empty($name)) { |
46
|
|
|
continue; // @codeCoverageIgnore |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
$path = sprintf('%s/%s', $style, $name); |
50
|
5 |
|
if (array_key_exists($path, $errors)) { |
51
|
1 |
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
try { |
55
|
5 |
|
return new Icon($this->findPath($path), $options); |
56
|
3 |
|
} catch (LoaderException $err) { |
57
|
3 |
|
$errors[sprintf('%s/%s', $style, $name)] = $err; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Could not resolve |
63
|
3 |
|
throw new LoaderException($ident, new \ReflectionClass(__CLASS__), null, 0, $errors); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|