Completed
Push — master ( 0b826a...66f577 )
by Xeriab
03:48
created

AbstractFileParser::findFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Konfig
4
 *
5
 * Yet another simple configuration file loader library.
6
 *
7
 * @author  Xeriab Nabil (aka KodeBurner) <[email protected]>
8
 * @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT
9
 * @link    https://xeriab.github.io/projects/konfig
10
 */
11
12
namespace Exen\Konfig\FileParser;
13
14
class AbstractFileParser implements FileParserInterface
15
{
16
    /**
17
     * Configuration file
18
     *
19
     * @var string
20
     */
21
    protected $file = null;
22
23
    /**
24
     * The configuration variables
25
     *
26
     * @var array
27
     */
28
    protected $vars = [];
29
30
    /**
31
     * Sets up the file to be parsed and variables
32
     *
33
     * @param string $file Config file name
34
     * @param array $vars Variables to parse in the file
35
     * @return void
36
     */
37
    /*public function __construct($file = null, $vars = [])
38
    {
39
    $this->file = $file;
40
41
    $this->vars = [] + $vars;
42
    }*/
43
44
    public function parse($file)
45
    {
46
        // Nothing to put here!
47
    }
48
49
    public function getSupportedFileExtensions()
50
    {
51
        // Nothing to put here!
52
    }
53
54
    #: Protected Methods
55
56
    /**
57
     * Finds the given config files
58
     *
59
     * @param bool $cache
60
     * param bool $multiple Whether to load multiple files or not
61
     * @return array
62
     */
63
    /*protected function findFile($cache = true)
64
    {
65
    // Nothing to put here!
66
    }*/
67
68
    /**
69
     * Parses a string using all of the previously set variables.
70
     * Allows you to use something like %ENV% in non-PHP files.
71
     *
72
     * @param string $string String to parse
73
     * @return string
74
     */
75
    protected function parseVars($string)
76
    {
77
        foreach ($this->vars as $var => $val) {
78
            $string = str_replace("%$var%", $val, $string);
79
        }
80
81
        return $string;
82
    }
83
84
    /**
85
     * Replaces given constants to their string counterparts.
86
     *
87
     * @param array $array Array to be prepped
88
     * @return array Prepped array
89
     */
90
    protected function prepVars(&$array)
91
    {
92
        static $replace = false;
93
94
        if ($replace === false) {
95
            foreach ($this->vars as $x => $v) {
96
                $replace['#^(' . preg_quote($v) . '){1}(.*)?#'] = '%' . $x . '%$2';
97
            }
98
        }
99
100
        foreach ($array as $x => $value) {
101
            if (is_string($value)) {
102
                $array[$x] = preg_replace(
103
                    array_keys($replace),
104
                    array_values($replace),
105
                    $value
106
                );
107
            } elseif (is_array($value)) {
108
                $this->prepVars($array[$x]);
109
            }
110
        }
111
    }
112
}
113
114
#: END OF ./src/FileParser/AbstractFileParser.php FILE
115