Completed
Push — psr2-config ( c6639e...e063ba )
by Andreas
03:24
created

ConfigParser::parse()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 4
nop 1
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\plugin\config\core;
4
5
/**
6
 * A naive PHP file parser
7
 *
8
 * This parses our very simple config file in PHP format. We use this instead of simply including
9
 * the file, because we want to keep expressions such as 24*60*60 as is.
10
 *
11
 * @author  Chris Smith <[email protected]>
12
 */
13
class ConfigParser {
14
    /** @var string variable to parse from the file */
15
    protected $varname;
16
    /** @var string the key to mark sub arrays */
17
    protected $keymarker;
18
19
    /**
20
     * ConfigParser constructor.
21
     *
22
     * @param string $varname variable to parse from the file
23
     * @param string $keymarker the key to mark sub arrays
24
     */
25
    public function __construct($varname = 'conf', $keymarker = '____') {
26
        $this->varname = $varname;
27
        $this->keymarker = $keymarker;
28
    }
29
30
    /**
31
     * Parse the given PHP file into an array
32
     *
33
     * When the given files does not exist, this returns an empty array
34
     *
35
     * @param string $file
36
     * @return array
37
     */
38
    public function parse($file) {
39
        if(!file_exists($file)) return array();
40
41
        $config = array();
42
        $contents = @php_strip_whitespace($file);
43
        $pattern = '/\$' . $this->varname . '\[[\'"]([^=]+)[\'"]\] ?= ?(.*?);(?=[^;]*(?:\$' . $this->varname . '|$))/s';
44
        $matches = array();
45
        preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER);
46
47
        for($i = 0; $i < count($matches); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
48
            $value = $matches[$i][2];
49
50
            // merge multi-dimensional array indices using the keymarker
51
            $key = preg_replace('/.\]\[./', $this->keymarker, $matches[$i][1]);
52
53
            // handle arrays
54
            if(preg_match('/^array ?\((.*)\)/', $value, $match)) {
55
                $arr = explode(',', $match[1]);
56
57
                // remove quotes from quoted strings & unescape escaped data
58
                $len = count($arr);
59
                for($j = 0; $j < $len; $j++) {
60
                    $arr[$j] = trim($arr[$j]);
61
                    $arr[$j] = $this->readValue($arr[$j]);
62
                }
63
64
                $value = $arr;
65
            } else {
66
                $value = $this->readValue($value);
67
            }
68
69
            $config[$key] = $value;
70
        }
71
72
        return $config;
73
    }
74
75
    /**
76
     * Convert php string into value
77
     *
78
     * @param string $value
79
     * @return bool|string
80
     */
81
    protected function readValue($value) {
82
        $removequotes_pattern = '/^(\'|")(.*)(?<!\\\\)\1$/s';
83
        $unescape_pairs = array(
84
            '\\\\' => '\\',
85
            '\\\'' => '\'',
86
            '\\"' => '"'
87
        );
88
89
        if($value == 'true') {
90
            $value = true;
91
        } elseif($value == 'false') {
92
            $value = false;
93
        } else {
94
            // remove quotes from quoted strings & unescape escaped data
95
            $value = preg_replace($removequotes_pattern, '$2', $value);
96
            $value = strtr($value, $unescape_pairs);
97
        }
98
        return $value;
99
    }
100
101
}
102