4 * Parses string hash files. File format is as such:
13 * Which would output something similar to:
16 * 'ID' => 'DefaultKeyValue',
19 * 'MULTILINE-KEY' => "Multiline\nvalue.\n",
22 * We use this as an easy to use file-format for configuration schema
23 * files, but the class itself is usage agnostic.
25 * You can use ---- to forcibly terminate parsing of a single string-hash;
26 * this marker is used in multi string-hashes to delimit boundaries.
28 class HTMLPurifier_StringHashParser
34 public $default = 'ID';
37 * Parses a file that contains a single string-hash.
41 public function parseFile($file)
43 if (!file_exists($file)) {
46 $fh = fopen($file, 'r');
50 $ret = $this->parseHandle($fh);
56 * Parses a file that contains multiple string-hashes delimited by '----'
60 public function parseMultiFile($file)
62 if (!file_exists($file)) {
66 $fh = fopen($file, 'r');
71 $ret[] = $this->parseHandle($fh);
78 * Internal parser that acepts a file handle.
79 * @note While it's possible to simulate in-memory parsing by using
80 * custom stream wrappers, if such a use-case arises we should
81 * factor out the file handle into its own class.
82 * @param resource $fh File handle with pointer at start of valid string-hash
86 protected function parseHandle($fh)
93 if ($line === false) {
96 $line = rtrim($line, "\n\r");
97 if (!$state && $line === '') {
100 if ($line === '----') {
103 if (strncmp('--#', $line, 3) === 0) {
106 } elseif (strncmp('--', $line, 2) === 0) {
107 // Multiline declaration
108 $state = trim($line, '- ');
109 if (!isset($ret[$state])) {
115 if (strpos($line, ':') !== false) {
116 // Single-line declaration
117 list($state, $line) = explode(':', $line, 2);
120 // Use default declaration
121 $state = $this->default;
125 $ret[$state] = $line;
129 $ret[$state] .= "$line\n";
131 } while (!feof($fh));
136 // vim: et sw=4 sts=4