Completed
Push — master ( b102f6...0b826a )
by Xeriab
03:52
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
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: exportFormat, loadFile
Loading history...
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37
    public function __construct($file = null, $vars = [])
38
    {
39
        $this->file = $file;
40
41
        $this->vars = [] + $vars;
42
    }
43
44
    public function load($overwrite = false, $cache = true)
0 ignored issues
show
Unused Code introduced by
The parameter $overwrite is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $cache is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        // Nothing to put here!
47
    }
48
49
    public function parse($file)
50
    {
51
        // Nothing to put here!
52
    }
53
54
    public function save($contents)
0 ignored issues
show
Unused Code introduced by
The parameter $contents is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        // Nothing to put here!
57
    }
58
59
    /**
60
     * Gets the default group name.
61
     *
62
     * @return string
63
     */
64
    public function group()
65
    {
66
        return $this->file;
67
    }
68
69
    public function getSupportedFileExtensions()
70
    {
71
        // Nothing to put here!
72
    }
73
74
    #: Protected Methods
75
76
    /**
77
     * Finds the given config files
78
     *
79
     * @param bool $cache
80
     * param bool $multiple Whether to load multiple files or not
81
     * @return array
82
     */
83
    protected function findFile($cache = true)
0 ignored issues
show
Unused Code introduced by
The parameter $cache is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
        // Nothing to put here!
86
    }
87
88
    /**
89
     * Parses a string using all of the previously set variables.
90
     * Allows you to use something like %ENV% in non-PHP files.
91
     *
92
     * @param string $string String to parse
93
     * @return string
94
     */
95
    protected function parseVars($string)
96
    {
97
        foreach ($this->vars as $var => $val) {
98
            $string = str_replace("%$var%", $val, $string);
99
        }
100
101
        return $string;
102
    }
103
104
    /**
105
     * Replaces given constants to their string counterparts.
106
     *
107
     * @param array $array Array to be prepped
108
     * @return array Prepped array
109
     */
110
    protected function prepVars(&$array)
111
    {
112
        static $replace = false;
113
114
        if ($replace === false) {
115
            foreach ($this->vars as $x => $v) {
116
                $replace['#^('.preg_quote($v).'){1}(.*)?#'] = '%'. $x .'%$2';
117
            }
118
        }
119
120
        foreach ($array as $x => $value) {
121
            if (is_string($value)) {
122
                $array[$x] = preg_replace(
123
                    array_keys($replace),
124
                    array_values($replace),
125
                    $value
126
                );
127
            } elseif (is_array($value)) {
128
                $this->prepVars($array[$x]);
129
            }
130
        }
131
    }
132
133
    #: Abstract Methods
134
135
    /**
136
     * Must be implemented by child class.
137
     * Gets called for each file to load.
138
     */
139
    abstract protected function loadFile($file);
140
141
    /**
142
     * Must be impletmented by child class.
143
     * Gets called when saving a configuration file.
144
     *
145
     * @param array $contents  config array to save
146
     * @return string formatted output
147
     */
148
    abstract protected function exportFormat($contents);
149
}
150
151
#: END OF ./src/FileParser/AbstractFileParser.php FILE
152