1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaloa\Renderer; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use XSLTProcessor; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
final class XmlLegacyRenderer implements RendererInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* @var XmlLegacyRenderer |
16
|
|
|
*/ |
17
|
|
|
private static $myself; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* @var XSLTProcessor |
22
|
|
|
*/ |
23
|
|
|
private $xsltProcessor = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @var Config |
28
|
|
|
*/ |
29
|
|
|
private $config; |
30
|
|
|
|
31
|
1 |
|
public function __construct(Config $config) |
32
|
|
|
{ |
33
|
1 |
|
$this->config = $config; |
34
|
|
|
|
35
|
1 |
|
self::$myself = $this; |
36
|
|
|
|
37
|
1 |
|
$this->initXsltProcessor(); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
1 |
|
private function initXsltProcessor() |
44
|
|
|
{ |
45
|
1 |
|
$xsl = file_get_contents(__DIR__ . '/xmllegacy.xsl'); |
46
|
|
|
|
47
|
1 |
|
$xsl = str_replace('__CLASS__', __CLASS__, $xsl); |
48
|
|
|
|
49
|
1 |
|
$xslDoc = new DOMDocument(); |
50
|
1 |
|
$xslDoc->loadXML($xsl); |
51
|
|
|
|
52
|
1 |
|
$xsltProcessor = new XSLTProcessor(); |
53
|
1 |
|
$xsltProcessor->registerPHPFunctions(); |
54
|
1 |
|
$xsltProcessor->importStyleSheet($xslDoc); |
55
|
|
|
|
56
|
1 |
|
$this->xsltProcessor = $xsltProcessor; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* @param string $input |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
1 |
|
public function render($input) |
65
|
|
|
{ |
66
|
|
|
$xmlCode = '<?xml version="1.0" encoding="utf-8"?> |
67
|
|
|
<!DOCTYPE root [ |
68
|
|
|
<!ENTITY nbsp " "> |
69
|
1 |
|
]>'; |
70
|
|
|
|
71
|
1 |
|
$xmlCode .= '<root>' . $input . '</root>'; |
72
|
|
|
|
73
|
1 |
|
$xmldoc = new DOMDocument(); |
74
|
1 |
|
$xmldoc->loadXML($xmlCode); |
75
|
|
|
|
76
|
1 |
|
$tmp = $this->xsltProcessor->transformToDoc($xmldoc); |
77
|
|
|
|
78
|
1 |
|
$html = $tmp->saveHTML($tmp->documentElement); |
79
|
1 |
|
$html = substr($html, 6, -7); |
80
|
|
|
|
81
|
1 |
|
return $html; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
* @param string $path |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
1 |
|
public static function imageUrl($path) |
|
|
|
|
90
|
|
|
{ |
91
|
1 |
|
if (preg_match('/^https?:\/\//', $path)) { |
92
|
1 |
|
return $path; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return self::$myself->config->getResourceBasePath() . '/' . $path; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* |
100
|
|
|
* @param string $path |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
1 |
|
public static function linkUrl($path) |
|
|
|
|
104
|
|
|
{ |
105
|
1 |
|
if (preg_match('/^(https?:\/\/|mailto:)/', $path)) { |
106
|
1 |
|
return $path; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return self::$myself->config->getResourceBasePath() . '/' . $path; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* |
114
|
|
|
* @todo OMG this method can't be efficient |
115
|
|
|
* |
116
|
|
|
* @param string $source |
117
|
|
|
* @param string $language |
118
|
|
|
* @return DOMElement? |
|
|
|
|
119
|
|
|
*/ |
120
|
1 |
|
public static function highlight($source, $language) |
121
|
|
|
{ |
122
|
|
|
// Smart trim code |
123
|
|
|
/*$source = preg_replace('/(?:\s*\n)?(.*)$/s', '$1', $source); |
|
|
|
|
124
|
|
|
$source = rtrim($source);*/ |
125
|
|
|
|
126
|
1 |
|
$parsed_code = self::$myself->config->getSyntaxHighlighter()->highlight($source, $language); |
127
|
|
|
|
128
|
|
|
$parsed_code = '<?xml version="1.0" encoding="utf-8"?> |
129
|
|
|
<!DOCTYPE root [ |
130
|
|
|
<!ENTITY nbsp " "> |
131
|
1 |
|
]>' . $parsed_code; |
132
|
|
|
|
133
|
1 |
|
$tmp = new DOMDocument(); |
134
|
|
|
|
135
|
1 |
|
$tmp->loadXML($parsed_code); |
136
|
|
|
|
137
|
1 |
|
return $tmp->documentElement; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.