ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
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'
 

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.

Definition at line 36 of file StringHashParser.php.

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

36  {
37  if (!file_exists($file)) return false;
38  $fh = fopen($file, 'r');
39  if (!$fh) return false;
40  $ret = $this->parseHandle($fh);
41  fclose($fh);
42  return $ret;
43  }
print $file
parseHandle($fh)
Internal parser that acepts a file handle.
+ 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
$fhFile handle with pointer at start of valid string-hash block.

Definition at line 68 of file StringHashParser.php.

References $default, and $ret.

Referenced by parseFile(), and parseMultiFile().

68  {
69  $state = false;
70  $single = false;
71  $ret = array();
72  do {
73  $line = fgets($fh);
74  if ($line === false) break;
75  $line = rtrim($line, "\n\r");
76  if (!$state && $line === '') continue;
77  if ($line === '----') break;
78  if (strncmp('--#', $line, 3) === 0) {
79  // Comment
80  continue;
81  } elseif (strncmp('--', $line, 2) === 0) {
82  // Multiline declaration
83  $state = trim($line, '- ');
84  if (!isset($ret[$state])) $ret[$state] = '';
85  continue;
86  } elseif (!$state) {
87  $single = true;
88  if (strpos($line, ':') !== false) {
89  // Single-line declaration
90  list($state, $line) = explode(':', $line, 2);
91  $line = trim($line);
92  } else {
93  // Use default declaration
94  $state = $this->default;
95  }
96  }
97  if ($single) {
98  $ret[$state] = $line;
99  $single = false;
100  $state = false;
101  } else {
102  $ret[$state] .= "$line\n";
103  }
104  } while (!feof($fh));
105  return $ret;
106  }
+ Here is the caller graph for this function:

◆ parseMultiFile()

HTMLPurifier_StringHashParser::parseMultiFile (   $file)

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

Definition at line 48 of file StringHashParser.php.

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

48  {
49  if (!file_exists($file)) return false;
50  $ret = array();
51  $fh = fopen($file, 'r');
52  if (!$fh) return false;
53  while (!feof($fh)) {
54  $ret[] = $this->parseHandle($fh);
55  }
56  fclose($fh);
57  return $ret;
58  }
print $file
parseHandle($fh)
Internal parser that acepts a file handle.
+ Here is the call graph for this function:

Field Documentation

◆ $default

HTMLPurifier_StringHashParser::$default = 'ID'

Definition at line 31 of file StringHashParser.php.

Referenced by parseHandle().


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