|
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; |
|
13
|
|
|
|
|
14
|
|
|
use Ocubom\Twig\Extension\Svg\Exception\LoaderException; |
|
15
|
|
|
use Ocubom\Twig\Extension\Svg\Loader\LoaderInterface; |
|
16
|
|
|
use Ocubom\Twig\Extension\Svg\Symbol; |
|
17
|
|
|
use Ocubom\Twig\Extension\Svg\Util\DomUtil; |
|
18
|
|
|
use Ocubom\Twig\Extension\Svg\Util\Html5Util; |
|
19
|
|
|
use Psr\Log\LoggerInterface; |
|
20
|
|
|
use Psr\Log\NullLogger; |
|
21
|
|
|
use Twig\Environment; |
|
22
|
|
|
use Twig\Extension\RuntimeExtensionInterface; |
|
23
|
|
|
|
|
24
|
|
|
class SvgRuntime implements RuntimeExtensionInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private LoaderInterface $loader; |
|
27
|
|
|
|
|
28
|
|
|
private LoggerInterface $logger; |
|
29
|
|
|
|
|
30
|
10 |
|
public function __construct(LoaderInterface $loader, LoggerInterface $logger = null) |
|
31
|
|
|
{ |
|
32
|
10 |
|
$this->loader = $loader; |
|
33
|
10 |
|
$this->logger = $logger ?? new NullLogger(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
5 |
|
public function convertToSymbols(Environment $twig, string $html): string |
|
37
|
|
|
{ |
|
38
|
|
|
// Load HTML |
|
39
|
5 |
|
$doc = Html5Util::loadHtml($html); |
|
40
|
|
|
|
|
41
|
|
|
/** @var \DOMElement[] $elements */ |
|
42
|
4 |
|
$elements = []; |
|
43
|
|
|
|
|
44
|
|
|
/** @var \DOMElement[] $symbols */ |
|
45
|
4 |
|
$symbols = []; |
|
46
|
|
|
|
|
47
|
|
|
/** @var \DOMElement $svg */ |
|
48
|
4 |
|
foreach ($doc->getElementsByTagName('svg') as $svg) { |
|
49
|
3 |
|
$symbol = new Symbol($svg, [ |
|
50
|
3 |
|
'debug' => $twig->isDebug(), |
|
51
|
3 |
|
]); |
|
52
|
|
|
|
|
53
|
|
|
// Replace the SVG with reference |
|
54
|
3 |
|
DomUtil::replaceNode($svg, $symbol->getReference()); |
|
55
|
|
|
|
|
56
|
|
|
// Index symbol by id |
|
57
|
3 |
|
$symbols[$symbol->getId()] = $symbols[$symbol->getId()] ?? $symbol->getElement(); |
|
58
|
3 |
|
$elements[] = $svg; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Dump all symbols |
|
62
|
4 |
|
if (count($symbols) > 0) { |
|
63
|
|
|
// Create symbols container element before the end of body tag or DOM |
|
64
|
3 |
|
$node = DomUtil::createElement('svg', '', $doc |
|
65
|
3 |
|
->getElementsByTagName('body') |
|
66
|
3 |
|
->item(0) ?? $doc |
|
67
|
3 |
|
); |
|
68
|
3 |
|
$node->setAttribute('style', 'display:none'); |
|
69
|
|
|
|
|
70
|
|
|
// Add format (duplicate previous node space) on debug mode |
|
71
|
3 |
|
if ($twig->isDebug()) { |
|
72
|
|
|
assert($node->previousSibling instanceof \DOMNode); |
|
73
|
3 |
|
DomUtil::appendChildNode($node->previousSibling, $node); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
uksort($symbols, 'strnatcasecmp'); |
|
77
|
3 |
|
foreach ($symbols as $symbol) { |
|
78
|
3 |
|
DomUtil::appendChildNode($symbol, $node); |
|
79
|
|
|
|
|
80
|
3 |
|
if ($twig->isDebug()) { |
|
81
|
|
|
assert($node->previousSibling instanceof \DOMNode); |
|
82
|
3 |
|
DomUtil::appendChildNode($node->previousSibling, $node); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
if ($twig->isDebug()) { |
|
87
|
|
|
assert($node->parentNode instanceof \DOMNode); |
|
88
|
|
|
|
|
89
|
3 |
|
DomUtil::appendChildNode( |
|
90
|
3 |
|
$doc->createTextNode("\n"), |
|
91
|
3 |
|
$node->parentNode |
|
92
|
3 |
|
); |
|
93
|
|
|
|
|
94
|
3 |
|
$this->logger->info('Converted {element_count} SVG elements into {symbol_count} SVG symbols', [ |
|
95
|
3 |
|
'element_count' => count($elements), |
|
96
|
3 |
|
'element_items' => array_map([DomUtil::class, 'toXml'], $elements), |
|
97
|
3 |
|
'symbol_count' => count($symbols), |
|
98
|
3 |
|
'symbol_items' => array_map([DomUtil::class, 'toXml'], $symbols), |
|
99
|
3 |
|
]); |
|
100
|
|
|
} |
|
101
|
1 |
|
} elseif ($twig->isDebug()) { |
|
102
|
1 |
|
$this->logger->debug('No SVG found'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Generate normalized HTML |
|
106
|
4 |
|
return Html5Util::toHtml($doc); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
5 |
|
public function embedSvg(Environment $twig, string $ident, array $options = []): string |
|
110
|
|
|
{ |
|
111
|
|
|
try { |
|
112
|
5 |
|
$svg = $this->loader->resolve($ident, $options); |
|
113
|
|
|
|
|
114
|
3 |
|
return (string) $svg; |
|
115
|
2 |
|
} catch (LoaderException $err) { |
|
116
|
1 |
|
$this->logger->error($err->getMessage(), ['exception' => $err]); |
|
117
|
|
|
|
|
118
|
1 |
|
if ($twig->isDebug()) { |
|
119
|
1 |
|
return sprintf('<!--{{ svg("%s") }}-->', $ident); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return ''; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|