Passed
Push — master ( 51c049...256bda )
by Sebastian
03:56
created

Highlighter::php()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * File containing the {@see AppUtils\Highlighter} class.
4
 *
5
 * @package Application Utils
6
 * @subpackage Highlighter
7
 * @see AppUtils\Highlighter
8
 */
9
10
namespace AppUtils;
11
12
use GeSHi;
13
use DOMDocument;
14
15
/**
16
 * Syntax highlighter helper: Uses GeSHi and other ways to add syntax
17
 * highlighting to a range of formats. Adds some GeSHi factory methods.
18
 *
19
 * Usage:
20
 * 
21
 * Parsing source code from a string or file
22
 * 
23
 * <pre>
24
 * $highlighted = Highlighter::fromString($xml, 'xml');
25
 * $highlighted = Highlighter::fromFile('/path/to/file.xml', 'xml');
26
 * </pre>
27
 * 
28
 * Creating a GeSHi instance from a string or file
29
 * 
30
 * <pre>
31
 * $geshi = Highlighter::fromString($xml, 'xml');
32
 * $geshi = Highlighter::fromFile('/path/to/file.xml', 'xml');
33
 * </pre>
34
 * 
35
 * Other, more specialized formats are available in the
36
 * according format methods, e.g. <code>json()</code>, <code>url()</code>.
37
 *
38
 * @package Application Utils
39
 * @subpackage Highlighter
40
 * @author Sebastian Mordziol <[email protected]>
41
 */
42
class Highlighter
43
{
44
   /**
45
    * Creates a new GeSHi instance from a source code string.
46
    * 
47
    * @param string $sourceCode
48
    * @param string $format
49
    * @return GeSHi
50
    */
51
    public static function fromString(string $sourceCode, string $format) : GeSHi
52
    {
53
        return new GeSHi($sourceCode, $format);
54
    }
55
    
56
   /**
57
    * Creates a new GeSHi instance from the contents of a file.
58
    * 
59
    * @param string $path
60
    * @param string $format
61
    * @return GeSHi
62
    */
63
    public static function fromFile(string $path, string $format) : GeSHi
64
    {
65
        return self::fromString(FileHelper::readContents($path), $format);
66
    }
67
    
68
   /**
69
    * Parses and highlights the target string.
70
    * 
71
    * @param string $sourceCode
72
    * @param string $format
73
    * @return string
74
    */
75
    public static function parseString(string $sourceCode, string $format) : string
76
    {
77
        return self::fromString($sourceCode, $format)->parse_code();
78
    }
79
    
80
   /**
81
    * Parses and highlights the contents of the target file.
82
    * 
83
    * @param string $path
84
    * @param string $format
85
    * @return string
86
    */
87
    public static function parseFile(string $path, string $format) : string
88
    {
89
        return self::fromFile($path, $format)->parse_code();
90
    }
91
    
92
   /**
93
    * Adds HTML syntax highlighting to the specified SQL string.
94
    *
95
    * @param string $sql
96
    * @return string
97
    */
98
    public static function sql(string $sql) : string
99
    {
100
        return self::parseString($sql, 'sql');
101
    }
102
    
103
   /**
104
    * Adds HTML syntax highlighting to a JSON string, or a data array/object.
105
    *
106
    * @param array|object|string $subject A JSON string, or data array/object to convert to JSON to highlight.
107
    * @return string
108
    */
109
    public static function json($subject) : string
110
    {
111
        if(!is_string($subject))
112
        {
113
            $subject = json_encode($subject, JSON_PRETTY_PRINT);
114
        }
115
        
116
        $subject = str_replace('\/', '/', $subject);
117
        
118
        return self::parseString($subject, 'javascript');
119
    }
120
    
121
   /**
122
    * Adds HTML syntax highlighting to the specified XML code.
123
    *
124
    * @param string $xml The XML to highlight.
125
    * @param bool $formatSource Whether to format the source with indentation to make it readable.
126
    * @return string
127
    */
128
    public static function xml(string $xml, bool $formatSource=false) : string
129
    {
130
        if($formatSource)
131
        {
132
            $dom = new DOMDocument();
133
            $dom->preserveWhiteSpace = false;
134
            $dom->formatOutput = true;
135
            
136
            $dom->loadXML($xml);
137
            
138
            $xml = $dom->saveXML();
139
        }
140
        
141
        return self::parseString($xml, 'xml');
142
    }
143
    
144
   /**
145
    * Adds HTML syntax highlighting to the specified HTML code.
146
    * 
147
    * @param string $html
148
    * @param bool $formatSource
149
    * @return string
150
    */
151
    public static function html(string $html, bool $formatSource=false) : string
152
    {
153
        if($formatSource)
154
        {
155
            $dom = new DOMDocument();
156
            $dom->preserveWhiteSpace = false;
157
            $dom->formatOutput = true;
158
            
159
            $dom->loadHTML($html);
160
            
161
            $html = $dom->saveHTML();
162
        }
163
        
164
        return self::parseString($html, 'xml');
165
    }
166
    
167
   /**
168
    * Adds HTML syntax highlighting to a bit of PHP code.
169
    * 
170
    * @param string $phpCode
171
    * @return string
172
    */
173
    public static function php(string $phpCode) : string
174
    {
175
        return self::parseString($phpCode, 'php');
176
    }
177
    
178
   /**
179
    * Adds HTML syntax highlighting to an URL.
180
    *
181
    * NOTE: Includes the necessary CSS styles. When
182
    * highlighting several URLs in the same page,
183
    * prefer using the `parseURL` function instead.
184
    *
185
    * @param string $url
186
    * @return string
187
    */
188
    public static function url(string $url) : string
189
    {
190
        $info = parseURL($url);
191
        
192
        return
193
        '<style>'.$info->getHighlightCSS().'</style>'.
194
        $info->getHighlighted();
195
    }
196
}
197