Passed
Push — master ( c3d752...3ad46d )
by 世昌
02:01
created

ConfigLoader::loadYaml()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\config;
3
4
use nebula\application\exception\JSONException;
5
use nebula\application\exception\YamlException;
6
7
/**
8
 * 文件配置类
9
 */
10
abstract class ConfigLoader
11
{
12
    public function loadConfig(string $path, array $extra =[]):?array
13
    {
14
        $data=null;
15
        if (!file_exists($path)) {
16
            $path =$this->resolve($path);
17
        }
18
        if ($path) {
19
            $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
20
            switch ($ext) {
21
                case 'yml': case 'yaml': $data = $this->loadYaml($path); break;
22
                case 'php': $data = include $path; break;
23
                case 'ini': $data = $this->loadIni($path); break;
24
                case 'json': default: $data = $this->loadJson($path);
25
            }
26
        }
27
        return $data;
28
    }
29
30
    protected function loadJson(string $path):array
31
    {
32
        $content = file_get_contents($path);
33
        $content =$this->parseValue($content, $extra);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $extra seems to be never defined.
Loading history...
34
        $data = json_decode($content, true);
35
        if (json_last_error()!==JSON_ERROR_NONE) {
36
            throw new JSONException(json_last_error());
37
        }
38
        return $data;
39
    }
40
41
    protected function loadIni(string $path):array
42
    {
43
        $content = file_get_contents($path);
44
        $content =$this->parseValue($content, $extra);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $extra seems to be never defined.
Loading history...
45
        return \parse_ini_string($content, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parse_ini_string($content, true) could return the type false which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
46
    }
47
48
    protected function loadYaml(string $path):array
49
    {
50
        if (function_exists('yaml_parse')) {
51
            $name = 'yaml_parse';
52
        } elseif (class_exists('Spyc')) {
53
            $name = 'Spyc::YAMLLoadString';
54
        } else {
55
            throw new YamlException("parse yaml config error : missing yaml extension or spyc", 1);
56
        }
57
        $content = file_get_contents($path);
58
        $content =$this->parseValue($content, $extra);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $extra seems to be never defined.
Loading history...
59
        return \call_user_func_array($name, [$content]);
60
    }
61
62
    abstract public function parseValue(string $content, array $extra =[]):string;
63
64
    public function resolve(string $path):?string
65
    {
66
        if (file_exists($path)) {
67
            return $path;
68
        }
69
        $basepath = pathinfo($path, PATHINFO_FILENAME);
70
        if (file_exists($conf = $basepath.'.yml')  || file_exists($conf = $basepath.'.yaml')) {
71
            if (function_exists('yaml_parse') || class_exists('Spyc')) {
72
                return $conf;
73
            }
74
        }
75
        foreach (['.json','.php','.ini'] as $ext) {
76
            if (file_exists($conf=$basepath.$ext)) {
77
                return $conf;
78
            }
79
        }
80
        return null;
81
    }
82
}
83