Passed
Push — master ( b5dddf...91d417 )
by Richard
09:12
created

Yaml::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf;
13
14
use Symfony\Component\Yaml\Yaml as VendorYaml;
15
16
/**
17
 * Yaml dump and parse methods
18
 *
19
 * YAML is a serialization format most useful when human readability
20
 * is a consideration. It can be useful for configuration files, as
21
 * well as import and export functions.
22
 *
23
 * This file is a front end for a separate YAML package present in the
24
 * vendor directory. The intent is to provide a consistent interface
25
 * no mater what underlying library is actually used.
26
 *
27
 * At present, this class expects the symfony/yaml package.
28
 *
29
 * @category  Xmf\Yaml
30
 * @package   Xmf
31
 * @author    Richard Griffith <[email protected]>
32
 * @copyright 2013-2018 XOOPS Project (https://xoops.org)
33
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
34
 * @link      https://xoops.org
35
 * @see       http://www.yaml.org/
36
 */
37
class Yaml
38
{
39
40
    /**
41
     * Dump an PHP array as a YAML string
42
     *
43
     * @param mixed   $var    Variable which will be dumped
44
     * @param integer $inline Nesting level where you switch to inline YAML
45
     * @param integer $indent Number of spaces to indent for nested nodes
46
     *
47
     * @return string|bool YAML string or false on error
48
     */
49
    public static function dump($var, $inline = 4, $indent = 4)
50
    {
51
        try {
52
            $ret = VendorYaml::dump($var, $inline, $indent);
53
        } catch (\Exception $e) {
54
            static::logError($e);
55
            $ret = false;
56
        }
57
        return $ret;
58
    }
59
60
    /**
61
     * Load a YAML string into a PHP array
62
     *
63
     * @param string $yamlString YAML dump string
64
     *
65
     * @return array|boolean PHP array or false on error
66
     */
67
    public static function load($yamlString)
68
    {
69
        try {
70
            $ret = VendorYaml::parse($yamlString);
71
        } catch (\Exception $e) {
72
            static::logError($e);
73
            $ret = false;
74
        }
75
        return $ret;
76
    }
77
78
    /**
79
     * Read a file containing YAML into a PHP array
80
     *
81
     * @param string $yamlFile filename of YAML file
82
     *
83
     * @return array|boolean PHP array or false on error
84
     */
85
    public static function read($yamlFile)
86
    {
87
        try {
88
            $yamlString = file_get_contents($yamlFile);
89
            $ret = VendorYaml::parse($yamlString);
90
        } catch (\Exception $e) {
91
            static::logError($e);
92
            $ret = false;
93
        }
94
        return $ret;
95
    }
96
97
    /**
98
     * Save a PHP array as a YAML file
99
     *
100
     * @param array   $var      variable which will be dumped
101
     * @param string  $yamlFile filename of YAML file
102
     * @param integer $inline   Nesting level where you switch to inline YAML
103
     * @param integer $indent   Number of spaces to indent for nested nodes
104
     *
105
     * @return integer|boolean number of bytes written, or false on error
106
     */
107
    public static function save($var, $yamlFile, $inline = 4, $indent = 4)
108
    {
109
        try {
110
            $yamlString = VendorYaml::dump($var, $inline, $indent);
111
            $ret = file_put_contents($yamlFile, $yamlString);
112
        } catch (\Exception $e) {
113
            static::logError($e);
114
            $ret = false;
115
        }
116
        return $ret;
117
    }
118
119
    /**
120
     * Dump an PHP array as a YAML string with a php wrapper
121
     *
122
     * The wrap is a php header that surrounds the yaml with section markers,
123
     * '---' and '...' along with php comment markers. The php wrapper keeps the
124
     * yaml file contents from being revealed by serving the file directly from
125
     * a poorly configured server.
126
     *
127
     * @param mixed   $var    Variable which will be dumped
128
     * @param integer $inline Nesting level where you switch to inline YAML
129
     * @param integer $indent Number of spaces to indent for nested nodes
130
     *
131
     * @return string|boolean YAML string or false on error
132
     */
133
    public static function dumpWrapped($var, $inline = 4, $indent = 4)
134
    {
135
        try {
136
            $yamlString = VendorYaml::dump($var, $inline, $indent);
137
            $ret = empty($yamlString) ? false : "<?php\n/*\n---\n" . $yamlString . "\n...\n*/\n";
138
        } catch (\Exception $e) {
139
            static::logError($e);
140
            $ret = false;
141
        }
142
        return $ret;
143
    }
144
145
    /**
146
     * Load a YAML string with a php wrapper into a PHP array
147
     *
148
     * The wrap is a php header that surrounds the yaml with section markers,
149
     * '---' and '...' along with php comment markers. The php wrapper keeps the
150
     * yaml file contents from being revealed by serving the file directly from
151
     * a poorly configured server.
152
     *
153
     * @param string $yamlString YAML dump string
154
     *
155
     * @return array|boolean PHP array or false on error
156
     */
157
    public static function loadWrapped($yamlString)
158
    {
159
        try {
160
            $lines = preg_split('/\R/', $yamlString);
161
            $count = count($lines);
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
            $count = count(/** @scrutinizer ignore-type */ $lines);
Loading history...
162
            for ($index = $count; --$index > 0;) {
163
                if ('...' === $lines[$index]) {
164
                    array_splice($lines, $index);
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $input of array_splice() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
                    array_splice(/** @scrutinizer ignore-type */ $lines, $index);
Loading history...
165
                    break;
166
                }
167
            }
168
            $count = count($lines);
169
            for ($index = 0; ++$index < $count;) {
170
                if ('---' === $lines[$index]) {
171
                    array_splice($lines, 0, $index);
172
                    break;
173
                }
174
            }
175
            $unwrapped = implode("\n", $lines);
176
            $ret = VendorYaml::parse($unwrapped);
177
        } catch (\Exception $e) {
178
            static::logError($e);
179
            $ret = false;
180
        }
181
        return $ret;
182
    }
183
184
    /**
185
     * Read a file containing YAML with a php wrapper into a PHP array
186
     *
187
     * The wrap is a php header that surrounds the yaml with section markers,
188
     * '---' and '...' along with php comment markers. The php wrapper keeps the
189
     * yaml file contents from being revealed by serving the file directly from
190
     * a poorly configured server.
191
     *
192
     * @param string $yamlFile filename of YAML file
193
     *
194
     * @return array|boolean PHP array or false on error
195
     */
196
    public static function readWrapped($yamlFile)
197
    {
198
        try {
199
            $yamlString = file_get_contents($yamlFile);
200
            $ret = static::loadWrapped($yamlString);
201
        } catch (\Exception $e) {
202
            static::logError($e);
203
            $ret = false;
204
        }
205
        return $ret;
206
    }
207
208
    /**
209
     * Save a PHP array as a YAML file with a php wrapper
210
     *
211
     * The wrap is a php header that surrounds the yaml with section markers,
212
     * '---' and '...' along with php comment markers. The php wrapper keeps the
213
     * yaml file contents from being revealed by serving the file directly from
214
     * a poorly configured server.
215
     *
216
     * @param array   $var      variable which will be dumped
217
     * @param string  $yamlFile filename of YAML file
218
     * @param integer $inline   Nesting level where you switch to inline YAML
219
     * @param integer $indent   Number of spaces to indent for nested nodes
220
     *
221
     * @return integer|boolean number of bytes written, or false on error
222
     */
223
    public static function saveWrapped($var, $yamlFile, $inline = 4, $indent = 4)
224
    {
225
        try {
226
            $yamlString = static::dumpWrapped($var, $inline, $indent);
227
            $ret = file_put_contents($yamlFile, $yamlString);
228
        } catch (\Exception $e) {
229
            static::logError($e);
230
            $ret = false;
231
        }
232
        return $ret;
233
    }
234
235
    /**
236
     * @param \Exception $e throwable to log
237
     */
238
    protected static function logError($e)
239
    {
240
        if (class_exists('Xoops')) {
241
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
242
        } else {
243
            trigger_error($e->getMessage(), E_USER_ERROR);
244
        }
245
    }
246
}
247