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\Loader\LoaderInterface; |
16
|
|
|
use Ocubom\Twig\Extension\Svg\Util\DomUtil; |
17
|
|
|
use Ocubom\Twig\Extension\Svg\Util\Html5Util; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use Psr\Log\NullLogger; |
20
|
|
|
use Twig\Environment; |
21
|
|
|
use Twig\Extension\RuntimeExtensionInterface; |
22
|
|
|
|
23
|
|
|
use function BenTools\IterableFunctions\iterable_to_array; |
24
|
|
|
|
25
|
|
|
class FontAwesomeRuntime implements RuntimeExtensionInterface |
26
|
|
|
{ |
27
|
|
|
private LoaderInterface $loader; |
28
|
|
|
private LoggerInterface $logger; |
29
|
|
|
|
30
|
3 |
|
public function __construct(LoaderInterface $loader, LoggerInterface $logger = null) |
31
|
|
|
{ |
32
|
3 |
|
$this->loader = $loader; |
33
|
3 |
|
$this->logger = $logger ?? new NullLogger(); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
public function replaceIcons(Environment $twig, string $html): string |
37
|
|
|
{ |
38
|
|
|
// Load HTML |
39
|
2 |
|
$doc = Html5Util::loadHtml($html); |
40
|
|
|
|
41
|
2 |
|
$query = implode(' | ', array_map( |
42
|
2 |
|
function ($class) { |
43
|
2 |
|
return sprintf( |
44
|
2 |
|
'descendant-or-self::*[@class and contains(concat(\' \', normalize-space(@class), \' \'), \' %s \')]', |
45
|
2 |
|
$class |
46
|
2 |
|
); |
47
|
2 |
|
}, |
48
|
2 |
|
array_keys(FontAwesome::PREFIXES) |
49
|
2 |
|
)); |
50
|
|
|
|
51
|
|
|
/** @var \DOMNode $node */ |
52
|
2 |
|
foreach (DomUtil::query($query, $doc) as $node) { |
53
|
2 |
|
if ($node instanceof \DOMElement) { |
54
|
|
|
try { |
55
|
2 |
|
if ($node->hasAttribute('data-fa-transform')) { |
56
|
|
|
continue; // Ignore icons with Power Transforms (use svg+js) |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
if ($twig->isDebug()) { |
60
|
2 |
|
DomUtil::createComment(DomUtil::toHtml($node), $node, true); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
$icon = $this->loader->resolve( |
64
|
|
|
// Resolve icon with class … |
65
|
2 |
|
$node->getAttribute('class'), |
66
|
|
|
// … and clone all its attributes as options |
67
|
2 |
|
iterable_to_array(DomUtil::getElementAttributes($node)) |
68
|
2 |
|
); |
69
|
|
|
|
70
|
|
|
// Replace node |
71
|
1 |
|
DomUtil::replaceNode($node, $icon->getElement()); |
72
|
1 |
|
} catch (LoaderException $err) { |
73
|
1 |
|
$this->logger->notice($err->getMessage(), ['exception' => $err]); |
74
|
|
|
|
75
|
1 |
|
DomUtil::removeNode($node); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Generate normalized HTML |
81
|
2 |
|
return Html5Util::toHtml($doc); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function renderHtmlTag(string $icon, array $options = []): string |
85
|
|
|
{ |
86
|
1 |
|
$icon = $this->loader->resolve($icon, $options); |
87
|
|
|
assert($icon instanceof FontAwesomeSvg); |
88
|
|
|
|
89
|
1 |
|
return DomUtil::toHtml($icon->getHtmlTag($options)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|