1
|
|
|
<?php |
2
|
|
|
requireComponents("Adapters"); |
3
|
|
|
jRequire("../JException/JException.php"); |
4
|
|
|
class Parser { |
5
|
|
|
private static function setParser ( $_type ) { |
6
|
|
|
$parser = null; |
|
|
|
|
7
|
|
|
$_type = strtolower($_type); |
8
|
|
|
switch ($_type) { |
9
|
|
|
case "twig": |
10
|
|
|
case "sql": |
11
|
|
|
$parser = new TwigAdapter(); |
12
|
|
|
break; |
13
|
|
|
case "jate": |
14
|
|
|
$parser = new JTagAdapter(); |
15
|
|
|
break; |
16
|
|
|
case 'jade': |
17
|
|
|
case "pug": |
18
|
|
|
$parser = new PugAdapter(); |
19
|
|
|
break; |
20
|
|
|
case "md": |
21
|
|
|
case "markdown": |
22
|
|
|
case "parsedown": |
23
|
|
|
$parser = new ParsedownAdapter(); |
24
|
|
|
break; |
25
|
|
|
default: |
26
|
|
|
$parser = -1; |
27
|
|
|
break; |
28
|
|
|
} |
29
|
|
|
return $parser; |
30
|
|
|
} |
31
|
|
|
public static function parseText( $_text, $_parameters = [], $_type = "html" ) { |
32
|
|
|
if(!is_string($_text) || !is_string($_type)) |
33
|
|
|
throw new JException("Parameter must be a string."); |
34
|
|
|
if(!is_array($_parameters)) |
35
|
|
|
throw new JException("Parameter must be an array."); |
36
|
|
|
$parser = self::setParser($_type); |
37
|
|
|
if($parser === -1) |
38
|
|
|
return $_text; |
39
|
|
|
return $parser->drawText($_text, $_parameters); |
40
|
|
|
} |
41
|
|
|
public static function parseFileMan( $_path, $_parameters = [], $_type = "html" ) { |
42
|
|
|
if(!is_string($_path)) |
43
|
|
|
throw new JException("Parameter must be a string."); |
44
|
|
|
if(!file_exists($_path)) |
45
|
|
|
throw new JException("File [$_path] not found."); |
46
|
|
|
$string = file_get_contents($_path); |
47
|
|
|
try { |
48
|
|
|
$text = self::parseText($string, $_parameters, $_type); |
49
|
|
|
} catch (Exception $e) { |
50
|
|
|
throw new JException($e->getMessage()); |
51
|
|
|
} |
52
|
|
|
return $text; |
53
|
|
|
} |
54
|
|
|
public static function parseFile( $_path, $_parameters = [] ) { |
55
|
|
|
if(!is_string($_path)) |
56
|
|
|
throw new JException("Parameter must be a string."); |
57
|
|
|
$extension = explode(".", $_path); |
58
|
|
|
$extension = $extension[count($extension)-1]; |
59
|
|
|
$extension = strtolower($extension); |
60
|
|
|
try { |
61
|
|
|
$text = self::parseFileMan($_path, $_parameters, $extension); |
62
|
|
|
} catch (Exception $e) { |
63
|
|
|
throw new JException($e->getMessage()); |
64
|
|
|
} |
65
|
|
|
return $text; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
?> |
69
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.