Ticket #174: parse_control.php

File parse_control.php, 1.8 kB (added by rho, 1 year ago)
Line 
1 <?php
2
3 //$file = file_get_contents('control.file');
4 $file = <<< END
5 #Control file for some plugin
6 Maintainer: Rolando Espinoza La Fuente <rho@prosoftpeople.com>
7 Title: Elgg test plugin
8 Package: elggtest
9 Priority: optional
10 Version: 1.0
11 Depends: elgg (>=0.9)
12 Tags: trackback, blog, integration
13 Description: Brief description
14  Description paragraph 1
15  .
16  Description paragraph 2
17 Conflicts: elgg
18 END;
19
20 $control = parse_plugin_control($file);
21
22 print_r($control);
23
24
25 function parse_plugin_control($string) {
26     $string = preg_replace("#\n+#", "\n", str_replace("\r", "", trim($string))); // crlf fix
27
28     $lines = split("\n", $string);
29
30     $control = array();
31     $lastfield = '';
32
33     foreach ($lines as $line) {
34         if (preg_match("/^[\s\t]*#/", $line)) { // ignore comments
35             continue;
36         }
37         elseif (preg_match("#^[^\t\s]#", $line)) { // not starts with space
38             if (preg_match("#(\w+)\s*\:\s*(.*)\s*$#", $line, $matches)) {
39                 $key = $matches[1];
40                 $val = $matches[2];
41
42                 $control[$key] = $val;
43                 $lastfield = $key;
44             } else {
45                 //error_log("Parse error: invalid plugin control file line\n$line");
46             }
47         }
48         elseif (preg_match("#([\s\t]+)(.*)$#", $line, $matches)){ // allow multilines values
49             if (empty($lastfield)) {
50                 //error_log("Parse error: line without previous entry\n$line");
51             }
52             elseif (trim($matches[2]) == '.') { // just new line?
53                 $control[$lastfield] .= "\n";
54             }
55             else {
56                  // append to last field
57                 $control[$lastfield] .= "\n$matches[2]";
58             }
59         }
60         else {
61             // duh!
62             //error_log("Parse error: invalid plugin control file line\n$line");
63         }
64     }
65
66     return $control;
67 }
68
69 ?>
70