ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
HTMLPurifier_StringHashParser Class Reference

Parses string hash files. More...

+ Collaboration diagram for HTMLPurifier_StringHashParser:

Public Member Functions

 parseFile ($file)
 Parses a file that contains a single string-hash. More...
 
 parseMultiFile ($file)
 Parses a file that contains multiple string-hashes delimited by '-—'. More...
 

Data Fields

 $default = 'ID'
 @type string More...
 

Protected Member Functions

 parseHandle ($fh)
 Internal parser that acepts a file handle. More...
 

Detailed Description

Parses string hash files.

File format is as such:

 DefaultKeyValue
 KEY: Value
 KEY2: Value2
 --MULTILINE-KEY--
 Multiline
 value.

Which would output something similar to:

 array(
     'ID' => 'DefaultKeyValue',
     'KEY' => 'Value',
     'KEY2' => 'Value2',
     'MULTILINE-KEY' => "Multiline\nvalue.\n",
 )

We use this as an easy to use file-format for configuration schema files, but the class itself is usage agnostic.

You can use -— to forcibly terminate parsing of a single string-hash; this marker is used in multi string-hashes to delimit boundaries.

Definition at line 28 of file StringHashParser.php.

Member Function Documentation

◆ parseFile()

HTMLPurifier_StringHashParser::parseFile (   $file)

Parses a file that contains a single string-hash.

Parameters
string$file
Returns
array

Definition at line 41 of file StringHashParser.php.

42 {
43 if (!file_exists($file)) {
44 return false;
45 }
46 $fh = fopen($file, 'r');
47 if (!$fh) {
48 return false;
49 }
50 $ret = $this->parseHandle($fh);
51 fclose($fh);
52 return $ret;
53 }
print $file
parseHandle($fh)
Internal parser that acepts a file handle.

References $file, $ret, and parseHandle().

+ Here is the call graph for this function:

◆ parseHandle()

HTMLPurifier_StringHashParser::parseHandle (   $fh)
protected

Internal parser that acepts a file handle.

Note
While it's possible to simulate in-memory parsing by using custom stream wrappers, if such a use-case arises we should factor out the file handle into its own class.
Parameters
resource$fhFile handle with pointer at start of valid string-hash block.
Returns
array

Definition at line 86 of file StringHashParser.php.

87 {
88 $state = false;
89 $single = false;
90 $ret = array();
91 do {
92 $line = fgets($fh);
93 if ($line === false) {
94 break;
95 }
96 $line = rtrim($line, "\n\r");
97 if (!$state && $line === '') {
98 continue;
99 }
100 if ($line === '----') {
101 break;
102 }
103 if (strncmp('--#', $line, 3) === 0) {
104 // Comment
105 continue;
106 } elseif (strncmp('--', $line, 2) === 0) {
107 // Multiline declaration
108 $state = trim($line, '- ');
109 if (!isset($ret[$state])) {
110 $ret[$state] = '';
111 }
112 continue;
113 } elseif (!$state) {
114 $single = true;
115 if (strpos($line, ':') !== false) {
116 // Single-line declaration
117 list($state, $line) = explode(':', $line, 2);
118 $line = trim($line);
119 } else {
120 // Use default declaration
121 $state = $this->default;
122 }
123 }
124 if ($single) {
125 $ret[$state] = $line;
126 $single = false;
127 $state = false;
128 } else {
129 $ret[$state] .= "$line\n";
130 }
131 } while (!feof($fh));
132 return $ret;
133 }

References $default, and $ret.

Referenced by parseFile(), and parseMultiFile().

+ Here is the caller graph for this function:

◆ parseMultiFile()

HTMLPurifier_StringHashParser::parseMultiFile (   $file)

Parses a file that contains multiple string-hashes delimited by '-—'.

Parameters
string$file
Returns
array

Definition at line 60 of file StringHashParser.php.

61 {
62 if (!file_exists($file)) {
63 return false;
64 }
65 $ret = array();
66 $fh = fopen($file, 'r');
67 if (!$fh) {
68 return false;
69 }
70 while (!feof($fh)) {
71 $ret[] = $this->parseHandle($fh);
72 }
73 fclose($fh);
74 return $ret;
75 }

References $file, $ret, and parseHandle().

+ Here is the call graph for this function:

Field Documentation

◆ $default

HTMLPurifier_StringHashParser::$default = 'ID'

@type string

Definition at line 34 of file StringHashParser.php.

Referenced by parseHandle().


The documentation for this class was generated from the following file: