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
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.